Scan Nix files for dead code
Go to file
2022-02-04 17:06:57 +01:00
.github build(deps): bump DeterminateSystems/update-flake-lock from 6 to 7 2022-01-31 18:13:35 +00:00
src update clap dependency from 2 to 3 2022-01-14 19:37:38 +01:00
.gitignore PoC 2021-12-06 00:04:52 +01:00
Cargo.lock update clap dependency from 2 to 3 2022-01-14 19:37:38 +01:00
Cargo.toml Cargo.toml: bump version to 0.1.3 2022-01-14 19:38:49 +01:00
example.nix test.nix: rename to exmaple.nix 2022-02-04 17:06:57 +01:00
flake.lock flake.lock: Update 2022-01-23 00:48:57 +00:00
flake.nix flake.nix: use naersk from github:nix-community 2021-12-15 00:55:54 +01:00
LICENSE LICENSE: add 2021-12-06 00:13:14 +01:00
README.md README: link deadnix-action 2021-12-13 23:13:21 +01:00

deadnix

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

Usage with Github Actions

See deadnix-action

Usage with Nix Flakes

Help

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

FLAGS:
    -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

ARGS:
    <FILE_PATHS>...    .nix files

Scan for unused code

$ nix run github:astro/deadnix test.nix
test.nix:1:
> unusedArgs@{ unusedArg, usedArg, ... }:
> ^^^^^^^^^^   ^^^^^^^^^
> |            |
> |            Unused lambda pattern: unusedArg
> Unused lambda pattern: unusedArgs
test.nix:3:
>   inherit (builtins) unused_inherit;
>                      ^^^^^^^^^^^^^^
>                      |
>                      Unused let binding: unused_inherit
test.nix:5:
>   unused = "fnord";
>   ^^^^^^
>   |
>   Unused let binding: unused
test.nix:10:
>   shadowed = 42;
>   ^^^^^^^^
>   |
>   Unused let binding: shadowed
test.nix:11:
>   _unused = unused: false;
>   ^^^^^^^   ^^^^^^
>   |         |
>   |         Unused lambda argument: unused
>   Unused let binding: _unused
test.nix:13:
>   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

Behavior

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.