Change bootloader from systemd to grub

I want to change my bootloader from systemd-boot to grub, for os-prober. These are the changes I made to configuration.nix:

-  boot.loader.systemd-boot.enable = true;
+  boot.loader.grub = {
+    enable = true;
+    useOSProber = true;
+    device = "/dev/sda";
+    efiSupport = true;
+  };

This is the current structure of boot partition:

/boot
├── 56cd92b57da3450a9d6712bf57525e7f
├── EFI
└── loader
    ├── entries
    │   ├── nixos-generation-26.conf
    │   ├── nixos-generation-27.conf
    │   ├── nixos-generation-28.conf
    │   ├── nixos-generation-29.conf
    │   ├── nixos-generation-30.conf
    │   ├── nixos-generation-31.conf
    │   └── nixos-generation-32.conf
    ├── loader.conf
    └── random-seed

Will the current generations be migrated over to grub when I do nixos-rebuild boot? What precautions should I take to avoid an unbootable system?

1 Like

there’s also the nixos-rebuild build-vm and nixos-rebuild build-vm-with-bootloader if you want to sanity check a configuration.

       build-vm
           Build a script that starts a NixOS virtual machine with the desired configuration. It
           leaves a symlink result in the current directory that points (under result/bin/
           run-hostname-vm) at the script that starts the VM. Thus, to test a NixOS configuration in
           a virtual machine, you should do the following:

               $ nixos-rebuild build-vm
               $ ./result/bin/run-*-vm

           The VM is implemented using the qemu package. For best performance, you should load the
           kvm-intel or kvm-amd kernel modules to get hardware virtualisation.

           The VM mounts the Nix store of the host through the 9P file system. The host Nix store is
           read-only, so Nix commands that modify the Nix store will not work in the VM. This
           includes commands such as nixos-rebuild; to change the VM’s configuration, you must halt
           the VM and re-run the commands above.

           The VM has its own ext3 root file system, which is automatically created when the VM is
           first started, and is persistent across reboots of the VM. It is stored in
           ./hostname.qcow2.

       build-vm-with-bootloader
           Like build-vm, but boots using the regular boot loader of your configuration (e.g., GRUB
           1 or 2), rather than booting directly into the kernel and initial ramdisk of the system.
           This allows you to test whether the boot loader works correctly. However, it does not
           guarantee that your NixOS configuration will boot successfully on the host hardware
           (i.e., after running nixos-rebuild switch), because the hardware and boot loader
           configuration in the VM are different. The boot loader is installed on an automatically
           generated virtual disk containing a /boot partition, which is mounted read-only in the
           VM.

obviously some limitations compared to bare metal, but I would still say low risk.

1 Like

I tried to switch to grub and in the process encountered some weird behavior. I made these changes to nixos configuration:

  boot.loader = {
    efi = {
      canTouchEfiVariables = true;
      efiSysMountPoint = "/boot/efi";
    };
  };

-  fileSystems."/boot" =
+  fileSystems."/boot/efi" =

and rebooted. Now the efi partition is mounted on both /boot and /boot/efi. And thats not all. The boot partition automatically gets mounted again if I unmount and try to access it:

[root@nixos:/etc/nixos]# lsblk /dev/sda1
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda1   8:1    0  512M  0 part /boot

[root@nixos:/etc/nixos]# umount /boot

[root@nixos:/etc/nixos]# lsblk /dev/sda1
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda1   8:1    0  512M  0 part

[root@nixos:/etc/nixos]# ls /boot
56cd92b57da3450a9d6712bf57525e7f  EFI  loader

[root@nixos:/etc/nixos]# lsblk /dev/sda1
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda1   8:1    0  512M  0 part /boot

This is my complete nixos configuration: (https://gist.github.com/fctorial/d350fead3e047f0e7d737e6520e84ae6).

Did you get GRUB working?

I’m trying to do the same thing but can’t get rid of systemd-boot. Even when enabling GRUB, my system still uses systemd-boot.

1 Like

I have the same issue

sudo -s
export NIXOS_INSTALL_BOOTLOADER=1
nixos-rebuild switch ...

If you’ve configured the system properly, this should cause the systemd-boot OR grub bootloader build paths to execute efibootmgr to poke the correct entry into your BIOS’s boot order. (The old one may remain, you may have to use efibootmgr or your BIOS’s menu to remove it. I’ve noticed the entries often go away if you go into /boot and delete the old bootloader’s executable.)

1 Like