Check why store paths aren't being garbage collected

Hi! I’ve been trying to garbage collect my nix store, but I’ve noticed that there are still a lot of duplicate packages left over. I have deleted my old generations on NixOS and home-manager, so I’m fairly confused what else could be referencing them.

Here is a screenshot showing a lot of duplicate packages with different versions:

Most notable ones would be linux-firmware and llvm-17.0.6, the latter being a custom derivation in my home config for development, which there should only be 1 of.

When I run nix-du, it doesn’t list any duplicates at all.

Is there any way to debug what derivations *in the store* are depended on by which other ones? I know about commands like nix why-depends, but that only seems to work on derivations that I can directly reference via nixpkgs#, which for example isn’t the case for my llvm derivation.

I haven’t found the answer to my specific question, i.e. I still don’t know how to check what depends on what.

However, I solved the root problem.
The root problem was that there are 3 different places I needed to clear generations/profiles to have proper garbage collection.

If I remember correctly, the commands I needed were:

# Delete all *system* generations except the last one 
sudo nix-env --delete-generations +1

# Delete all *user* profiles that are generated by NixOS (afaik)
nix-env --delete-generations +1

# Delete home-manager generations
home-manager expire-generations "-0 days"

More information on the delete-generations options here:

For home-manager expire-generations, I can’t for the life of me find the documentation page that explained the details (even though I clearly remember what it said and quote searched around google). It’s not even in my browsing history, as if I’ve been gaslit, but that feels pretty typical for NixOS related documentation.

Another source of derivations not being collected can be stray result links and other such files. You can do nix-store --gc --print-roots | grep -vE '^/(proc|nix)' to find them.


Never mind, that doesn’t actually delete system generations.
sudo nix-env --delete-generations --profile /nix/var/nix/profiles/system +1 does.