deadnix 0.1.3

Find unused code in Nix projects
deadnix-0.1.3 is not a library.


Scan .nix files for dead code (unused variable bindings).

Usage with Github Actions

See deadnix-action

Usage with Nix Flakes


$ nix run github:astro/deadnix -- --help
    deadnix [FLAGS] [FILE_PATHS]...

    -e, --edit             Remove unused code and write to source file
    -l, --no-lambda-arg    Don't check lambda parameter arguments
    -_, --no-underscore    Don't check any bindings that start with a _
    -q, --quiet            Don't print dead code report
    -h, --help             Prints help information
    -V, --version          Prints version information

    <FILE_PATHS>...    .nix files

Scan for unused code

$ nix run github:astro/deadnix test.nix
> unusedArgs@{ unusedArg, usedArg, ... }:
> ^^^^^^^^^^   ^^^^^^^^^
> |            |
> |            Unused lambda pattern: unusedArg
> Unused lambda pattern: unusedArgs
>   inherit (builtins) unused_inherit;
>                      ^^^^^^^^^^^^^^
>                      |
>                      Unused let binding: unused_inherit
>   unused = "fnord";
>   ^^^^^^
>   |
>   Unused let binding: unused
>   shadowed = 42;
>   ^^^^^^^^
>   |
>   Unused let binding: shadowed
>   _unused = unused: false;
>   ^^^^^^^   ^^^^^^
>   |         |
>   |         Unused lambda argument: unused
>   Unused let binding: _unused
>   x = { unusedArg2, x ? args.y, ... }@args: used1 + x;
>         ^^^^^^^^^^
>         |
>         Unused lambda pattern: unusedArg2

Remove unused code automatically

Do commit your changes into version control before!

$ nix run github:astro/deadnix -- -eq test.nix


Renaming of all unused to lambda args to start with _

If you disfavor marking them as unused, use option --no-lambda-arg.

nixpkgs callPackages with multiple imports

callPackages guesses the packages to inject by the names of a packages' lambda attrset pattern names. Some packages alias these with @args to pass them to another import ...nix args.

As the used args are only named in the imported file they will be recognized as dead in the package source file that is imported by callPackage, rendering it unable to guess the dependencies to call the packages with.

Use option --no-lambda-pattern-names in this case.

What if the produced reports are wrong?

Please open an issue. Do not forget to include the .nix code that produces incorrect results.

Commercial Support

The author can be hired to implement the features that you wish, or to integrate this tool into your toolchain.