Dual Booting NixOS and Windows: Difference between revisions

m remove original and have only archive mirror
Add method to boot Windows if it is installed on a different drive than NixOS
Line 121: Line 121:
==== EFI with multiple disks ====
==== EFI with multiple disks ====


When Windows is installed on another disk with a separate EFI partition, the following might work:
===== systemd-boot =====
As systemd-boot cannot directly load binaries from other ESPs<ref>https://github.com/systemd/systemd/issues/3252</ref>, let alone other disks, we have to employ [https://search.nixos.org/packages?channel=unstable&show=edk2-uefi-shell&from=0&size=50&sort=relevance&type=packages&query=edk2-uefi-shell edk2-uefi-shell] to implement a chainloading strategy<ref>https://wiki.archlinux.org/title/Systemd-boot#Boot_from_another_disk</ref>. The basic config looks like this:{{File|/etc/nixos/configuration.nix|nix|<nowiki>
{ config, ... }:
 
{
  boot.loader = {
    systemd-boot.enable = true;
    efi.canTouchEfiVariables = true;
 
    # Copy EDK2 Shell to boot partition
    systemd-boot.extraFiles."efi/shell.efi" = "${pkgs.edk2-uefi-shell}/shell.efi";
    systemd-boot.extraEntries = {
      # Chainload Windows bootloader via EDK2 Shell
      "windows.conf" =
        let
          # To determine the name of the windows boot drive, boot into edk2 first, then run
          # `map -c` to get drive aliases, and try out running `FS1:`, then `ls EFI` to check
          # which alias corresponds to which EFI partition.
          boot-drive = "FS1";
        in
        ''
          title Windows Bootloader
          efi /efi/shell.efi
          options -nointerrupt -nomap -noversion ${boot-drive}:EFI\Microsoft\Boot\Bootmgfw.efi
          sort-key y_windows
        '';
      # Make EDK2 Shell available as a boot option
      "edk2-uefi-shell.conf" = ''
        title EDK2 UEFI Shell
        efi /efi/shell.efi
        sort-key z_edk2
      '';
    };
  };
}</nowiki>}}You can try if this works without changes, but most likely you have to modify the value of <code>boot-drive</code> first to match your hardware configuration.
 
First, make sure you <code>nixos-rebuild switch</code> or <code>nixos-rebuild boot</code> , then reboot your machine and select the entry "EDK2 UEFI Shell" from the systemd-boot menu. In this shell, run the command <code>map -c</code> to get a list of all "consistent" device mappings:<syntaxhighlight lang="text">
Press ESC in 1 seconds to skip startup.nsh or any other key to continue.
Shell> map -c
Mapping table
    HD0c3: Alias(s):FS0:;BLK7:
          PciRoot(0x0)/Pci(0x1,0x1)/Ata(0x0)/HD(3,GPT,5CBAF773-8FFA-11EB-952D-FCAA14203853,0x1DAA6000,0x32000)
    HD0d1: Alias(s):FS1:;BLK10:
          PciRoot(0x0)/Pci(0x1,0x1)/Ata(0x0)/HD(1,GPT,7F623BEA-5891-49EE-9980-6534716F0F50,0x800,0x1F4000)
</syntaxhighlight>Then, change to each of these drives by entering the drive name (not one of the aliases!) and check whether the Windows bootloader is present:<syntaxhighlight lang="text">
Shell> HD0d1:
HD0d1:\> ls EFI
Directory of: HD0d1:\EFI\
09/21/2024  22:05 <DIR>        4,096  .
09/21/2024  22:05 <DIR>            0  ..
09/21/2024  23:08 <DIR>        4,096  BOOT
09/21/2024  22:05 <DIR>        4,096  Linux
09/24/2024  08:30 <DIR>        4,096  nixos
01/01/1980  00:00          1,060,672  shell.efi
09/21/2024  23:08 <DIR>        4,096  systemd
          1 File(s)  1,060,672 bytes
          6 Dir(s)
HD0d1:\> HD0c3:
HD0c3:\> ls EFI
Directory of: HD0c3:\EFI\
03/28/2021  21:28 <DIR>        1,024  .
03/28/2021  21:28 <DIR>            0  ..
03/28/2021  21:28 <DIR>        1,024  Boot
03/28/2021  21:28 <DIR>        1,024  Microsoft
          0 File(s)          0 bytes
          4 Dir(s)
</syntaxhighlight>In this case, HD0d1 is the ESP of our NixOS installation, and HD0c3 is the ESP of Windows.
 
After entering the Windows ESP, you can boot into it by running <code>EFI\Microsoft\Boot\Bootmgfw.efi</code>. This is also useful if you have multiple Windows installations and want to find out which ESP belongs to which installation.
 
After this, you can change the value of <code>boot-drive</code> in the configuration snippet above, <code>nixos-rebuild switch</code> and reboot to boot into windows. Make sure that you use the actual device name, not one of the aliases, as these might not be available immediately on boot, making the shell invocation fail.
 
===== Grub =====
In Grub, the following might work:


{{File|/etc/nixos/configuration.nix|nix|<nowiki>
{{File|/etc/nixos/configuration.nix|nix|<nowiki>