disko/lib/types/luks.nix

184 lines
6.7 KiB
Nix
Raw Normal View History

{ config, options, lib, diskoLib, parent, device, ... }:
let
2023-08-11 09:11:01 +03:00
keyFile =
2023-09-20 23:16:38 +03:00
if config.settings ? "keyFile"
then config.settings.keyFile
2023-09-20 23:16:38 +03:00
else if config.askPassword
then ''<(set +x; echo -n "$password"; set -x)''
else if config.passwordFile != null
2023-09-25 17:51:25 +03:00
# do not print the password to the console
then ''<(set +x; echo -n "$(cat ${config.passwordFile})"; set -x)''
else if config.keyFile != null
2023-09-02 19:08:33 +03:00
then
lib.warn
("The option `keyFile` is deprecated."
+ "Use passwordFile instead if you want to use interactive login or settings.keyFile if you want to use key file login")
config.keyFile
else null;
keyFileArgs = ''
${lib.optionalString (keyFile != null) "--key-file ${keyFile}"} \
${lib.optionalString (lib.hasAttr "keyFileSize" config.settings) "--keyfile-size ${builtins.toString config.settings.keyFileSize}"} \
${lib.optionalString (lib.hasAttr "keyFileOffset" config.settings) "--keyfile-offset ${builtins.toString config.settings.keyFileOffset}"} \
'';
cryptsetupOpen = ''
cryptsetup open ${config.device} ${config.name} \
${lib.optionalString (config.settings.allowDiscards or false) "--allow-discards"} \
${lib.optionalString (config.settings.bypassWorkqueues or false) "--perf-no_read_workqueue --perf-no_write_workqueue"} \
${toString config.extraOpenArgs} \
${keyFileArgs} \
'';
in
2023-01-28 18:19:13 +03:00
{
options = {
type = lib.mkOption {
type = lib.types.enum [ "luks" ];
internal = true;
description = "Type";
};
device = lib.mkOption {
type = lib.types.str;
description = "Device to encrypt";
default = device;
};
2023-01-28 18:19:13 +03:00
name = lib.mkOption {
type = lib.types.str;
description = "Name of the LUKS";
};
keyFile = lib.mkOption {
2023-05-16 14:40:03 +03:00
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
2023-01-28 18:19:13 +03:00
default = null;
description = "DEPRECATED use passwordFile or settings.keyFile. Path to the key for encryption";
example = "/tmp/disk.key";
};
passwordFile = lib.mkOption {
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
default = null;
description = "Path to the file which contains the password for initial encryption";
2023-04-19 00:19:53 +03:00
example = "/tmp/disk.key";
2023-01-28 18:19:13 +03:00
};
2023-09-20 23:16:38 +03:00
askPassword = lib.mkOption {
type = lib.types.bool;
default = config.keyFile == null && config.passwordFile == null && (! config.settings ? "keyFile");
description = "Whether to ask for a password for initial encryption";
};
settings = lib.mkOption {
default = { };
description = "LUKS settings (as defined in configuration.nix in boot.initrd.luks.devices.<name>)";
example = ''{
keyFile = "/tmp/disk.key";
keyFileSize = 2048;
keyFileOffset = 1024;
fallbackToPassword = true;
2023-11-04 14:54:07 +03:00
allowDiscards = true;
};
'';
};
2023-07-06 21:33:44 +03:00
additionalKeyFiles = lib.mkOption {
type = lib.types.listOf diskoLib.optionTypes.absolute-pathname;
2023-08-11 09:11:01 +03:00
default = [ ];
2023-07-06 21:33:44 +03:00
description = "Path to additional key files for encryption";
2023-08-11 09:11:01 +03:00
example = [ "/tmp/disk2.key" ];
2023-07-06 21:33:44 +03:00
};
initrdUnlock = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to add a boot.initrd.luks.devices entry for the specified disk.";
};
extraFormatArgs = lib.mkOption {
2023-01-28 18:19:13 +03:00
type = lib.types.listOf lib.types.str;
default = [ ];
description = "Extra arguments to pass to `cryptsetup luksFormat` when formatting";
2023-04-19 00:19:53 +03:00
example = [ "--pbkdf argon2id" ];
};
extraOpenArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "Extra arguments to pass to `cryptsetup luksOpen` when opening";
2023-11-04 14:54:07 +03:00
example = [ "--timeout 10" ];
2023-01-28 18:19:13 +03:00
};
content = diskoLib.deviceType { parent = config; device = "/dev/mapper/${config.name}"; };
_parent = lib.mkOption {
internal = true;
default = parent;
};
2023-01-28 18:19:13 +03:00
_meta = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo diskoLib.jsonType;
default = dev:
lib.optionalAttrs (config.content != null) (config.content._meta dev);
2023-01-28 18:19:13 +03:00
description = "Metadata";
};
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
2023-09-20 23:16:38 +03:00
${lib.optionalString config.askPassword ''
set +x
askPassword() {
echo "Enter password for ${config.device}: "
IFS= read -r -s password
2023-09-20 23:16:38 +03:00
echo "Enter password for ${config.device} again to be safe: "
IFS= read -r -s password_check
2023-09-20 23:16:38 +03:00
export password
[ "$password" = "$password_check" ]
}
until askPassword; do
echo "Passwords did not match, please try again."
done
set -x
''}
cryptsetup -q luksFormat ${config.device} ${toString config.extraFormatArgs} ${keyFileArgs}
${cryptsetupOpen} --persistent
${toString (lib.forEach config.additionalKeyFiles (keyFile: ''
cryptsetup luksAddKey ${config.device} ${keyFile} ${keyFileArgs}
''))}
${lib.optionalString (config.content != null) config.content._create}
2023-01-28 18:19:13 +03:00
'';
};
_mount = diskoLib.mkMountOption {
inherit config options;
default =
2023-01-28 18:19:13 +03:00
let
contentMount = config.content._mount;
2023-01-28 18:19:13 +03:00
in
{
dev = ''
2023-09-20 23:16:38 +03:00
if ! cryptsetup status ${config.name} >/dev/null 2>/dev/null; then
${lib.optionalString config.askPassword ''
set +x
echo "Enter password for ${config.device}"
IFS= read -r -s password
export password
set -x
''}
${cryptsetupOpen}
2023-09-20 23:16:38 +03:00
fi
${lib.optionalString (config.content != null) contentMount.dev or ""}
2023-01-28 18:19:13 +03:00
'';
fs = lib.optionalAttrs (config.content != null) contentMount.fs or { };
2023-01-28 18:19:13 +03:00
};
};
_config = lib.mkOption {
internal = true;
readOnly = true;
default = [ ]
# If initrdUnlock is true, then add a device entry to the initrd.luks.devices config.
++ (lib.optional config.initrdUnlock [
2023-08-11 09:11:01 +03:00
{
boot.initrd.luks.devices.${config.name} = {
inherit (config) device;
} // config.settings;
}
]) ++ (lib.optional (config.content != null) config.content._config);
2023-01-28 18:19:13 +03:00
description = "NixOS configuration";
};
_pkgs = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.cryptsetup ] ++ (lib.optionals (config.content != null) (config.content._pkgs pkgs));
2023-01-28 18:19:13 +03:00
description = "Packages";
};
};
}