How to declaratively keep packages in store without adding them to environment.systemPackages?

Adding packages to environment.systemPackages will keep them (and their dependencies) from being GC’ed away.

However, there are packages that I would like to keep in the store without adding them to environment.systemPackages, and I’d like to do so declaratively (i.e., in my configuration.nix).

For example, I would like to keep kicad.libraries.packages3d.src, so the source doesn’t need to be re-downloaded to rebuild kicad (unless kicad is actually updated, of course).

You can use system.extraDependencies in that case. Anything listed there becomes a direct dependency of the built system configuration, so it won’t be collected until the configuration is (or, all of them, as you rebuild your system and multiple builds point to it).

You could perhaps have more control over the lifetime of the sources by doing a nix-build '<nixpkgs>' -A kicad.libraries.packages3d.src -o kicad-src, to create a symlink that will act as a GC root to keep the sources alive. Then you can remove it more easily, but it won’t be updated automatically when you update Nixpkgs.

Thanks! I think system.extraDependencies is exactly what I was looking for.

The other alternative won’t work for me because I want to specify it declaratively.