nixos/modules/boot/default.nix
2023-08-22 16:53:33 +08:00

85 lines
2.3 KiB
Nix

inputs:
{
options.nixos.boot = let inherit (inputs.lib) mkOption types; in
{
grub =
{
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; };
};
network.enable = mkOption { type = types.bool; default = false; };
sshd =
{
enable = mkOption { type = types.bool; default = false; };
hostKeys = mkOption { type = types.listOf types.nonEmptyStr; default = []; };
};
};
config =
let
inherit (inputs.lib) mkMerge mkIf;
inherit (inputs.localLib) mkConditional attrsToList stripeTabs;
inherit (inputs.config.nixos) boot;
inherit (builtins) concatStringsSep map;
in mkMerge
[
# generic
{
boot =
{
loader.grub = { enable = true; useOSProber = false; };
initrd.systemd.enable = true;
};
}
# grub.timeout
{ boot.loader.timeout = boot.grub.timeout; }
# grub.windowsEntries
{
boot.loader.grub.extraEntries = concatStringsSep "" (map (system: stripeTabs
''
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
}
'') (attrsToList boot.grub.windowsEntries));
}
# grub.installDevice
(
mkConditional (boot.grub.installDevice == "efi" || boot.grub.installDevice == "efiRemovable")
(
mkConditional (boot.grub.installDevice == "efi")
{
boot.loader =
{
efi = { canTouchEfiVariables = true; efiSysMountPoint = "/boot/efi"; };
grub = { device = "nodev"; efiSupport = true; };
};
}
{
boot.loader =
{
efi.efiSysMountPoint = "/boot/efi";
grub = { device = "nodev"; efiSupport = true; efiInstallAsRemovable = true; };
};
}
)
{ boot.loader.grub.device = boot.grub.installDevice; }
)
# network
(
mkIf boot.network.enable
{ boot = { initrd.network.enable = true; kernelParams = [ "ip=dhcp" ]; }; }
)
# sshd
(
mkIf boot.sshd.enable
{ boot.initrd.network.ssh = { enable = true; hostKeys = boot.sshd.hostKeys; };}
)
];
}