How to remove old NixOS generations in your system and bootloader — Hugo Sum

How to remove old NixOS generations in your system and bootloader

I remembered running around 60 times of nixos-rebuild switch to make all my hardware and Wi-Fi works in NixOS, in the beginning of my dual booting NixOS and Windows adventure. The next time when I boot, I saw all those old and imperfect generations and I had to press the Down button for 3 seconds until I reach the Windows 11 boot options… Those generations have polluted my bootloader menu and took over my disk space, which is a pain in the back side.

In this Nix tips, I am going to show you a few tricks to remove and avoid keeping old NixOS generations in your system and bootloader menu.

Limit the number of generations kept by NixOS

There is a NixOS configuration called boot.loader.systemd-boot.configurationLimit for limiting the maximum number of generations to keep in your system. When the generations you have exceeded the limit, the oldest one will be removed from your boot menu and marked for garbage collection.

# in your configuration.nix
{ config, pkgs, lib, ... }:

{
    boot.loader.systemd-boot.configurationLimit = 10; # only 10 generations are kept
}

Use nixos-rebuild test and nixos-rebuild dry-activate for testing your configuration

nixos-rebuild switch should be the first command you came across when you start your NixOS, but you should not abuse this command for just experimenting configuration. By itself, this command would build, activate and store a generation in the bootloader menu. Using this for testing, you will easily pollute your bootloader menu.

Unlike nixos-rebuild switch, nixos-rebuild test would only build and activate a new generation based on your current configuration, and it would not add this generation to the bootloader menu. This is really useful if you just want to try out a new configuration.

# if you are not using Nix Flake to manage your NixOS configuration
nixos-rebuild test
# if you are using Nix Flake to manage your NixOS configuration
nixos-rebuild test --flake .#replaceMeWithYourSystemName --use-remote-sudo

If you just want to verify if your current configuration would build without error, you can use nixos-rebuild dry-activate. This command would only build your configuration, but it would not activate or store it in the bootloader menu. On top of that, it would show what changes would be made comparing with the current one, if this generation is activated.

# if you are not using Nix Flake to manage your NixOS configuration
nixos-rebuild dry-activate
# if you are using Nix Flake to manage your NixOS configuration
nixos-rebuild dry-activate --flake .#replaceMeWithYourSystemName --use-remote-sudo

I highly recommend you to check out the doc of nixos-rebuild, and discover other available arguments.

Periodically clean up old generations

NixOS provides an option called nix.gc, allowing you to set up a systemd service to automatically garbage collect old generations for you.

# in your configuration.nix
{ config, pkgs, lib, ... }:

{
  nix.gc = {
    automatic = true;
    dates = "*-*-* 21:00:00"; # conduct a GC operation every night at 2100
    options = "--delete-older-than 7d";
  };
}

If you can’t wait and want to get rid of all generations right away, except the latest one, you can use nix-collect-garbage and nixos-rebuild boot.

Remember you have to run nix-collect-garbage with sudo or as root, otherwise the old generations would not be removed, yet the command would still exit without error.

sudo nix-collect-garbage -d
# if you are not using Nix Flake to manage your NixOS configuration
nixos-rebuild boot
# if you are using Nix Flake to manage your NixOS configuration
nixos-rebuild boot --flake .#replaceMeWithYourSystemName --use-remote-sudo

Hugo Sum

A Hongkonger living in the UK. The only thing I know is there is so much I don't know.

Archive