Dual Booting NixOS and Windows: Difference between revisions

From NixOS Wiki
imported>Samueldr
m (Mirrors structure for == MBR ==)
(→‎Grub: Remove old info)
 
(22 intermediate revisions by 15 users not shown)
Line 1: Line 1:
== MBR ==
This section explains various methods to have the bootloader prompt whether to boot windows or NixOS.
 
== Autodetection ==
 
=== systemd-boot ===
 
When {{ic|systemd-boot}} is installed to the same EFI System Partition (ESP) that Windows uses, it will automatically detect the Windows installation ({{ic|/EFI/Microsoft/Boot/bootmgfw.efi}}) and present it as a boot option.
 
You can verify detected boot loaders by running the {{ic|bootctl}} command.
 
A system pre-installed with Windows might have a small ESP partition size that is not sufficient to store the kernel and initrd files for multiple NixOS generations. One solution is to create an additional [https://uapi-group.org/specifications/specs/boot_loader_specification/#the-partitions XBOOTLDR] partition and configure {{ic|systemd-boot}} to use it:
 
{{File|/etc/nixos/configuration.nix|nix|<nowiki>
{
  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/57D4-A2B2";
      fsType = "vfat";
    };
  fileSystems."/efi" =
    { device = "/dev/disk/by-uuid/3280-5418";
      fsType = "vfat";
    };
 
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;
 
  boot.loader.efi.efiSysMountPoint = "/efi";
  boot.loader.systemd-boot.xbootldrMountPoint = "/boot";
}
</nowiki>}}
 
=== os-prober ===
 
<code>os-prober</code> is a tool to autodetect which other systems are present on the machine. Grub can be
told to use os-prober to add a menu-entry for each of them.
 
{{File|/etc/nixos/configuration.nix|nix|<nowiki>
{ config, pkgs, ... }: {
  # ...
  boot.loader.grub.enable = true;
  boot.loader.grub.device = "nodev";
  boot.loader.grub.useOSProber = true;
  # ...
 
}</nowiki>}}
 
== Manual configuration ==
In case <code>os-prober</code> does not detect your windows partition you can configure your bootloader manually to find it.
 
=== MBR ===


All MBR bootloaders will need at least some configuration to chainload Windows.
All MBR bootloaders will need at least some configuration to chainload Windows.


=== MBR + Grub ===
==== Grub ====


When using MBR on your disk then you can configure grub chain-loading:
Here is an example config:


<syntaxhighlight lang="nix">{
{{File|/etc/nixos/configuration.nix|nix|<nowiki>
{ config, pkgs, ... }: {
  # ...
   boot.loader.grub.enable = true;
   boot.loader.grub.enable = true;
  boot.loader.grub.version = 2;
   boot.loader.grub.device = "/dev/sda";
   boot.loader.grub.device = "/dev/sda";
   boot.loader.grub.extraEntries = ''
   boot.loader.grub.extraEntries = ''
Line 16: Line 66:
     }
     }
   '';
   '';
}</syntaxhighlight>
}</nowiki>}}
Source: https://www.reddit.com/r/NixOS/comments/31lx3i/windows_and_nixos_dual_boot/
Source: https://www.reddit.com/r/NixOS/comments/31lx3i/windows_and_nixos_dual_boot/


== EFI ==
=== UEFI ===


After setting up a 256mb EFI Partition dualboot should work out of the box (at least for windows10)
After setting up a 256mb EFI Partition dualboot should work out of the box (at least for windows10)


Source: https://zimbatm.com/journal/2016/09/09/nixos-window-dual-boot/
Source: https://zimbatm.com/journal/2016/09/09/nixos-window-dual-boot/ ([https://web.archive.org/web/20170523065625/https://zimbatm.com/journal/2016/09/09/nixos-window-dual-boot/ Archive.org Mirror])


=== EFI + Grub ===
Here is another article that documents dual booting NixOS and Windows on a Lenovo ThinkPad X1 Carbon (6th Gen): https://github.com/andywhite37/nixos/blob/master/DUAL_BOOT_WINDOWS_GUIDE.md


