How to figure out which part of a derivation is pulling in a specific store path?

I don’t have any pkgs.python* in my OS:

❯ type -a python
bash: type: python: not found

After entering a Nix shell I’ve built, I notice there are two Python executables available:

❯ type -a python
python is /nix/store/z9nrcbp0bhwgml26rmxsrpn16q7pj28f-python3-3.11.9-env/bin/python
python is /nix/store/pgb120fb7srbh418v4i2a70aq1w9dawd-python3-3.12.5/bin/python

That is unexpected, since I only reference pkgs.python311 directly. I’d like to figure out what is pulling in pkgs.python3 (aka. pkgs.python312 in this version of nixpkgs); how do I do that?

I’ve already tried nix-store --query --referrers /nix/store/pgb120fb7srbh418v4i2a70aq1w9dawd-python3-3.12.5/bin/python, but it lists a whole bunch of packages I have installed elsewhere in the system, and I can’t tell which ones of the several hundred trace back to my shell.

Some options for a usable command would be

  • something which takes two store paths and prints the intermediate store paths,
  • something which takes the Nix shell (or its store path) and prints its recursive dependencies hierarchically, rather than a flat list, or
  • something which takes the Python store path and prints its reverse dependencies recursively and hierarchically.

nix why-depends

nix-store --query --tree but I much prefer using nix-tree.

nix-store --query --referrers-closure

8 Likes

Nice! In my case I can nix why-depends "$(nix-instantiate shell.nix)" /nix/store/pgb120fb7srbh418v4i2a70aq1w9dawd-python3-3.12.5/bin/python and nix-store --query --tree "$(nix-instantiate shell.nix)"/nix-tree "$(nix-instantiate shell.nix)", which all work great, thank you!

But if I take one of the dependencies of shell.nix and run nix-store --query --referrers-closure /nix/store/rcv47cyjhszzd3shx5m38d7psj0m6l87-gnumake-4.4.1.drv, I get a flat list rather than a hierarchy of reverse dependencies:

❯ nix-store --query --referrers-closure /nix/store/rcv47cyjhszzd3shx5m38d7psj0m6l87-gnumake-4.4.1.drv
/nix/store/rcv47cyjhszzd3shx5m38d7psj0m6l87-gnumake-4.4.1.drv
/nix/store/k9dvcwblf3kcldfg90932kwjzcgacbvg-nix-shell-env.drv
/nix/store/xdv188666i3rvg06v7bd9k8bilbn02l5-nix-shell-env.drv
/nix/store/z8lc0jvm0yddlnzj882klwp84dps62pf-nix-shell.drv

Indeed it does not appear possible to show the referrers-closure hierarchically.