From 9d88a4060002d88813e92a542a6da28939fd90d6 Mon Sep 17 00:00:00 2001 From: yottanami Date: Thu, 6 Aug 2026 12:06:05 +0200 Subject: [PATCH] swap: allow randomEncryption to take an attrset for extra config randomEncryption was typed as a plain bool, which just passed enable/allowDiscards down to NixOS's swapDevices.*.randomEncryption submodule, with no way to reach its other options (cipher, sectorSize, etc). Widen the type to bool or an attrset. A bool keeps the old behavior unchanged. An attrset enables encryption by default (overridable via its own enable key) and is merged over the enable/allowDiscards defaults, so extra NixOS randomEncryption options pass through. Fixes #438 --- lib/types/swap.nix | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/types/swap.nix b/lib/types/swap.nix index 3bed885..0723c73 100644 --- a/lib/types/swap.nix +++ b/lib/types/swap.nix @@ -7,6 +7,13 @@ device, ... }: +let + randomEncryptionEnabled = + if builtins.isBool config.randomEncryption then + config.randomEncryption + else + config.randomEncryption.enable or true; +in { options = { type = lib.mkOption { @@ -58,9 +65,17 @@ ''; }; randomEncryption = lib.mkOption { - type = lib.types.bool; + type = lib.types.either lib.types.bool (lib.types.attrsOf lib.types.anything); default = false; - description = "Whether to randomly encrypt the swap"; + description = '' + Whether to randomly encrypt the swap. + + Can be a plain boolean to just enable/disable it, or an attribute set + to pass through additional options to NixOS's + `swapDevices.*.randomEncryption` (e.g. `cipher`, `sectorSize`). When + given as an attribute set, encryption is enabled unless `enable` is + explicitly set to `false`. + ''; }; resumeDevice = lib.mkOption { type = lib.types.bool; @@ -81,7 +96,7 @@ _create = diskoLib.mkCreateOption { inherit config options; # TODO: we don't support encrypted swap yet - default = lib.optionalString (!config.randomEncryption) '' + default = lib.optionalString (!randomEncryptionEnabled) '' if ! blkid "${config.device}" -o export | grep -q '^TYPE='; then mkswap \ ${toString config.extraArgs} \ @@ -92,7 +107,7 @@ _mount = diskoLib.mkMountOption { inherit config options; # TODO: we don't support encrypted swap yet - default = lib.optionalAttrs (!config.randomEncryption) { + default = lib.optionalAttrs (!randomEncryptionEnabled) { fs.${config.device} = '' if test "''${DISKO_SKIP_SWAP:-}" != 1 && ! swapon --show | grep -q "^$(readlink -f "${config.device}") "; then swapon ${ @@ -107,7 +122,7 @@ }; _unmount = diskoLib.mkUnmountOption { inherit config options; - default = lib.optionalAttrs (!config.randomEncryption) { + default = lib.optionalAttrs (!randomEncryptionEnabled) { fs.${config.device} = '' if swapon --show | grep -q "^$(readlink -f "${config.device}") "; then swapoff "${config.device}" @@ -125,10 +140,11 @@ device = config.device; inherit (config) discardPolicy priority; randomEncryption = { - enable = config.randomEncryption; + enable = randomEncryptionEnabled; # forward discard/TRIM attempts through dm-crypt allowDiscards = config.discardPolicy != null; - }; + } + // lib.optionalAttrs (!builtins.isBool config.randomEncryption) config.randomEncryption; options = config.mountOptions; } ];