Removing "old" store paths

I’m not using NixOS but do have a project built using flake.nix. I generally go nix develop and then nix build to build the project. Over time my store has been growing. I’d like to garbage collect old store paths, but without having to rebuild the world next time I run nix develop.

I’m guessing that whenever I do a nix build, nix marks the package I’m going to build as a “root”, so if nix store --gc is run at the same time, it doesn’t trash packages that are currently in use. If there’s a timestamp on this marking, it should be relatively easy to work out the recency of each store path, by just defining the store path recency as the most recent recency anything that depends on it.

Does this information exist, and is there a way to use it to delete all store paths that are say, more than a month old, but keep everything else intact? I did see the option –delete-older-than, but this relates to profiles, which would do the job if every time I run nix develop a profile is created and stored but I’m not sure that’s the case.

Just a guess: have u tried nix-collect-garbage -d and/or sudo nix-collect-garbage -d?
IMHO the docs can be helpful: Storage optimization - NixOS Wiki


You can make it the case with the --profile flag; I use a small wrapper for nix develop that uses this flag to maintain a profile in the local project directory, and another small script that deletes older generations from those profiles:

find /nix/var/nix/gcroots/auto -lname '*/.profile/develop-*' -printf '%l\0' \
  | xargs -0 dirname \
  | sort -u \
  | xargs -i nix-env -p '{}/develop' --delete-generations 30d

Wish this were more out-of-the-box, but I don’t know if it’s ‘good’ per the current design for nix develop.