nixos/modules/boot/default.nix
2023-08-01 13:05:10 +08:00

82 lines
2.0 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 = {}; };
installDevice = mkOption { type = types.str; }; # "efi" using efi, or dev path like "/dev/sda" using bios
};
network.enable = mkOption { type = types.bool; default = false; };
sshd.enable = mkOption { type = types.bool; default = false; };
};
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.loader =
{
efi = { canTouchEfiVariables = true; efiSysMountPoint = "/boot/efi"; };
grub = { device = "nodev"; efiSupport = true; };
};
}
{ boot.loader.grub.device = boot.grub.installDevice; }
)
# network
(
mkIf inputs.config.nixos.boot.network.enable
{
boot =
{
initrd.network.enable = true;
kernelParams = [ "ip=dhcp" ];
};
}
)
# sshd
(
mkIf inputs.config.nixos.boot.sshd.enable
{
boot.initrd.network.ssh =
{
enable = true;
hostKeys = [ "/etc/ssh/initrd_ssh_host_ed25519_key" ];
};
}
)
];
}