nixos/modules/system/grub.nix

85 lines
2.8 KiB
Nix
Raw Normal View History

2023-09-02 14:54:37 +08:00
inputs:
{
options.nixos.system.grub = let inherit (inputs.lib) mkOption types; in
{
timeout = mkOption { type = types.int; default = 5; };
windowsEntries = mkOption { type = types.attrsOf types.nonEmptyStr; default = {}; };
# "efi" using efi, "efiRemovable" using efi with install grub removable, or dev path like "/dev/sda" using bios
installDevice = mkOption { type = types.str; };
};
2024-02-01 12:30:01 +08:00
config = let inherit (inputs.config.nixos.system) grub; in inputs.lib.mkMerge
[
# general settings
{ boot.loader.grub = { enable = true; useOSProber = false; }; }
# grub timeout
{ boot.loader.timeout = grub.timeout; }
# grub install
2023-09-02 14:54:37 +08:00
{
2024-02-01 12:30:01 +08:00
boot.loader =
2023-09-02 14:54:37 +08:00
{
2024-02-01 12:30:01 +08:00
grub =
{
device = if builtins.elem grub.installDevice [ "efi" "efiRemovable" ] then "nodev" else grub.installDevice;
efiSupport = builtins.elem grub.installDevice [ "efi" "efiRemovable" ];
efiInstallAsRemovable = grub.installDevice == "efiRemovable";
};
efi =
{
canTouchEfiVariables = grub.installDevice == "efi";
efiSysMountPoint = inputs.lib.mkIf (builtins.elem grub.installDevice [ "efi" "efiRemovable" ]) "/boot/efi";
};
2023-09-02 14:54:37 +08:00
};
2024-02-01 12:30:01 +08:00
}
# extra grub entries
{
boot.loader.grub =
2023-09-02 14:54:37 +08:00
{
2024-03-25 17:49:35 +08:00
memtest86.enable = inputs.lib.mkIf (inputs.config.nixos.system.nixpkgs.arch == "x86_64") true;
2024-02-01 12:38:52 +08:00
extraFiles = inputs.lib.mkIf (builtins.elem grub.installDevice [ "efi" "efiRemovable" ])
{ "shell.efi" = "${inputs.pkgs.edk2-uefi-shell}/shell.efi"; };
extraEntries = inputs.lib.mkMerge (builtins.concatLists
[
2024-02-01 12:30:01 +08:00
(builtins.map
(system:
''
menuentry "${system.value}" {
insmod part_gpt
insmod fat
insmod search_fs_uuid
insmod chain
search --fs-uuid --set=root ${system.name}
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
}
'')
(inputs.localLib.attrsToList grub.windowsEntries))
2024-02-01 12:38:52 +08:00
[
2024-02-01 12:30:01 +08:00
''
menuentry "System shutdown" {
echo "System shutting down..."
halt
}
menuentry "System restart" {
echo "System rebooting..."
reboot
}
''
(
inputs.lib.optionalString (builtins.elem grub.installDevice [ "efi" "efiRemovable" ])
''
menuentry 'UEFI Firmware Settings' --id 'uefi-firmware' {
fwsetup
}
2024-02-01 12:38:52 +08:00
menuentry "UEFI Shell" {
insmod fat
insmod chain
2024-08-17 11:53:01 +08:00
chainloader /shell.efi
2024-02-01 12:38:52 +08:00
}
2024-02-01 12:30:01 +08:00
''
)
]
2024-02-01 12:38:52 +08:00
]);
2023-09-02 14:54:37 +08:00
};
2024-02-01 12:30:01 +08:00
}
];
2023-09-02 14:54:37 +08:00
}