config/modules/base/boot/default.nix

187 lines
4.9 KiB
Nix
Raw Normal View History

2023-07-22 16:05:44 +03:00
{
config,
lib,
pkgs,
...
2024-07-29 15:39:54 +03:00
}:
let
2023-07-22 16:05:44 +03:00
cfg = config.zfs-root.boot;
2024-07-29 15:39:54 +03:00
inherit (lib)
mkIf
types
mkDefault
mkOption
mkMerge
strings
;
inherit (builtins)
head
toString
map
tail
;
in
{
2023-07-22 16:05:44 +03:00
options.zfs-root.boot = {
enable = mkOption {
description = "Enable root on ZFS support";
type = types.bool;
default = false; # TODO: change by @motiejus
};
devNodes = mkOption {
description = "Specify where to discover ZFS pools";
type = types.str;
2024-07-29 15:39:54 +03:00
apply =
x:
assert (strings.hasSuffix "/" x || abort "devNodes '${x}' must have trailing slash!");
x;
2023-07-22 16:05:44 +03:00
default = "/dev/disk/by-id/";
};
bootDevices = mkOption {
description = "Specify boot devices";
type = types.nonEmptyListOf types.str;
};
availableKernelModules = mkOption {
type = types.nonEmptyListOf types.str;
2024-07-29 15:39:54 +03:00
default = [
"uas"
"nvme"
"ahci"
];
2023-07-22 16:05:44 +03:00
};
kernelParams = mkOption {
type = types.listOf types.str;
2024-07-29 15:39:54 +03:00
default = [ ];
2023-07-22 16:05:44 +03:00
};
immutable = mkOption {
description = "Enable root on ZFS immutable root support";
type = types.bool;
default = false;
};
removableEfi = mkOption {
description = "install bootloader to fallback location";
type = types.bool;
default = true;
};
partitionScheme = mkOption {
default = {
biosBoot = "-part5";
efiBoot = "-part1";
swap = "-part4";
bootPool = "-part2";
rootPool = "-part3";
};
description = "Describe on disk partitions";
type = types.attrsOf types.str;
};
sshUnlock = {
enable = mkOption {
type = types.bool;
default = false;
};
authorizedKeys = mkOption {
type = types.listOf types.str;
2024-07-29 15:39:54 +03:00
default = [ ];
2023-07-22 16:05:44 +03:00
};
};
};
config = mkIf (cfg.enable) (mkMerge [
{
zfs-root.fileSystems.datasets = {
"rpool/nixos/home" = mkDefault "/home";
"rpool/nixos/var/lib" = mkDefault "/var/lib";
"rpool/nixos/var/log" = mkDefault "/var/log";
"rpool/nixos/nix" = mkDefault "/nix";
"bpool/nixos/root" = "/boot";
};
}
(mkIf (!cfg.immutable) {
2024-07-29 15:39:54 +03:00
zfs-root.fileSystems.datasets = {
"rpool/nixos/root" = "/";
};
2023-07-22 16:05:44 +03:00
})
(mkIf cfg.immutable {
zfs-root.fileSystems = {
datasets = {
"rpool/nixos/empty" = "/";
"rpool/nixos/root" = "/oldroot";
};
bindmounts = {
"/oldroot/nix" = "/nix";
"/oldroot/etc/nixos" = "/etc/nixos";
};
};
boot.initrd.postDeviceCommands = ''
if ! grep -q zfs_no_rollback /proc/cmdline; then
zpool import -N rpool
zfs rollback -r rpool/nixos/empty@start
zpool export -a
fi
'';
})
{
zfs-root.fileSystems = {
2024-07-29 15:39:54 +03:00
efiSystemPartitions = map (diskName: diskName + cfg.partitionScheme.efiBoot) cfg.bootDevices;
2023-07-22 16:05:44 +03:00
swapPartitions =
2024-07-29 15:39:54 +03:00
if cfg.partitionScheme ? swap then
map (diskName: diskName + cfg.partitionScheme.swap) cfg.bootDevices
else
[ ];
2023-07-22 16:05:44 +03:00
};
boot = {
initrd.availableKernelModules = cfg.availableKernelModules;
kernelParams = cfg.kernelParams;
2024-07-29 15:39:54 +03:00
supportedFilesystems = [ "zfs" ];
2023-07-22 16:05:44 +03:00
zfs = {
devNodes = cfg.devNodes;
forceImportRoot = mkDefault false;
};
loader = {
efi = {
2024-07-29 15:39:54 +03:00
canTouchEfiVariables = if cfg.removableEfi then false else true;
efiSysMountPoint = "/boot/efis/" + (head cfg.bootDevices) + cfg.partitionScheme.efiBoot;
2023-07-22 16:05:44 +03:00
};
generationsDir.copyKernels = true;
grub = {
enable = true;
2023-11-28 00:49:49 +02:00
devices = map (diskName: cfg.devNodes + diskName) cfg.bootDevices;
2023-07-22 16:05:44 +03:00
efiInstallAsRemovable = cfg.removableEfi;
copyKernels = true;
efiSupport = true;
zfsSupport = true;
2024-07-29 15:39:54 +03:00
extraInstallCommands = toString (
map (diskName: ''
set -x
${pkgs.coreutils-full}/bin/cp -r ${config.boot.loader.efi.efiSysMountPoint}/EFI /boot/efis/${diskName}${cfg.partitionScheme.efiBoot}
set +x
'') (tail cfg.bootDevices)
);
2023-07-22 16:05:44 +03:00
};
};
};
}
(mkIf cfg.sshUnlock.enable {
boot.initrd = {
network = {
enable = true;
ssh = {
enable = true;
hostKeys = [
"/var/lib/ssh_unlock_zfs_ed25519_key"
"/var/lib/ssh_unlock_zfs_rsa_key"
];
authorizedKeys = cfg.sshUnlock.authorizedKeys;
};
postCommands = ''
tee -a /root/.profile >/dev/null <<EOF
if zfs load-key rpool/nixos; then
pkill zfs
fi
exit
EOF'';
2023-07-22 16:05:44 +03:00
};
};
})
]);
}