2014-05-05 18:58:51 +00:00
|
|
|
{ config, lib, ... }:
|
2014-04-26 21:26:23 +00:00
|
|
|
|
2014-05-05 18:58:51 +00:00
|
|
|
with lib;
|
2014-04-26 21:26:23 +00:00
|
|
|
|
|
|
|
let
|
2015-11-25 19:09:09 +00:00
|
|
|
fileSystems = config.system.build.fileSystems ++ config.swapDevices;
|
2014-04-26 21:26:23 +00:00
|
|
|
encDevs = filter (dev: dev.encrypted.enable) fileSystems;
|
|
|
|
keyedEncDevs = filter (dev: dev.encrypted.keyFile != null) encDevs;
|
2015-09-21 19:02:27 +00:00
|
|
|
keylessEncDevs = filter (dev: dev.encrypted.keyFile == null) encDevs;
|
2014-04-26 21:26:23 +00:00
|
|
|
isIn = needle: haystack: filter (p: p == needle) haystack != [];
|
|
|
|
anyEncrypted =
|
|
|
|
fold (j: v: v || j.encrypted.enable) false encDevs;
|
|
|
|
|
|
|
|
encryptedFSOptions = {
|
|
|
|
|
|
|
|
encrypted = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2014-06-24 19:23:14 +00:00
|
|
|
description = "The block device is backed by an encrypted one, adds this device as a initrd luks entry.";
|
2014-04-26 21:26:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
blkDev = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/dev/sda1";
|
2015-08-17 17:52:45 +00:00
|
|
|
type = types.nullOr types.str;
|
2014-06-24 19:23:14 +00:00
|
|
|
description = "Location of the backing encrypted device.";
|
2014-04-26 21:26:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
label = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "rootfs";
|
2015-11-26 14:40:31 +00:00
|
|
|
type = types.nullOr types.str;
|
2015-09-21 19:02:27 +00: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 21:26:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
keyFile = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/root/.swapkey";
|
2015-08-17 17:52:45 +00:00
|
|
|
type = types.nullOr types.str;
|
2015-09-21 19:02:27 +00:00
|
|
|
description = "File system location of keyfile. This unlocks the drive after the root has been mounted to <literal>/mnt-root</literal>.";
|
2014-04-26 21:26:23 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
fileSystems = mkOption {
|
|
|
|
options = [encryptedFSOptions];
|
|
|
|
};
|
|
|
|
swapDevices = mkOption {
|
|
|
|
options = [encryptedFSOptions];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf anyEncrypted {
|
|
|
|
boot.initrd = {
|
|
|
|
luks = {
|
|
|
|
devices =
|
2015-09-21 19:02:27 +00:00
|
|
|
map (dev: { name = dev.encrypted.label; device = dev.encrypted.blkDev; } ) keylessEncDevs;
|
2014-04-26 21:26:23 +00:00
|
|
|
cryptoModules = [ "aes" "sha256" "sha1" "xts" ];
|
|
|
|
};
|
|
|
|
postMountCommands =
|
2015-09-21 19:02:27 +00:00
|
|
|
concatMapStrings (dev: "cryptsetup luksOpen --key-file ${dev.encrypted.keyFile} ${dev.encrypted.blkDev} ${dev.encrypted.label};\n") keyedEncDevs;
|
2014-04-26 21:26:23 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|