Prevent garbage collection of flake?

I’m using a flake.nix to set up my project’s development environment and then work under nix develop. Is there a way to prevent garbage collection of the packages installed by the flake?

1 Like


The easiest way is to use nix-direnv


Another method is to save the develop environment as what is called a “profile”. The naming is not ideal because it is not the exact same as the other profile mechanism, but develop has its own concept of a “profile”, see the help page. You run “nix develop —profile some-path” and it makes a symlink to a store path that will prevent gc of your packages.


Thanks for the pointer. Indeed nix develop —profile some-path creates a link from some-path into the nix store. But how does the nix garbage collector know about the existence of the link? Also, when one wants to remove the project at some point and garbage collect its flake in the store, how can this be done?

Nix keeps track of these profiles in the same way it keeps track of “normal profiles” and builds, it places a symlink into /nix/var/nix/gcroots/auto. When that link is broken, the next garbage collection will do the clean up. So removing that project, or just the symlink allows GC to get rid of it.

I see, things in /nix/var/nix/gcroots/auto can point outside the nix store. Thanks for explaining.