How to GC without losing dependencies?

How can I use nix-store --gc together with gcroots to remove old unused dependencies but leave those that are still used to build my project? Just adding the final derivation of my project to gcroots doesn’t seem to prevent it’s dependencies from being garbage collected.

I saw a script that someone used to add all of the dependencies to gcroots, but that’s a very slow process and running it every time the derivation builds makes no sense. Is there a way to garbage collect orphaned dependencies but keep ones used in my project?

3 Likes

The --keep-outputs option (which can be set permanently in nix.conf with keep-outputs = true) instructs the GC to consider the outputs paths of live .drv paths to also be live. This is useful because the .drv path of any live output path is also considered live (via the keep-deriver option, which is on by default). So by enabling keep-outputs, you’re making all the build inputs live via their deriver paths.

4 Likes

Wow, what the hell! You’re absolutely right, and I have tried these settings before but they didn’t help. But prompted by your reply I tried using the options directly via the --option flag and it worked!

I don’t know why but nix-store --gc is ignoring my keep-outputs and keep-derivations settings in my nix.conf despite $NIX_CONF_DIR being set and pointing to it. I can even see it being opened when using strace:

15753 openat(AT_FDCWD, "/home/joe/project/nix/nix.conf", O_RDONLY|O_CLOEXEC) = 3
15753 fstat(3, {st_mode=S_IFREG|0644, st_size=628, ...}) = 0
15753 read(3, "keep-outputs = true\nkeep-derivat"..., 628) = 628
15753 close(3)                          = 0

But for some reason my settings are being ignored.

Thanks for reminding me about the --option flag. But this is weird.

1 Like