How to remove grub entries in `grub.cfg`

Normally you can just run sudo nixos-rebuild switch which will replace your grub.cfg with only the live system profiles. The live system profiles are the symlinks /nix/var/nix/profiles. However, this does not work if you do not have a configuration.nix on your NixOS box like with many server deployments.

In that case you can run, as root, nix-env --delete-generations old --profile /nix/var/nix/profiles/system followed by /nix/var/nix/profiles/system/bin/switch-to-configuration switch.

Running nix-env --delete-generations old --profile /nix/var/nix/profiles/system will remove all but the newest system garbage collection roots and /nix/var/nix/profiles/system/bin/switch-to-configuration switch will reload the system configuration you are already on and update grub.cfg with only the live system profiles. Thus your grub.cfg will only have one entry in it and you have not had to change your system configuration. If you want to then garbage collect the old profiles you can run nix-store --gc .

2 Likes

Isn’t nix-collect-garbage -d supposed to do all this?

1 Like

nix-collect-garbage -d does not clean grub boot entries. Remember it is a Nix tool, not a NixOS tool. It would be really strange if your Nix installation on say a Debian system was editing your grub boot entries, see issue 3542.

1 Like

From the wiki https://nixos.wiki/wiki/Bootloader

How do I remove older generations from the bootloader?

First, collect garbages in your system, then rebuild. The scripts will collect leftover files.

The first command, in the example below, removes everything older than 14 days.

sudo nix-collect-garbage --delete-older-than 14d sudo nixos-rebuild boot

1 Like

If you have a your /etc/nixos/configuration.nix on your server, then you can run sudo nix-collect-garbage --delete-older-than 14d to remove system generations followed by sudo nixos-rebuild boot which actually updates your grub.cfg. However, this does not work if you do not have your configuration on your server like with most deployments, since you cannot run nixos-rebuild unless your configuration.nix is on your server.

1 Like