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