git.iko.soy/typeable/nixpkgs
on master help sign in
swap: extend randomEncryption to plainOpen and ability to select cipher
commitc3d5cfdc3ca709a9c5081b1a11bca533bc4788af authorMartin Wohlert <martin@b-root-force.de> dateMon 22 May 2017, 20:07 (10 years ago) parent81e998b
2 files changed+32−12
nixos/modules/config/swap.nix+31−11 nixos/modules/system/boot/stage-1.nix+1−1
nixos/modules/config/swap.nix+31−11
@@ -45,7 +45,7 @@ let
4545 '';
4646 };
4747
48− randomEncryption = mkOption {
48+ randomEncryption.enable = mkOption {
4949 default = false;
5050 type = types.bool;
5151 description = ''
@@ -61,6 +61,26 @@ let
6161 '';
6262 };
6363
64+ randomEncryption.cipher = mkOption {
65+ default = "aes-xts-plain64";
66+ example = "serpent-xts-plain64";
67+ type = types.str;
68+ description = ''
69+ Use specified cipher for randomEncryption.
70+
71+ Hint: Run "cryptsetup benchmark" to see which one is fastest on your machine.
72+ '';
73+ };
74+
75+ randomEncryption.source = mkOption {
76+ default = "/dev/urandom";
77+ example = "/dev/random";
78+ type = types.str;
79+ description = ''
80+ Define the source of randomness to obtain a random key for encryption.
81+ '';
82+ };
83+
6484 deviceName = mkOption {
6585 type = types.str;
6686 internal = true;
@@ -77,7 +97,7 @@ let
7797 device = mkIf options.label.isDefined
7898 "/dev/disk/by-label/${config.label}";
7999 deviceName = lib.replaceChars ["\\"] [""] (escapeSystemdPath config.device);
80− realDevice = if config.randomEncryption then "/dev/mapper/${deviceName}" else config.device;
100+ realDevice = if config.randomEncryption.enable then "/dev/mapper/${deviceName}" else config.device;
81101 };
82102
83103 };
@@ -125,14 +145,14 @@ in
125145
126146 createSwapDevice = sw:
127147 assert sw.device != "";
128− assert !(sw.randomEncryption && lib.hasPrefix "/dev/disk/by-uuid" sw.device);
129− assert !(sw.randomEncryption && lib.hasPrefix "/dev/disk/by-label" sw.device);
148+ assert !(sw.randomEncryption.enable && lib.hasPrefix "/dev/disk/by-uuid" sw.device);
149+ assert !(sw.randomEncryption.enable && lib.hasPrefix "/dev/disk/by-label" sw.device);
130150 let realDevice' = escapeSystemdPath sw.realDevice;
131151 in nameValuePair "mkswap-${sw.deviceName}"
132152 { description = "Initialisation of swap device ${sw.device}";
133153 wantedBy = [ "${realDevice'}.swap" ];
134154 before = [ "${realDevice'}.swap" ];
135− path = [ pkgs.utillinux ] ++ optional sw.randomEncryption pkgs.cryptsetup;
155+ path = [ pkgs.utillinux ] ++ optional sw.randomEncryption.enable pkgs.cryptsetup;
136156
137157 script =
138158 ''
@@ -145,11 +165,11 @@ in
145165 truncate --size "${toString sw.size}M" "${sw.device}"
146166 fi
147167 chmod 0600 ${sw.device}
148− ${optionalString (!sw.randomEncryption) "mkswap ${sw.realDevice}"}
168+ ${optionalString (!sw.randomEncryption.enable) "mkswap ${sw.realDevice}"}
149169 fi
150170 ''}
151− ${optionalString sw.randomEncryption ''
152− cryptsetup open ${sw.device} ${sw.deviceName} --type plain --key-file /dev/urandom
171+ ${optionalString sw.randomEncryption.enable ''
172+ cryptsetup plainOpen -c ${sw.randomEncryption.cipher} -d ${sw.randomEncryption.source} ${sw.device} ${sw.deviceName}
153173 mkswap ${sw.realDevice}
154174 ''}
155175 '';
@@ -157,12 +177,12 @@ in
157177 unitConfig.RequiresMountsFor = [ "${dirOf sw.device}" ];
158178 unitConfig.DefaultDependencies = false; # needed to prevent a cycle
159179 serviceConfig.Type = "oneshot";
160− serviceConfig.RemainAfterExit = sw.randomEncryption;
161− serviceConfig.ExecStop = optionalString sw.randomEncryption "${pkgs.cryptsetup}/bin/cryptsetup luksClose ${sw.deviceName}";
180+ serviceConfig.RemainAfterExit = sw.randomEncryption.enable;
181+ serviceConfig.ExecStop = optionalString sw.randomEncryption.enable "${pkgs.cryptsetup}/bin/cryptsetup luksClose ${sw.deviceName}";
162182 restartIfChanged = false;
163183 };
164184
165− in listToAttrs (map createSwapDevice (filter (sw: sw.size != null || sw.randomEncryption) config.swapDevices));
185+ in listToAttrs (map createSwapDevice (filter (sw: sw.size != null || sw.randomEncryption.enable) config.swapDevices));
166186
167187 };
168188
nixos/modules/system/boot/stage-1.nix+1−1
@@ -207,7 +207,7 @@ let
207207 preLVMCommands preDeviceCommands postDeviceCommands postMountCommands preFailCommands kernelModules;
208208
209209 resumeDevices = map (sd: if sd ? device then sd.device else "/dev/disk/by-label/${sd.label}")
210− (filter (sd: hasPrefix "/dev/" sd.device && !sd.randomEncryption
210+ (filter (sd: hasPrefix "/dev/" sd.device && !sd.randomEncryption.enable
211211 # Don't include zram devices
212212 && !(hasPrefix "/dev/zram" sd.device)
213213 ) config.swapDevices);