<nowiki>systemd-boot</nowiki> can not load EFI binaries from other partitions,
==== Grub ====
and a pre-exisitng EFI partition from a Windows install may be smaller than we would like our <nowiki>/boot</nowiki> partition to be. If we still want Windows and NixOS to use the same EFI partition, we can use GRUB instead.


Here we assume:
Here we assume:
* the EFI partition has been mounted on <nowiki>/boot/efi</nowiki>
* the EFI partition has been mounted on <nowiki>/boot/efi</nowiki>
* <nowiki>$FS_UUID</nowiki> is the UUID of the EFI partition
* <code>$FS_UUID</code> is the UUID of the EFI partition
* the <nowiki>boot.loader.systemd-boot.enable = true;</nowiki> line added to configuration.nix by <nowiki>nixos-generate-config</nowiki> has been removed
* the <code>boot.loader.systemd-boot.enable = true;</code> line added to configuration.nix by <code>nixos-generate-config</code> has been removed


<syntaxhighlight lang="nix">
{{File|/etc/nixos/configuration.nix|nix|<nowiki>
{ config, ... }:
{ config, ... }:


Line 42: Line 91:
     efi = {
     efi = {
       canTouchEfiVariables = true;
       canTouchEfiVariables = true;
       # assuming /boot/efi is the mount point of the  EFI partition in NixOS
       # assuming /boot is the mount point of the  EFI partition in NixOS (as the installation section recommends).
       efiSysMountPoint = "/boot/efi";
       efiSysMountPoint = "/boot";
     };
     };
     grub = {
     grub = {
Line 61: Line 110:
           insmod search_fs_uuid
           insmod search_fs_uuid
           insmod chain
           insmod chain
           search --fs--uid --set=root $FS_UUID
           search --fs-uuid --set=root $FS_UUID
           chainloader /EFI/Microsoft/Boot/bootmgfw.efi
           chainloader /EFI/Microsoft/Boot/bootmgfw.efi
         }
         }
Line 68: Line 117:
     };
     };
   };
   };
}</syntaxhighlight>
}</nowiki>}}
 
==== EFI with multiple disks ====
 
When Windows is installed on another disk with a separate EFI partition, the following might work:
 
{{File|/etc/nixos/configuration.nix|nix|<nowiki>
{ config, ... }:
 
{
  boot.loader = {
    efi.canTouchEfiVariables = true;
    grub = {
      enable = true;
      devices = [ "nodev" ];
      efiSupport = true;
      useOSProber = true;
    };
  };
}</nowiki>}}


Sources:
== System time ==
* [https://wiki.archlinux.org/index.php/GRUB#Windows_installed_in_UEFI-GPT_Mode_menu_entry Arch Wiki GRUB article]
 
System clock might be incorrect after booting Windows and going back to the NixOS. It can be fixed by either setting RTC time standard to UTC on Windows, or setting it to localtime on NixOS.
 
Setting RTC time standard to localtime, compatible with Windows in its default configuration:
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{
  time.hardwareClockInLocalTime = true;
}</nowiki>}}
 
