2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
fileSystems = config.system.build.fileSystems ++ config.swapDevices;
|
|
|
|
encDevs = filter (dev: dev.encrypted.enable) fileSystems;
|
|
|
|
keyedEncDevs = filter (dev: dev.encrypted.keyFile != null) encDevs;
|
|
|
|
keylessEncDevs = filter (dev: dev.encrypted.keyFile == null) encDevs;
|
|
|
|
anyEncrypted =
|
2021-08-05 21:33:18 +00:00
|
|
|
foldr (j: v: v || j.encrypted.enable) false encDevs;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
encryptedFSOptions = {
|
|
|
|
|
|
|
|
options.encrypted = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The block device is backed by an encrypted one, adds this device as a initrd luks entry.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
blkDev = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/dev/sda1";
|
|
|
|
type = types.nullOr types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Location of the backing encrypted device.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
label = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "rootfs";
|
|
|
|
type = types.nullOr types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Label of the unlocked encrypted device. Set `fileSystems.<name?>.device` to `/dev/mapper/<label>` to mount the unlocked device.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
keyFile = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/mnt-root/root/.swapkey";
|
|
|
|
type = types.nullOr types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-08-20 17:08:02 +00:00
|
|
|
Path to a keyfile used to unlock the backing encrypted
|
|
|
|
device. At the time this keyfile is accessed, the
|
2022-08-12 12:06:08 +00:00
|
|
|
`neededForBoot` filesystems (see
|
|
|
|
`fileSystems.<name?>.neededForBoot`)
|
|
|
|
will have been mounted under `/mnt-root`,
|
2020-08-20 17:08:02 +00:00
|
|
|
so the keyfile path should usually start with "/mnt-root/".
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
fileSystems = mkOption {
|
2020-09-25 04:45:31 +00:00
|
|
|
type = with lib.types; attrsOf (submodule encryptedFSOptions);
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
swapDevices = mkOption {
|
|
|
|
type = with lib.types; listOf (submodule encryptedFSOptions);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf anyEncrypted {
|
|
|
|
assertions = map (dev: {
|
|
|
|
assertion = dev.encrypted.label != null;
|
|
|
|
message = ''
|
|
|
|
The filesystem for ${dev.mountPoint} has encrypted.enable set to true, but no encrypted.label set
|
|
|
|
'';
|
|
|
|
}) encDevs;
|
|
|
|
|
|
|
|
boot.initrd = {
|
|
|
|
luks = {
|
|
|
|
devices =
|
2020-08-20 17:08:02 +00:00
|
|
|
builtins.listToAttrs (map (dev: {
|
|
|
|
name = dev.encrypted.label;
|
|
|
|
value = { device = dev.encrypted.blkDev; };
|
|
|
|
}) keylessEncDevs);
|
2020-04-24 23:36:52 +00:00
|
|
|
forceLuksSupportInInitrd = true;
|
|
|
|
};
|
|
|
|
postMountCommands =
|
2020-08-20 17:08:02 +00:00
|
|
|
concatMapStrings (dev:
|
|
|
|
"cryptsetup luksOpen --key-file ${dev.encrypted.keyFile} ${dev.encrypted.blkDev} ${dev.encrypted.label};\n"
|
|
|
|
) keyedEncDevs;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|