LinuxCommandLibrary

nix-why-depends

Show why a package depends on something

TLDR

Show why the currently running NixOS system requires a certain store path

$ nix why-depends [/run/current-system] /nix/store/[checksum-package-version.ext]
copy

Show why a package from nixpkgs requires another package as a _build-time_ dependency
$ nix why-depends --derivation [nixpkgs#dependent] [nixpkgs#dependency]
copy

SYNOPSIS

nix-why-depends [options...] <drv-path> <dependency-path>

PARAMETERS

-h, --help
    Display usage information and exit.

--all
    Print all dependency paths, not just one.

DESCRIPTION

nix-why-depends is a diagnostic tool in the Nix package manager ecosystem. It analyzes the dependency graph of a Nix derivation and prints human-readable paths explaining why a specific dependency is required, either directly or transitively. This is invaluable for debugging bloated dependency closures, minimizing environments, or understanding why a package pulls in unexpected libraries.

Invoked with a derivation store path (e.g., from nix-build or nix-store) and a dependency store path, it traverses the runtime dependency tree using nix-store -qR. By default, it shows one representative path. The output lists sequential dependencies like:
drv-path
→ intermediate.drv
→ dependency-path

This reveals exact propagation chains, such as a binary linking to a library via a wrapper script. Use cases include optimizing NixOS configurations, reducing flake inputs, or investigating cross-compilation issues. It operates on fixed store paths, ensuring reproducibility, and integrates seamlessly with nix repl or scripts for automated analysis.

CAVEATS

Requires absolute Nix store paths (/nix/store/...). Only shows runtime dependencies (not build-time). Fails if dependency not in transitive closure.

EXAMPLE

nix-why-depends $(nix-build '<nixpkgs>' -A hello) $(nix-store -qd $(nix-build '<nixpkgs>' -A hello) | grep glibc | head -1)

Sample output:
/nix/store/...-hello-2.12
→ /nix/store/...-hello-2.12
→ /nix/store/...-glibc-2.35-... (via patchelf or rpath)

INTEGRATION TIP

Combine with nix-store -qR --tree for visual graphs, or nix eval in flakes: nix eval .#packages.x86_64-linux.hello.drvPath | xargs nix-why-depends ...

HISTORY

Developed in Nixpkgs around 2014-2015 as why-depends.sh script; evolved into standalone nix-why-depends binary for better usability in Nix 2.x+ era, aiding reproducible builds and minimalism focus.

SEE ALSO

nix-store(1), nix-build(1), nix repl(1)

Copied to clipboard