mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-07-13 21:50:33 +03:00
nixos/systemd-boot: Simpler windows dual booting (#344327)
This commit is contained in:
commit
12ef18d2e3
3 changed files with 593 additions and 272 deletions
|
@ -1,4 +1,9 @@
|
|||
{ config, lib, pkgs, ... }:
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
|
||||
with lib;
|
||||
|
||||
|
@ -10,16 +15,21 @@ let
|
|||
# We check the source code in a derivation that does not depend on the
|
||||
# system configuration so that most users don't have to redo the check and require
|
||||
# the necessary dependencies.
|
||||
checkedSource = pkgs.runCommand "systemd-boot" {
|
||||
preferLocalBuild = true;
|
||||
} ''
|
||||
install -m755 -D ${./systemd-boot-builder.py} $out
|
||||
${lib.getExe pkgs.buildPackages.mypy} \
|
||||
--no-implicit-optional \
|
||||
--disallow-untyped-calls \
|
||||
--disallow-untyped-defs \
|
||||
$out
|
||||
'';
|
||||
checkedSource =
|
||||
pkgs.runCommand "systemd-boot"
|
||||
{
|
||||
preferLocalBuild = true;
|
||||
}
|
||||
''
|
||||
install -m755 -D ${./systemd-boot-builder.py} $out
|
||||
${lib.getExe pkgs.buildPackages.mypy} \
|
||||
--no-implicit-optional \
|
||||
--disallow-untyped-calls \
|
||||
--disallow-untyped-defs \
|
||||
$out
|
||||
'';
|
||||
|
||||
edk2ShellEspPath = "efi/edk2-uefi-shell/shell.efi";
|
||||
|
||||
systemdBootBuilder = pkgs.substituteAll rec {
|
||||
name = "systemd-boot";
|
||||
|
@ -44,13 +54,17 @@ let
|
|||
|
||||
configurationLimit = if cfg.configurationLimit == null then 0 else cfg.configurationLimit;
|
||||
|
||||
inherit (cfg) consoleMode graceful editor rebootForBitlocker;
|
||||
inherit (cfg)
|
||||
consoleMode
|
||||
graceful
|
||||
editor
|
||||
rebootForBitlocker
|
||||
;
|
||||
|
||||
inherit (efi) efiSysMountPoint canTouchEfiVariables;
|
||||
|
||||
bootMountPoint = if cfg.xbootldrMountPoint != null
|
||||
then cfg.xbootldrMountPoint
|
||||
else efi.efiSysMountPoint;
|
||||
bootMountPoint =
|
||||
if cfg.xbootldrMountPoint != null then cfg.xbootldrMountPoint else efi.efiSysMountPoint;
|
||||
|
||||
nixosDir = "/EFI/nixos";
|
||||
|
||||
|
@ -60,29 +74,35 @@ let
|
|||
|
||||
netbootxyz = optionalString cfg.netbootxyz.enable pkgs.netbootxyz-efi;
|
||||
|
||||
edk2-uefi-shell = optionalString cfg.edk2-uefi-shell.enable pkgs.edk2-uefi-shell;
|
||||
|
||||
checkMountpoints = pkgs.writeShellScript "check-mountpoints" ''
|
||||
fail() {
|
||||
echo "$1 = '$2' is not a mounted partition. Is the path configured correctly?" >&2
|
||||
exit 1
|
||||
}
|
||||
${pkgs.util-linuxMinimal}/bin/findmnt ${efiSysMountPoint} > /dev/null || fail efiSysMountPoint ${efiSysMountPoint}
|
||||
${lib.optionalString
|
||||
(cfg.xbootldrMountPoint != null)
|
||||
"${pkgs.util-linuxMinimal}/bin/findmnt ${cfg.xbootldrMountPoint} > /dev/null || fail xbootldrMountPoint ${cfg.xbootldrMountPoint}"}
|
||||
${lib.optionalString (cfg.xbootldrMountPoint != null)
|
||||
"${pkgs.util-linuxMinimal}/bin/findmnt ${cfg.xbootldrMountPoint} > /dev/null || fail xbootldrMountPoint ${cfg.xbootldrMountPoint}"
|
||||
}
|
||||
'';
|
||||
|
||||
copyExtraFiles = pkgs.writeShellScript "copy-extra-files" ''
|
||||
empty_file=$(${pkgs.coreutils}/bin/mktemp)
|
||||
|
||||
${concatStrings (mapAttrsToList (n: v: ''
|
||||
${pkgs.coreutils}/bin/install -Dp "${v}" "${bootMountPoint}/"${escapeShellArg n}
|
||||
${pkgs.coreutils}/bin/install -D $empty_file "${bootMountPoint}/${nixosDir}/.extra-files/"${escapeShellArg n}
|
||||
'') cfg.extraFiles)}
|
||||
${concatStrings (
|
||||
mapAttrsToList (n: v: ''
|
||||
${pkgs.coreutils}/bin/install -Dp "${v}" "${bootMountPoint}/"${escapeShellArg n}
|
||||
${pkgs.coreutils}/bin/install -D $empty_file "${bootMountPoint}/${nixosDir}/.extra-files/"${escapeShellArg n}
|
||||
'') cfg.extraFiles
|
||||
)}
|
||||
|
||||
${concatStrings (mapAttrsToList (n: v: ''
|
||||
${pkgs.coreutils}/bin/install -Dp "${pkgs.writeText n v}" "${bootMountPoint}/loader/entries/"${escapeShellArg n}
|
||||
${pkgs.coreutils}/bin/install -D $empty_file "${bootMountPoint}/${nixosDir}/.extra-files/loader/entries/"${escapeShellArg n}
|
||||
'') cfg.extraEntries)}
|
||||
${concatStrings (
|
||||
mapAttrsToList (n: v: ''
|
||||
${pkgs.coreutils}/bin/install -Dp "${pkgs.writeText n v}" "${bootMountPoint}/loader/entries/"${escapeShellArg n}
|
||||
${pkgs.coreutils}/bin/install -D $empty_file "${bootMountPoint}/${nixosDir}/.extra-files/loader/entries/"${escapeShellArg n}
|
||||
'') cfg.extraEntries
|
||||
)}
|
||||
'';
|
||||
};
|
||||
|
||||
|
@ -91,23 +111,61 @@ let
|
|||
${systemdBootBuilder}/bin/systemd-boot "$@"
|
||||
${cfg.extraInstallCommands}
|
||||
'';
|
||||
in {
|
||||
in
|
||||
{
|
||||
|
||||
meta.maintainers = with lib.maintainers; [ julienmalka ];
|
||||
|
||||
imports =
|
||||
[ (mkRenamedOptionModule [ "boot" "loader" "gummiboot" "enable" ] [ "boot" "loader" "systemd-boot" "enable" ])
|
||||
(lib.mkChangedOptionModule
|
||||
[ "boot" "loader" "systemd-boot" "memtest86" "entryFilename" ]
|
||||
[ "boot" "loader" "systemd-boot" "memtest86" "sortKey" ]
|
||||
(config: lib.strings.removeSuffix ".conf" config.boot.loader.systemd-boot.memtest86.entryFilename)
|
||||
)
|
||||
(lib.mkChangedOptionModule
|
||||
[ "boot" "loader" "systemd-boot" "netbootxyz" "entryFilename" ]
|
||||
[ "boot" "loader" "systemd-boot" "netbootxyz" "sortKey" ]
|
||||
(config: lib.strings.removeSuffix ".conf" config.boot.loader.systemd-boot.netbootxyz.entryFilename)
|
||||
)
|
||||
];
|
||||
imports = [
|
||||
(mkRenamedOptionModule
|
||||
[
|
||||
"boot"
|
||||
"loader"
|
||||
"gummiboot"
|
||||
"enable"
|
||||
]
|
||||
[
|
||||
"boot"
|
||||
"loader"
|
||||
"systemd-boot"
|
||||
"enable"
|
||||
]
|
||||
)
|
||||
(lib.mkChangedOptionModule
|
||||
[
|
||||
"boot"
|
||||
"loader"
|
||||
"systemd-boot"
|
||||
"memtest86"
|
||||
"entryFilename"
|
||||
]
|
||||
[
|
||||
"boot"
|
||||
"loader"
|
||||
"systemd-boot"
|
||||
"memtest86"
|
||||
"sortKey"
|
||||
]
|
||||
(config: lib.strings.removeSuffix ".conf" config.boot.loader.systemd-boot.memtest86.entryFilename)
|
||||
)
|
||||
(lib.mkChangedOptionModule
|
||||
[
|
||||
"boot"
|
||||
"loader"
|
||||
"systemd-boot"
|
||||
"netbootxyz"
|
||||
"entryFilename"
|
||||
]
|
||||
[
|
||||
"boot"
|
||||
"loader"
|
||||
"systemd-boot"
|
||||
"netbootxyz"
|
||||
"sortKey"
|
||||
]
|
||||
(config: lib.strings.removeSuffix ".conf" config.boot.loader.systemd-boot.netbootxyz.entryFilename)
|
||||
)
|
||||
];
|
||||
|
||||
options.boot.loader.systemd-boot = {
|
||||
enable = mkOption {
|
||||
|
@ -124,7 +182,7 @@ in {
|
|||
|
||||
sortKey = mkOption {
|
||||
default = "nixos";
|
||||
type = lib.types.str;
|
||||
type = types.str;
|
||||
description = ''
|
||||
The sort key used for the NixOS bootloader entries.
|
||||
This key determines sorting relative to non-NixOS entries.
|
||||
|
@ -218,7 +276,15 @@ in {
|
|||
consoleMode = mkOption {
|
||||
default = "keep";
|
||||
|
||||
type = types.enum [ "0" "1" "2" "5" "auto" "max" "keep" ];
|
||||
type = types.enum [
|
||||
"0"
|
||||
"1"
|
||||
"2"
|
||||
"5"
|
||||
"auto"
|
||||
"max"
|
||||
"keep"
|
||||
];
|
||||
|
||||
description = ''
|
||||
The resolution of the console. The following values are valid:
|
||||
|
@ -281,9 +347,32 @@ in {
|
|||
};
|
||||
};
|
||||
|
||||
edk2-uefi-shell = {
|
||||
enable = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
description = ''
|
||||
Make the EDK2 UEFI Shell available from the systemd-boot menu.
|
||||
It can be used to manually boot other operating systems or for debugging.
|
||||
'';
|
||||
};
|
||||
|
||||
sortKey = mkOption {
|
||||
type = types.str;
|
||||
default = "o_edk2-uefi-shell";
|
||||
description = ''
|
||||
`systemd-boot` orders the menu entries by their sort keys,
|
||||
so if you want something to appear after all the NixOS entries,
|
||||
it should start with {file}`o` or onwards.
|
||||
|
||||
See also {option}`boot.loader.systemd-boot.sortKey`..
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
extraEntries = mkOption {
|
||||
type = types.attrsOf types.lines;
|
||||
default = {};
|
||||
default = { };
|
||||
example = literalExpression ''
|
||||
{ "memtest86.conf" = '''
|
||||
title Memtest86+
|
||||
|
@ -306,7 +395,7 @@ in {
|
|||
|
||||
extraFiles = mkOption {
|
||||
type = types.attrsOf types.path;
|
||||
default = {};
|
||||
default = { };
|
||||
example = literalExpression ''
|
||||
{ "efi/memtest86/memtest.efi" = "''${pkgs.memtest86plus}/memtest.efi"; }
|
||||
'';
|
||||
|
@ -349,40 +438,126 @@ in {
|
|||
Windows can unseal the encryption key.
|
||||
'';
|
||||
};
|
||||
|
||||
windows = mkOption {
|
||||
default = { };
|
||||
description = ''
|
||||
Make Windows bootable from systemd-boot. This option is not necessary when Windows and
|
||||
NixOS use the same EFI System Partition (ESP). In that case, Windows will automatically be
|
||||
detected by systemd-boot.
|
||||
|
||||
However, if Windows is installed on a separate drive or ESP, you can use this option to add
|
||||
a menu entry for each installation manually.
|
||||
|
||||
The attribute name is used for the title of the menu entry and internal file names.
|
||||
'';
|
||||
example = literalExpression ''
|
||||
{
|
||||
"10".efiDeviceHandle = "HD0c3";
|
||||
"11-ame" = {
|
||||
title = "Windows 11 Ameliorated Edition";
|
||||
efiDeviceHandle = "HD0b1";
|
||||
};
|
||||
"11-home" = {
|
||||
title = "Windows 11 Home";
|
||||
efiDeviceHandle = "FS1";
|
||||
sortKey = "z_windows";
|
||||
};
|
||||
}
|
||||
'';
|
||||
type = types.attrsOf (
|
||||
types.submodule (
|
||||
{ name, ... }:
|
||||
{
|
||||
options = {
|
||||
efiDeviceHandle = mkOption {
|
||||
type = types.str;
|
||||
example = "HD1b3";
|
||||
description = ''
|
||||
The device handle of the EFI System Partition (ESP) where the Windows bootloader is
|
||||
located. This is the device handle that the EDK2 UEFI Shell uses to load the
|
||||
bootloader.
|
||||
|
||||
To find this handle, follow these steps:
|
||||
1. Set {option}`boot.loader.systemd-boot.edk2-uefi-shell.enable` to `true`
|
||||
2. Run `nixos-rebuild boot`
|
||||
3. Reboot and select "EDK2 UEFI Shell" from the systemd-boot menu
|
||||
4. Run `map -c` to list all consistent device handles
|
||||
5. For each device handle (for example, `HD0c1`), run `ls HD0c1:\EFI`
|
||||
6. If the output contains the directory `Microsoft`, you might have found the correct device handle
|
||||
7. Run `HD0c1:\EFI\Microsoft\Boot\Bootmgfw.efi` to check if Windows boots correctly
|
||||
8. If it does, this device handle is the one you need (in this example, `HD0c1`)
|
||||
|
||||
This option is required, there is no useful default.
|
||||
'';
|
||||
};
|
||||
|
||||
title = mkOption {
|
||||
type = types.str;
|
||||
example = "Michaelsoft Binbows";
|
||||
default = "Windows ${name}";
|
||||
defaultText = ''attribute name of this entry, prefixed with "Windows "'';
|
||||
description = ''
|
||||
The title of the boot menu entry.
|
||||
'';
|
||||
};
|
||||
|
||||
sortKey = mkOption {
|
||||
type = types.str;
|
||||
default = "o_windows_${name}";
|
||||
defaultText = ''attribute name of this entry, prefixed with "o_windows_"'';
|
||||
description = ''
|
||||
`systemd-boot` orders the menu entries by their sort keys,
|
||||
so if you want something to appear after all the NixOS entries,
|
||||
it should start with {file}`o` or onwards.
|
||||
|
||||
See also {option}`boot.loader.systemd-boot.sortKey`..
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
)
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
assertions = [
|
||||
{
|
||||
assertion = (hasPrefix "/" efi.efiSysMountPoint);
|
||||
message = "The ESP mount point '${toString efi.efiSysMountPoint}' must be an absolute path";
|
||||
}
|
||||
{
|
||||
assertion = cfg.xbootldrMountPoint == null || (hasPrefix "/" cfg.xbootldrMountPoint);
|
||||
message = "The XBOOTLDR mount point '${toString cfg.xbootldrMountPoint}' must be an absolute path";
|
||||
}
|
||||
{
|
||||
assertion = cfg.xbootldrMountPoint != efi.efiSysMountPoint;
|
||||
message = "The XBOOTLDR mount point '${toString cfg.xbootldrMountPoint}' cannot be the same as the ESP mount point '${toString efi.efiSysMountPoint}'";
|
||||
}
|
||||
{
|
||||
assertion = (config.boot.kernelPackages.kernel.features or { efiBootStub = true; }) ? efiBootStub;
|
||||
message = "This kernel does not support the EFI boot stub";
|
||||
}
|
||||
{
|
||||
assertion = cfg.installDeviceTree -> config.hardware.deviceTree.enable -> config.hardware.deviceTree.name != null;
|
||||
message = "Cannot install devicetree without 'config.hardware.deviceTree.enable' enabled and 'config.hardware.deviceTree.name' set";
|
||||
}
|
||||
] ++ concatMap (filename: [
|
||||
{
|
||||
assertion = !(hasInfix "/" filename);
|
||||
message = "boot.loader.systemd-boot.extraEntries.${lib.strings.escapeNixIdentifier filename} is invalid: entries within folders are not supported";
|
||||
}
|
||||
{
|
||||
assertion = hasSuffix ".conf" filename;
|
||||
message = "boot.loader.systemd-boot.extraEntries.${lib.strings.escapeNixIdentifier filename} is invalid: entries must have a .conf file extension";
|
||||
}
|
||||
]) (builtins.attrNames cfg.extraEntries)
|
||||
assertions =
|
||||
[
|
||||
{
|
||||
assertion = (hasPrefix "/" efi.efiSysMountPoint);
|
||||
message = "The ESP mount point '${toString efi.efiSysMountPoint}' must be an absolute path";
|
||||
}
|
||||
{
|
||||
assertion = cfg.xbootldrMountPoint == null || (hasPrefix "/" cfg.xbootldrMountPoint);
|
||||
message = "The XBOOTLDR mount point '${toString cfg.xbootldrMountPoint}' must be an absolute path";
|
||||
}
|
||||
{
|
||||
assertion = cfg.xbootldrMountPoint != efi.efiSysMountPoint;
|
||||
message = "The XBOOTLDR mount point '${toString cfg.xbootldrMountPoint}' cannot be the same as the ESP mount point '${toString efi.efiSysMountPoint}'";
|
||||
}
|
||||
{
|
||||
assertion = (config.boot.kernelPackages.kernel.features or { efiBootStub = true; }) ? efiBootStub;
|
||||
message = "This kernel does not support the EFI boot stub";
|
||||
}
|
||||
{
|
||||
assertion =
|
||||
cfg.installDeviceTree
|
||||
-> config.hardware.deviceTree.enable
|
||||
-> config.hardware.deviceTree.name != null;
|
||||
message = "Cannot install devicetree without 'config.hardware.deviceTree.enable' enabled and 'config.hardware.deviceTree.name' set";
|
||||
}
|
||||
]
|
||||
++ concatMap (filename: [
|
||||
{
|
||||
assertion = !(hasInfix "/" filename);
|
||||
message = "boot.loader.systemd-boot.extraEntries.${lib.strings.escapeNixIdentifier filename} is invalid: entries within folders are not supported";
|
||||
}
|
||||
{
|
||||
assertion = hasSuffix ".conf" filename;
|
||||
message = "boot.loader.systemd-boot.extraEntries.${lib.strings.escapeNixIdentifier filename} is invalid: entries must have a .conf file extension";
|
||||
}
|
||||
]) (builtins.attrNames cfg.extraEntries)
|
||||
++ concatMap (filename: [
|
||||
{
|
||||
assertion = !(hasPrefix "/" filename);
|
||||
|
@ -396,7 +571,13 @@ in {
|
|||
assertion = !(hasInfix "nixos/.extra-files" (toLower filename));
|
||||
message = "boot.loader.systemd-boot.extraFiles.${lib.strings.escapeNixIdentifier filename} is invalid: files cannot be placed in the nixos/.extra-files directory";
|
||||
}
|
||||
]) (builtins.attrNames cfg.extraFiles);
|
||||
]) (builtins.attrNames cfg.extraFiles)
|
||||
++ concatMap (winVersion: [
|
||||
{
|
||||
assertion = lib.match "^[-_0-9A-Za-z]+$" winVersion != null;
|
||||
message = "boot.loader.systemd-boot.windows.${winVersion} is invalid: key must only contain alphanumeric characters, hyphens, and underscores";
|
||||
}
|
||||
]) (builtins.attrNames cfg.windows);
|
||||
|
||||
boot.loader.grub.enable = mkDefault false;
|
||||
|
||||
|
@ -409,24 +590,44 @@ in {
|
|||
(mkIf cfg.netbootxyz.enable {
|
||||
"efi/netbootxyz/netboot.xyz.efi" = "${pkgs.netbootxyz-efi}";
|
||||
})
|
||||
(mkIf (cfg.edk2-uefi-shell.enable || cfg.windows != { }) {
|
||||
${edk2ShellEspPath} = "${pkgs.edk2-uefi-shell}/shell.efi";
|
||||
})
|
||||
];
|
||||
|
||||
boot.loader.systemd-boot.extraEntries = mkMerge [
|
||||
(mkIf cfg.memtest86.enable {
|
||||
"memtest86.conf" = ''
|
||||
title Memtest86+
|
||||
efi /efi/memtest86/memtest.efi
|
||||
sort-key ${cfg.memtest86.sortKey}
|
||||
boot.loader.systemd-boot.extraEntries = mkMerge (
|
||||
[
|
||||
(mkIf cfg.memtest86.enable {
|
||||
"memtest86.conf" = ''
|
||||
title Memtest86+
|
||||
efi /efi/memtest86/memtest.efi
|
||||
sort-key ${cfg.memtest86.sortKey}
|
||||
'';
|
||||
})
|
||||
(mkIf cfg.netbootxyz.enable {
|
||||
"netbootxyz.conf" = ''
|
||||
title netboot.xyz
|
||||
efi /efi/netbootxyz/netboot.xyz.efi
|
||||
sort-key ${cfg.netbootxyz.sortKey}
|
||||
'';
|
||||
})
|
||||
(mkIf cfg.edk2-uefi-shell.enable {
|
||||
"edk2-uefi-shell.conf" = ''
|
||||
title EDK2 UEFI Shell
|
||||
efi /${edk2ShellEspPath}
|
||||
sort-key ${cfg.edk2-uefi-shell.sortKey}
|
||||
'';
|
||||
})
|
||||
]
|
||||
++ (mapAttrsToList (winVersion: cfg: {
|
||||
"windows_${winVersion}.conf" = ''
|
||||
title ${cfg.title}
|
||||
efi /${edk2ShellEspPath}
|
||||
options -nointerrupt -nomap -noversion ${cfg.efiDeviceHandle}:EFI\Microsoft\Boot\Bootmgfw.efi
|
||||
sort-key ${cfg.sortKey}
|
||||
'';
|
||||
})
|
||||
(mkIf cfg.netbootxyz.enable {
|
||||
"netbootxyz.conf" = ''
|
||||
title netboot.xyz
|
||||
efi /efi/netbootxyz/netboot.xyz.efi
|
||||
sort-key ${cfg.netbootxyz.sortKey}
|
||||
'';
|
||||
})
|
||||
];
|
||||
}) cfg.windows)
|
||||
);
|
||||
|
||||
boot.bootspec.extensions."org.nixos.systemd-boot" = {
|
||||
inherit (config.boot.loader.systemd-boot) sortKey;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue