2014-04-14 16:26:48 +02:00
|
|
|
|
{ config, lib, pkgs, utils, ... }:
|
2009-10-12 19:27:57 +02:00
|
|
|
|
|
2012-10-12 23:01:49 +02:00
|
|
|
|
with utils;
|
2014-05-05 20:58:51 +02:00
|
|
|
|
with lib;
|
2009-03-06 13:27:35 +01:00
|
|
|
|
|
2015-10-18 20:20:46 +02:00
|
|
|
|
let
|
|
|
|
|
|
2017-05-28 14:46:18 +02:00
|
|
|
|
randomEncryptionCoerce = enable: { inherit enable; };
|
|
|
|
|
|
|
|
|
|
randomEncryptionOpts = { ... }: {
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2017-05-28 14:46:18 +02:00
|
|
|
|
Encrypt swap device with a random key. This way you won't have a persistent swap device.
|
|
|
|
|
|
|
|
|
|
WARNING: Don't try to hibernate when you have at least one swap partition with
|
|
|
|
|
this option enabled! We have no way to set the partition into which hibernation image
|
|
|
|
|
is saved, so if your image ends up on an encrypted one you would lose it!
|
|
|
|
|
|
|
|
|
|
WARNING #2: Do not use /dev/disk/by-uuid/… or /dev/disk/by-label/… as your swap device
|
|
|
|
|
when using randomEncryption as the UUIDs and labels will get erased on every boot when
|
|
|
|
|
the partition is encrypted. Best to use /dev/disk/by-partuuid/…
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cipher = mkOption {
|
|
|
|
|
default = "aes-xts-plain64";
|
|
|
|
|
example = "serpent-xts-plain64";
|
|
|
|
|
type = types.str;
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2017-05-28 14:46:18 +02:00
|
|
|
|
Use specified cipher for randomEncryption.
|
|
|
|
|
|
|
|
|
|
Hint: Run "cryptsetup benchmark" to see which one is fastest on your machine.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
source = mkOption {
|
|
|
|
|
default = "/dev/urandom";
|
|
|
|
|
example = "/dev/random";
|
|
|
|
|
type = types.str;
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2017-05-28 14:46:18 +02:00
|
|
|
|
Define the source of randomness to obtain a random key for encryption.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-23 22:06:36 +02:00
|
|
|
|
allowDiscards = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2021-07-23 22:06:36 +02:00
|
|
|
|
Whether to allow TRIM requests to the underlying device. This option
|
|
|
|
|
has security implications; please read the LUKS documentation before
|
|
|
|
|
activating it.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2017-05-28 14:46:18 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-18 20:20:46 +02:00
|
|
|
|
swapCfg = {config, options, ...}: {
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
device = mkOption {
|
|
|
|
|
example = "/dev/sda3";
|
|
|
|
|
type = types.str;
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc "Path of the device or swap file.";
|
2015-10-18 20:20:46 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
label = mkOption {
|
|
|
|
|
example = "swap";
|
|
|
|
|
type = types.str;
|
|
|
|
|
description = ''
|
|
|
|
|
Label of the device. Can be used instead of <varname>device</varname>.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
size = mkOption {
|
|
|
|
|
default = null;
|
|
|
|
|
example = 2048;
|
|
|
|
|
type = types.nullOr types.int;
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2015-10-18 20:20:46 +02:00
|
|
|
|
If this option is set, ‘device’ is interpreted as the
|
|
|
|
|
path of a swapfile that will be created automatically
|
2016-07-06 13:41:40 +02:00
|
|
|
|
with the indicated size (in megabytes).
|
2015-10-18 20:20:46 +02:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
priority = mkOption {
|
|
|
|
|
default = null;
|
|
|
|
|
example = 2048;
|
|
|
|
|
type = types.nullOr types.int;
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2015-10-18 20:20:46 +02:00
|
|
|
|
Specify the priority of the swap device. Priority is a value between 0 and 32767.
|
|
|
|
|
Higher numbers indicate higher priority.
|
|
|
|
|
null lets the kernel choose a priority, which will show up as a negative value.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-28 14:46:18 +02:00
|
|
|
|
randomEncryption = mkOption {
|
2015-10-18 20:20:46 +02:00
|
|
|
|
default = false;
|
2017-05-28 14:46:18 +02:00
|
|
|
|
example = {
|
|
|
|
|
enable = true;
|
|
|
|
|
cipher = "serpent-xts-plain64";
|
|
|
|
|
source = "/dev/random";
|
|
|
|
|
};
|
|
|
|
|
type = types.coercedTo types.bool randomEncryptionCoerce (types.submodule randomEncryptionOpts);
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2015-10-18 20:20:46 +02:00
|
|
|
|
Encrypt swap device with a random key. This way you won't have a persistent swap device.
|
|
|
|
|
|
2017-05-28 14:46:18 +02:00
|
|
|
|
HINT: run "cryptsetup benchmark" to test cipher performance on your machine.
|
|
|
|
|
|
2015-10-18 20:20:46 +02:00
|
|
|
|
WARNING: Don't try to hibernate when you have at least one swap partition with
|
|
|
|
|
this option enabled! We have no way to set the partition into which hibernation image
|
|
|
|
|
is saved, so if your image ends up on an encrypted one you would lose it!
|
2016-08-31 15:34:08 +02:00
|
|
|
|
|
|
|
|
|
WARNING #2: Do not use /dev/disk/by-uuid/… or /dev/disk/by-label/… as your swap device
|
|
|
|
|
when using randomEncryption as the UUIDs and labels will get erased on every boot when
|
|
|
|
|
the partition is encrypted. Best to use /dev/disk/by-partuuid/…
|
2015-10-18 20:20:46 +02:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-27 16:09:42 +01:00
|
|
|
|
discardPolicy = mkOption {
|
|
|
|
|
default = null;
|
|
|
|
|
example = "once";
|
|
|
|
|
type = types.nullOr (types.enum ["once" "pages" "both" ]);
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2020-12-27 16:09:42 +01:00
|
|
|
|
Specify the discard policy for the swap device. If "once", then the
|
|
|
|
|
whole swap space is discarded at swapon invocation. If "pages",
|
|
|
|
|
asynchronous discard on freed pages is performed, before returning to
|
|
|
|
|
the available pages pool. With "both", both policies are activated.
|
|
|
|
|
See swapon(8) for more information.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-24 19:58:18 +02:00
|
|
|
|
options = mkOption {
|
|
|
|
|
default = [ "defaults" ];
|
|
|
|
|
example = [ "nofail" ];
|
|
|
|
|
type = types.listOf types.nonEmptyStr;
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2021-06-24 19:58:18 +02:00
|
|
|
|
Options used to mount the swap.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-18 20:20:46 +02:00
|
|
|
|
deviceName = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
internal = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
realDevice = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
internal = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = rec {
|
|
|
|
|
device = mkIf options.label.isDefined
|
|
|
|
|
"/dev/disk/by-label/${config.label}";
|
2016-08-31 15:34:08 +02:00
|
|
|
|
deviceName = lib.replaceChars ["\\"] [""] (escapeSystemdPath config.device);
|
2017-05-22 20:07:04 +02:00
|
|
|
|
realDevice = if config.randomEncryption.enable then "/dev/mapper/${deviceName}" else config.device;
|
2015-10-18 20:20:46 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-05-28 01:14:38 +02:00
|
|
|
|
options = {
|
|
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
|
swapDevices = mkOption {
|
2009-05-28 01:14:38 +02:00
|
|
|
|
default = [];
|
|
|
|
|
example = [
|
|
|
|
|
{ device = "/dev/hda7"; }
|
|
|
|
|
{ device = "/var/swapfile"; }
|
|
|
|
|
{ label = "bigswap"; }
|
|
|
|
|
];
|
2022-07-28 23:19:15 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2009-05-28 01:14:38 +02:00
|
|
|
|
The swap devices and swap files. These must have been
|
2022-07-28 23:19:15 +02:00
|
|
|
|
initialised using {command}`mkswap`. Each element
|
2009-05-28 01:14:38 +02:00
|
|
|
|
should be an attribute set specifying either the path of the
|
2022-07-28 23:19:15 +02:00
|
|
|
|
swap device or file (`device`) or the label
|
|
|
|
|
of the swap device (`label`, see
|
|
|
|
|
{command}`mkswap -L`). Using a label is
|
2009-05-28 01:14:38 +02:00
|
|
|
|
recommended.
|
2009-07-16 16:51:49 +02:00
|
|
|
|
'';
|
|
|
|
|
|
2015-10-18 20:20:46 +02:00
|
|
|
|
type = types.listOf (types.submodule swapCfg);
|
2009-05-28 01:14:38 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
2009-07-16 16:51:49 +02:00
|
|
|
|
|
2012-08-07 23:34:10 +02:00
|
|
|
|
config = mkIf ((length config.swapDevices) != 0) {
|
2012-10-12 22:47:11 +02:00
|
|
|
|
|
2012-08-07 23:34:10 +02:00
|
|
|
|
system.requiredKernelConfig = with config.lib.kernelConfig; [
|
|
|
|
|
(isYes "SWAP")
|
|
|
|
|
];
|
2012-10-12 22:47:11 +02:00
|
|
|
|
|
|
|
|
|
# Create missing swapfiles.
|
2013-01-16 12:33:18 +01:00
|
|
|
|
systemd.services =
|
2012-10-12 22:47:11 +02:00
|
|
|
|
let
|
|
|
|
|
|
2012-10-12 23:01:49 +02:00
|
|
|
|
createSwapDevice = sw:
|
|
|
|
|
assert sw.device != "";
|
2017-05-22 20:07:04 +02:00
|
|
|
|
assert !(sw.randomEncryption.enable && lib.hasPrefix "/dev/disk/by-uuid" sw.device);
|
|
|
|
|
assert !(sw.randomEncryption.enable && lib.hasPrefix "/dev/disk/by-label" sw.device);
|
2015-10-18 20:20:46 +02:00
|
|
|
|
let realDevice' = escapeSystemdPath sw.realDevice;
|
|
|
|
|
in nameValuePair "mkswap-${sw.deviceName}"
|
|
|
|
|
{ description = "Initialisation of swap device ${sw.device}";
|
|
|
|
|
wantedBy = [ "${realDevice'}.swap" ];
|
|
|
|
|
before = [ "${realDevice'}.swap" ];
|
2020-11-24 16:29:28 +01:00
|
|
|
|
path = [ pkgs.util-linux ] ++ optional sw.randomEncryption.enable pkgs.cryptsetup;
|
2016-01-12 15:27:21 +01:00
|
|
|
|
|
2012-10-12 22:47:11 +02:00
|
|
|
|
script =
|
|
|
|
|
''
|
2015-10-18 20:20:46 +02:00
|
|
|
|
${optionalString (sw.size != null) ''
|
2016-07-06 13:41:40 +02:00
|
|
|
|
currentSize=$(( $(stat -c "%s" "${sw.device}" 2>/dev/null || echo 0) / 1024 / 1024 ))
|
|
|
|
|
if [ "${toString sw.size}" != "$currentSize" ]; then
|
2021-11-13 16:04:42 +01:00
|
|
|
|
dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size}
|
2015-10-18 20:20:46 +02:00
|
|
|
|
chmod 0600 ${sw.device}
|
2017-05-22 20:07:04 +02:00
|
|
|
|
${optionalString (!sw.randomEncryption.enable) "mkswap ${sw.realDevice}"}
|
2015-10-18 20:20:46 +02:00
|
|
|
|
fi
|
|
|
|
|
''}
|
2017-05-22 20:07:04 +02:00
|
|
|
|
${optionalString sw.randomEncryption.enable ''
|
2021-07-23 22:06:36 +02:00
|
|
|
|
cryptsetup plainOpen -c ${sw.randomEncryption.cipher} -d ${sw.randomEncryption.source} \
|
|
|
|
|
${optionalString sw.randomEncryption.allowDiscards "--allow-discards"} ${sw.device} ${sw.deviceName}
|
2015-10-18 20:20:46 +02:00
|
|
|
|
mkswap ${sw.realDevice}
|
|
|
|
|
''}
|
2012-10-12 22:47:11 +02:00
|
|
|
|
'';
|
2016-01-12 15:27:21 +01:00
|
|
|
|
|
2012-10-12 23:32:36 +02:00
|
|
|
|
unitConfig.RequiresMountsFor = [ "${dirOf sw.device}" ];
|
2012-10-12 22:47:11 +02:00
|
|
|
|
unitConfig.DefaultDependencies = false; # needed to prevent a cycle
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
2017-05-22 20:07:04 +02:00
|
|
|
|
serviceConfig.RemainAfterExit = sw.randomEncryption.enable;
|
|
|
|
|
serviceConfig.ExecStop = optionalString sw.randomEncryption.enable "${pkgs.cryptsetup}/bin/cryptsetup luksClose ${sw.deviceName}";
|
2016-01-12 15:27:21 +01:00
|
|
|
|
restartIfChanged = false;
|
2012-10-12 22:47:11 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-22 20:07:04 +02:00
|
|
|
|
in listToAttrs (map createSwapDevice (filter (sw: sw.size != null || sw.randomEncryption.enable) config.swapDevices));
|
2012-10-12 22:47:11 +02:00
|
|
|
|
|
2012-08-07 23:34:10 +02:00
|
|
|
|
};
|
|
|
|
|
|
2006-12-21 02:07:23 +01:00
|
|
|
|
}
|