Is there a way to prevent the first generation to be garbage collected by nix-collect-garbage?

Is there a way to prevent the first generation (or some other generations) to be garbage collected by nix-collect-garbage?

Thanks,
AM

for a generation to be collected it first has to be deleted. don’t delete generations you want to keep?

1 Like

nix-collect-garbage doesn’t delete any gcroots. Perhaps you meant nix-collect-garbage -d, or another alternate mode?

Also, the “first generation” is a very strange thing to want to keep, assuming that means “oldest generation”. If that means “newest generation” instead, then don’t worry, nix-collect-garbage -d and its companions never delete that.

Yes, I meant the oldest generation. Also as my disk space is limited, I periodically run nix-collect-garbage -d.

Well, wanting to keep the oldest generation and running nix-collect-garbage -d are not compatible.

If you want a pattern of generation deletion not supported by nix-collect-garbage’s options, you’ll need to write something yourself that deletes the right profile symlinks, then run nix-collect-garbage with no options.

I think a feature to pin/freeze/protect a given generation that nix-garbage-collect skips would be nice.

1 Like

Why? Seriously, this seems like a really strange thing to want…

Why is it strange?

I want nix-garbage-collect -d to skip certain flagged generations.

It is for convenience.

Convenience of what?

I think you mean that you want to delete selected generations and after that run ‘nix-garbage-collect‘

sudo nix-env -p /nix/var/nix/profiles/system --delete-generations 2 3 5 99

and i found a sh on wiki

wiki_nix

Convenience of running nix-garbage-collect -d, and knowing certain generations won’t get deleted (1, 13, 17, 73, etc.), without memorizing them.

Of course I can write a bash script to do it, but having it built-in would be nice.

For example:

nix-protect-generations 1 13 17 73

after which nix-garbage-collect -d skips those generations automatically.

Also why are you so angry?

It doesn’t seem convenient because why would you want to boot into the first generation? That doesn’t make sense, it will be insecure and completely different, missing any programs and changes you made since then. If you have a working recent generation you should keep that instead.

Also, keeping really old generations will use more disk space as they will have less overlap with the current generation even with store optimisation enabled.

1 Like

Even if I don’t want to keep the first generation, I might want to keep some of the later generations.

2 Likes

You’re still avoiding the real question: why would you want to keep specific generations around beyond normal time/count constraints?

I’m not angry. Sorry if it seems that way. I am mildly frustrated because this feels distinctly like an XY Problem, and you’re avoiding giving me the information I need to actually help you.

not really I imagine saving a good one before testing a whole lot of potentially breaking changes makes a lot of sense

Better to use nixos-rebuild test for that, so they don’t go in the generation list at all.

1 Like

To be fair in some cases that isn’t possible, especially if the new generation needs a reboot.

4 Likes

I think generations with a profile name nixos-rebuild switch --profile-name <name here> do not get deleted by nix-collect-garbage
(and I think also usually show up lower down in the boot menu)

So that might be the solution for you
(but as others have said, apart from temporarily testing something else, there is little reason to keep previous generations around, especially if you put your .nix file in a git repo)