Collect garbage but keep build inputs

If I understand right, normal garbage collection deletes build inputs that aren’t used at runtime. Is it possible to gc but keep everything that went into the build of the currently-live objects?

I wonder if inputDerivation can be used for this? I don’t know how to automate that for all currently-live objects, but if you wanted to preserve all the inputs for hello, I think you could make hello.inputDerivation a gc root.

I believe you can do this by setting:

keep-outputs = true
keep-derivations = true

in your nix.conf (or nix.extraOptions on NixOS).

it deletes everything that doesn’t have a gc root, this is effectively any symlinks located in /nix/var/nix/gcroots/. Generally, nix takes care of this when you do a nix-build, nix-env --profile, nixos-rebuild, or nix-store --add-root.

For projects, which change over time, you may want to look at GitHub - target/lorri: Your project's nix-env which is a daemon which will manage the gcroots and build the shell environment in the background.