git.iko.soy/typeable/nixpkgs
on master help sign in
boot.initrd.luks.devices: Change into an attribute set

This allows setting options for the same LUKS device in different modules. For example, the auto-generated hardware-configuration.nix can contain boot.initrd.luks.devices.crypted.device = "/dev/disk/..."; while configuration.nix can add boot.initrd.luks.devices.crypted.allowDiscards = true; Also updated the examples/docs to use /disk/disk/by-uuid instead of /dev/sda, since we shouldn't promote the use of the latter.

commit845c9b50bf7137c3e21f443e70ebcb16510f4e68 authorEelco Dolstra <eelco.dolstra@logicblox.com> dateWed 25 May 2016, 13:23 (11 years ago) parentbf2aaeb
4 files changed+29−30
nixos/doc/manual/configuration/luks-file-systems.xml+6−6 nixos/modules/system/boot/luksroot.nix+21−21 nixos/modules/virtualisation/qemu-vm.nix+1−1 nixos/tests/installer.nix+1−2
nixos/doc/manual/configuration/luks-file-systems.xml+6−6
@@ -9,21 +9,21 @@
99<para>NixOS supports file systems that are encrypted using
1010<emphasis>LUKS</emphasis> (Linux Unified Key Setup). For example,
1111here is how you create an encrypted Ext4 file system on the device
12−<filename>/dev/sda2</filename>:
12+<filename>/dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d</filename>:
1313
1414<screen>
15−$ cryptsetup luksFormat /dev/sda2
15+$ cryptsetup luksFormat /dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d
1616
1717WARNING!
1818========
19−This will overwrite data on /dev/sda2 irrevocably.
19+This will overwrite data on /dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d irrevocably.
2020
2121Are you sure? (Type uppercase yes): YES
2222Enter LUKS passphrase: ***
2323Verify passphrase: ***
2424
25−$ cryptsetup luksOpen /dev/sda2 crypted
26−Enter passphrase for /dev/sda2: ***
25+$ cryptsetup luksOpen /dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d crypted
26+Enter passphrase for /dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d: ***
2727
2828$ mkfs.ext4 /dev/mapper/crypted
2929</screen>
@@ -33,7 +33,7 @@ as <filename>/</filename>, add the following to
3333<filename>configuration.nix</filename>:
3434
3535<programlisting>
36−boot.initrd.luks.devices = [ { device = "/dev/sda2"; name = "crypted"; } ];
36+boot.initrd.luks.devices.crypted.device = "/dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d";
3737fileSystems."/".device = "/dev/mapper/crypted";
3838</programlisting>
3939
nixos/modules/system/boot/luksroot.nix+21−21
@@ -5,7 +5,7 @@ with lib;
55let
66 luks = config.boot.initrd.luks;
77
8− openCommand = { name, device, header, keyFile, keyFileSize, allowDiscards, yubikey, ... }: ''
8+ openCommand = name': { name, device, header, keyFile, keyFileSize, allowDiscards, yubikey, ... }: assert name' == name; ''
99 # Wait for luksRoot to appear, e.g. if on a usb drive.
1010 # XXX: copied and adapted from stage-1-init.sh - should be
1111 # available as a function.
@@ -192,9 +192,8 @@ let
192192 ''}
193193 '';
194194
195− isPreLVM = f: f.preLVM;
196− preLVM = filter isPreLVM luks.devices;
197− postLVM = filter (f: !(isPreLVM f)) luks.devices;
195+ preLVM = filterAttrs (n: v: v.preLVM) luks.devices;
196+ postLVM = filterAttrs (n: v: !v.preLVM) luks.devices;
198197
199198in
200199{
@@ -228,31 +227,31 @@ in
228227 };
229228
230229 boot.initrd.luks.devices = mkOption {
231− default = [ ];
232− example = literalExample ''[ { name = "luksroot"; device = "/dev/sda3"; preLVM = true; } ]'';
230+ default = { };
231+ example = { "luksroot".device = "/dev/disk/by-uuid/430e9eff-d852-4f68-aa3b-2fa3599ebe08"; };
233232 description = ''
234− The list of devices that should be decrypted using LUKS before trying to mount the
235− root partition. This works for both LVM-over-LUKS and LUKS-over-LVM setups.
236−
237− The devices are decrypted to the device mapper names defined.
238−
239− Make sure that initrd has the crypto modules needed for decryption.
233+ The encrypted disk that should be opened before the root
234+ filesystem is mounted. Both LVM-over-LUKS and LUKS-over-LVM
235+ setups are sypported. The unencrypted devices can be accessed as
236+ <filename>/dev/mapper/<replaceable>name</replaceable></filename>.
240237 '';
241238
242− type = types.listOf types.optionSet;
239+ type = types.loaOf types.optionSet;
243240
244− options = {
241+ options = { name, ... }: { options = {
245242
246243 name = mkOption {
244+ visible = false;
245+ default = name;
247246 example = "luksroot";
248247 type = types.str;
249− description = "Named to be used for the generated device in /dev/mapper.";
248+ description = "Name of the unencrypted device in <filename>/dev/mapper</filename>.";
250249 };
251250
252251 device = mkOption {
253− example = "/dev/sda2";
252+ example = "/dev/disk/by-uuid/430e9eff-d852-4f68-aa3b-2fa3599ebe08";
254253 type = types.str;
255− description = "Path of the underlying block device.";
254+ description = "Path of the underlying encrypted block device.";
256255 };
257256
258257 header = mkOption {
@@ -289,6 +288,7 @@ in
289288 '';
290289 };
291290
291+ # FIXME: get rid of this option.
292292 preLVM = mkOption {
293293 default = true;
294294 type = types.bool;
@@ -394,7 +394,7 @@ in
394394 };
395395 };
396396
397− };
397+ }; };
398398 };
399399
400400 boot.initrd.luks.yubikeySupport = mkOption {
@@ -408,7 +408,7 @@ in
408408 };
409409 };
410410
411− config = mkIf (luks.devices != []) {
411+ config = mkIf (luks.devices != {}) {
412412
413413 # actually, sbp2 driver is the one enabling the DMA attack, but this needs to be tested
414414 boot.blacklistedKernelModules = optionals luks.mitigateDMAAttacks
@@ -463,8 +463,8 @@ in
463463 ''}
464464 '';
465465
466− boot.initrd.preLVMCommands = concatMapStrings openCommand preLVM;
467− boot.initrd.postDeviceCommands = concatMapStrings openCommand postLVM;
466+ boot.initrd.preLVMCommands = concatStrings (mapAttrsToList openCommand preLVM);
467+ boot.initrd.postDeviceCommands = concatStrings (mapAttrsToList openCommand postLVM);
468468
469469 environment.systemPackages = [ pkgs.cryptsetup ];
470470 };
nixos/modules/virtualisation/qemu-vm.nix+1−1
@@ -465,7 +465,7 @@ in
465465 });
466466
467467 swapDevices = mkVMOverride [ ];
468− boot.initrd.luks.devices = mkVMOverride [];
468+ boot.initrd.luks.devices = mkVMOverride {};
469469
470470 # Don't run ntpd in the guest. It should get the correct time from KVM.
471471 services.ntp.enable = false;
nixos/tests/installer.nix+1−2
@@ -363,8 +363,7 @@ in {
363363 # XXX: Currently, generate-config doesn't detect LUKS yet.
364364 extraConfig = ''
365365 boot.kernelParams = lib.mkAfter [ "console=tty0" ];
366− boot.initrd.luks.devices = lib.singleton {
367− name = "cryptroot";
366+ boot.initrd.luks.devices.cryptroot = {
368367 device = "/dev/vda3";
369368 preLVM = true;
370369 };