How to delete new generations

This is usually opposite to what people want, but here’s my rationale:

I often make use of NixOS’ generations to test really crazy, possibly breaking changes to my system. If the changes break, I have an easy escape hatch. The problem is that I cannot erase this new broken generation from my system unless I nixos-rebuild the system and clear out the “old generations”. This is an annoying extra step that I’d like to avoid, so I was wondering how to do that. I’m not confident in messing with /nix/var/nix/profiles directly, hence why I’m asking here.

1 Like

For most such tests, I would recommend using nixos-rebuild test, so no new generation is created at all. You can always add it later if things are fine.

If the test absolutely requires a reboot into the new generation, however, then I would say to use nix-env -p /nix/var/nix/profiles/system --switch-generation and nix-env -p /nix/var/nix/profiles/system --delete-generations to revert the “current” generation to a good one, and delete the bad one.

3 Likes

Normally yes, but the changes I make sometimes are for services that I need to fire during boot, so I can’t really test them without actually rebooting. One of these days I should try and tinker with virtualisation.vmVariant so that I can just build-vm instead…

Awesome thank you!

1 Like

Another option could be to use specialisations so that you know exactly what differs between your base config and the specialised config. Once you’re sure everything’s good, you can move it over to your base config and your specialisation would essentially be empty.

3 Likes