See [https://wiki.archlinux.org/title/System_time#Time_standard Arch Linux wiki#System time].
 
== See also ==
* [https://wiki.archlinux.org/index.php/GRUB#Windows_installed_in_UEFI-GPT_Mode_menu_entry Arch Linux wiki#GRUB]
* [https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/system/boot/loader/grub/install-grub.pl NixOS GRUB installer] (check the code block beginning with <nowiki># install EFI GRUB</nowiki>)
* [https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/system/boot/loader/grub/install-grub.pl NixOS GRUB installer] (check the code block beginning with <nowiki># install EFI GRUB</nowiki>)
[[Category:Cookbook]][[Category:NixOS]]

Latest revision as of 05:32, 8 April 2024

This section explains various methods to have the bootloader prompt whether to boot windows or NixOS.

Autodetection

systemd-boot

When systemd-boot is installed to the same EFI System Partition (ESP) that Windows uses, it will automatically detect the Windows installation (/EFI/Microsoft/Boot/bootmgfw.efi) and present it as a boot option.

You can verify detected boot loaders by running the bootctl command.

A system pre-installed with Windows might have a small ESP partition size that is not sufficient to store the kernel and initrd files for multiple NixOS generations. One solution is to create an additional XBOOTLDR partition and configure systemd-boot to use it:

/etc/nixos/configuration.nix
{
  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/57D4-A2B2";
      fsType = "vfat";
    };
  fileSystems."/efi" =
    { device = "/dev/disk/by-uuid/3280-5418";
      fsType = "vfat";
    };

  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  boot.loader.efi.efiSysMountPoint = "/efi";
  boot.loader.systemd-boot.xbootldrMountPoint = "/boot";
}

os-prober

os-prober is a tool to autodetect which other systems are present on the machine. Grub can be told to use os-prober to add a menu-entry for each of them.

/etc/nixos/configuration.nix
{ config, pkgs, ... }: {
  # ...
  boot.loader.grub.enable = true;
  boot.loader.grub.device = "nodev";
  boot.loader.grub.useOSProber = true;
  # ...

}

Manual configuration

In case os-prober does not detect your windows partition you can configure your bootloader manually to find it.

MBR

All MBR bootloaders will need at least some configuration to chainload Windows.

Grub

Here is an example config:

/etc/nixos/configuration.nix
{ config, pkgs, ... }: {
  # ...
  boot.loader.grub.enable = true;
  boot.loader.grub.device = "/dev/sda";
  boot.loader.grub.extraEntries = ''
    menuentry "Windows 7" {
      chainloader (hd0,1)+1
    }
  '';
}

Source: https://www.reddit.com/r/NixOS/comments/31lx3i/windows_and_nixos_dual_boot/

UEFI

After setting up a 256mb EFI Partition dualboot should work out of the box (at least for windows10)

Source: https://zimbatm.com/journal/2016/09/09/nixos-window-dual-boot/ (Archive.org Mirror)

Here is another article that documents dual booting NixOS and Windows on a Lenovo ThinkPad X1 Carbon (6th Gen): https://github.com/andywhite37/nixos/blob/master/DUAL_BOOT_WINDOWS_GUIDE.md

Grub

Here we assume:

  • the EFI partition has been mounted on /boot/efi
  • $FS_UUID is the UUID of the EFI partition
  • the boot.loader.systemd-boot.enable = true; line added to configuration.nix by nixos-generate-config has been removed
/etc/nixos/configuration.nix
{ config, ... }:

{
  boot.loader = {
    efi = {
      canTouchEfiVariables = true;
      # assuming /boot is the mount point of the  EFI partition in NixOS (as the installation section recommends).
      efiSysMountPoint = "/boot";
    };
    grub = {
      # despite what the configuration.nix manpage seems to indicate,
      # as of release 17.09, setting device to "nodev" will still call
      # `grub-install` if efiSupport is true
      # (the devices list is not used by the EFI grub install,
      # but must be set to some value in order to pass an assert in grub.nix)
      devices = [ "nodev" ];
      efiSupport = true;
      enable = true;
      # set $FS_UUID to the UUID of the EFI partition
      extraEntries = ''
        menuentry "Windows" {
          insmod part_gpt
          insmod fat
          insmod search_fs_uuid
          insmod chain
          search --fs-uuid --set=root $FS_UUID
          chainloader /EFI/Microsoft/Boot/bootmgfw.efi
        }
      '';
      version = 2;
    };
  };
}

EFI with multiple disks

When Windows is installed on another disk with a separate EFI partition, the following might work:

/etc/nixos/configuration.nix
{ config, ... }:

{
  boot.loader = {
    efi.canTouchEfiVariables = true;
    grub = {
      enable = true;
      devices = [ "nodev" ];
      efiSupport = true;
      useOSProber = true;
    };
  };
}

System time

System clock might be incorrect after booting Windows and going back to the NixOS. It can be fixed by either setting RTC time standard to UTC on Windows, or setting it to localtime on NixOS.

Setting RTC time standard to localtime, compatible with Windows in its default configuration:

/etc/nixos/configuration.nix
{
  time.hardwareClockInLocalTime = true;
}

See Arch Linux wiki#System time.

See also