1{ lib, pkgs, ... }:
2{
3 name = "systemd-initrd-luks-password";
4
5 nodes.machine =
6 { pkgs, ... }:
7 {
8 # Use systemd-boot
9 virtualisation = {
10 emptyDiskImages = [
11 512
12 512
13 ];
14 useBootLoader = true;
15 # Booting off the encrypted disk requires an available init script
16 mountHostNixStore = true;
17 useEFIBoot = true;
18 };
19 boot.loader.systemd-boot.enable = true;
20
21 environment.systemPackages = with pkgs; [ cryptsetup ];
22 boot.initrd.systemd = {
23 enable = true;
24 emergencyAccess = true;
25 };
26
27 specialisation.boot-luks.configuration = {
28 boot.initrd.luks.devices = lib.mkVMOverride {
29 # We have two disks and only type one password - key reuse is in place
30 cryptroot.device = "/dev/vdb";
31 cryptroot2.device = "/dev/vdc";
32 };
33 virtualisation.rootDevice = "/dev/mapper/cryptroot";
34 virtualisation.fileSystems."/".autoFormat = true;
35 # test mounting device unlocked in initrd after switching root
36 virtualisation.fileSystems."/cryptroot2".device = "/dev/mapper/cryptroot2";
37 };
38 };
39
40 testScript = ''
41 # Create encrypted volume
42 machine.wait_for_unit("multi-user.target")
43 machine.succeed("echo -n supersecret | cryptsetup luksFormat -q --iter-time=1 /dev/vdb -")
44 machine.succeed("echo -n supersecret | cryptsetup luksFormat -q --iter-time=1 /dev/vdc -")
45 machine.succeed("echo -n supersecret | cryptsetup luksOpen -q /dev/vdc cryptroot2")
46 machine.succeed("mkfs.ext4 /dev/mapper/cryptroot2")
47
48 # Boot from the encrypted disk
49 machine.succeed("bootctl set-default nixos-generation-1-specialisation-boot-luks.conf")
50 machine.succeed("sync")
51 machine.crash()
52
53 # Boot and decrypt the disk
54 machine.start()
55 machine.wait_for_console_text("Please enter passphrase for disk cryptroot")
56 machine.send_console("supersecret\n")
57 machine.wait_for_unit("multi-user.target")
58
59 assert "/dev/mapper/cryptroot on / type ext4" in machine.succeed("mount"), "/dev/mapper/cryptroot do not appear in mountpoints list"
60 assert "/dev/mapper/cryptroot2 on /cryptroot2 type ext4" in machine.succeed("mount")
61 '';
62}