Gcroot for partial nixos-rebuild

Hello, I was wondering if there’s any way to create a root to prevent the garbage collector from cleaning up the packages downloaded by the latest nixos-rebuild in cases where the rebuild does not succeed?

As I’m running unstable and some other flakes, so it’s not uncommon for a rebuild to fail. Often these can be fixed by switching a package to my input for nixpkgs-stable or nixpkgs-master, but sometimes end up in the ‘check on this later’ pile.

I enabled nix.gc.automatic. If the garbage collector runs before a new rebuild generation completes it will delete the downloaded packages, resulting in them all being downloaded again next rebuild.

Any ideas on how that could be avoided?

you could make the garbage collector less aggressive using max-free and min-free maybe?

Make sure you have

  nix.settings.keep-outputs = true;

In your configuration.nix (and rebuild).

Then if a nixos-rebuild fails (assuming non-flakes), you can run

nix-instantiate '<nixpkgs/nixos>' -A config.system.build.toplevel --add-root system

to prevent garbage collection of already-downloaded/built paths for the system.

Ideally nixos-rebuild would have this functionality built-in


I suppose the way with flakes would be (replace MACHINE-NAME with flake output name)?

nix-instantiate $(nix path-info --derivation .#nixosConfigurations.MACHINE-NAME.config.system.build.toplevel) --add-root system