Inspired from: https://github.com/SayantanRC/URLs/blob/master/repair-grub-live-usb.md
- Already installed NixOS partition for which bootloader needs to installed.
- Patience and some terminal knowledge is helpful.
Assuming linux is installed in /dev/sda3
. This will be different for your system. Use Gparted program to check.
- Boot from the USB drive. Open a terminal with "Alt+Ctrl+T".
- Mount the partition.
sudo mkdir -p /cdrom sudo mount /dev/sda3 /cdrom
- Bind mount some necessary partitions from live USB environment.
We also need to bind mount
sudo mount -o bind /dev /cdrom/dev/ sudo mount -o bind /proc /cdrom/proc/ sudo mount -o bind /sys /cdrom/sys/
resolv.conf
to be able to get internet access in chroot environment.
See how to connect to WiFi using terminal before running this command.sudo mount -o bind /etc/resolv.conf /cdrom/etc/resolv.conf
- Chroot into the mount point.
sudo chroot /cdrom /nix/var/nix/profiles/system/activate sudo chroot /cdrom /run/current-system/sw/bin/bash
- Now we are in the environment of the actual NixOS installation. Usually the bootloader is installed in
/dev/sda1
.
Hence we need to mount it. In NixOS we need to mount in under/boot
.sudo mount /dev/sda1 /boot
- Rebuild NixOS boot.
sudo nixos-rebuild boot --option sandbox false
- Reinstall bootloader
sudo NIXOS_INSTALL_BOOTLOADER=1 /nix/var/nix/profiles/system/bin/switch-to-configuration boot
- Finally, unmount partitions. In chroot environment (not all may be successful):
In live USB:
sudo umount /boot exit
sudo umount /cdrom/proc sudo umount /cdrom/sys sudo umount /cdrom/dev sudo umount /cdrom/etc/resolv.conf sudo umount /cdrom
Reboot and enjoy.
Sources:
NixOS/nix#5076 (comment)
https://nixos.wiki/wiki/Change_root
https://www.reddit.com/r/NixOS/comments/ouxjzh/how_to_reinstall_bootloader