1{
2 systemdStage1,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 keyfile = pkgs.writeText "luks-keyfile" ''
9 MIGHAoGBAJ4rGTSo/ldyjQypd0kuS7k2OSsmQYzMH6TNj3nQ/vIUjDn7fqa3slt2
10 gV6EK3TmTbGc4tzC1v4SWx2m+2Bjdtn4Fs4wiBwn1lbRdC6i5ZYCqasTWIntWn+6
11 FllUkMD5oqjOR/YcboxG8Z3B5sJuvTP9llsF+gnuveWih9dpbBr7AgEC
12 '';
13in
14{
15 name = "initrd-luks-empty-passphrase";
16
17 _module.args.systemdStage1 = lib.mkDefault false;
18
19 nodes.machine =
20 { pkgs, ... }:
21 {
22 imports = lib.optionals (!systemdStage1) [ ./common/auto-format-root-device.nix ];
23
24 virtualisation = {
25 emptyDiskImages = [ 512 ];
26 useBootLoader = true;
27 useEFIBoot = true;
28 # This requires to have access
29 # to a host Nix store as
30 # the new root device is /dev/vdb
31 # an empty 512MiB drive, containing no Nix store.
32 mountHostNixStore = true;
33 fileSystems."/".autoFormat = lib.mkIf systemdStage1 true;
34 };
35
36 boot.loader.systemd-boot.enable = true;
37 boot.initrd.systemd = lib.mkIf systemdStage1 {
38 enable = true;
39 emergencyAccess = true;
40 };
41 environment.systemPackages = with pkgs; [ cryptsetup ];
42
43 specialisation.boot-luks-wrong-keyfile.configuration = {
44 boot.initrd.luks.devices = lib.mkVMOverride {
45 cryptroot = {
46 device = "/dev/vdb";
47 keyFile = "/etc/cryptroot.key";
48 tryEmptyPassphrase = true;
49 fallbackToPassword = !systemdStage1;
50 };
51 };
52 virtualisation.rootDevice = "/dev/mapper/cryptroot";
53 boot.initrd.secrets."/etc/cryptroot.key" = keyfile;
54 };
55
56 specialisation.boot-luks-missing-keyfile.configuration = {
57 boot.initrd.luks.devices = lib.mkVMOverride {
58 cryptroot = {
59 device = "/dev/vdb";
60 keyFile = "/etc/cryptroot.key";
61 tryEmptyPassphrase = true;
62 fallbackToPassword = !systemdStage1;
63 };
64 };
65 virtualisation.rootDevice = "/dev/mapper/cryptroot";
66 };
67 };
68
69 testScript = ''
70 # Encrypt key with empty key so boot should try keyfile and then fallback to empty passphrase
71
72
73 def grub_select_boot_luks_wrong_key_file():
74 """
75 Selects "boot-luks" from the GRUB menu
76 to trigger a login request.
77 """
78 machine.send_monitor_command("sendkey down")
79 machine.send_monitor_command("sendkey down")
80 machine.send_monitor_command("sendkey ret")
81
82 def grub_select_boot_luks_missing_key_file():
83 """
84 Selects "boot-luks" from the GRUB menu
85 to trigger a login request.
86 """
87 machine.send_monitor_command("sendkey down")
88 machine.send_monitor_command("sendkey ret")
89
90 # Create encrypted volume
91 machine.wait_for_unit("multi-user.target")
92 machine.succeed("echo "" | cryptsetup luksFormat /dev/vdb --batch-mode")
93 machine.succeed("bootctl set-default nixos-generation-1-specialisation-boot-luks-wrong-keyfile.conf")
94 machine.succeed("sync")
95 machine.crash()
96
97 # Check if rootfs is on /dev/mapper/cryptroot
98 machine.wait_for_unit("multi-user.target")
99 assert "/dev/mapper/cryptroot on / type ext4" in machine.succeed("mount")
100
101 # Choose boot-luks-missing-keyfile specialisation
102 machine.succeed("bootctl set-default nixos-generation-1-specialisation-boot-luks-missing-keyfile.conf")
103 machine.succeed("sync")
104 machine.crash()
105
106 # Check if rootfs is on /dev/mapper/cryptroot
107 machine.wait_for_unit("multi-user.target")
108 assert "/dev/mapper/cryptroot on / type ext4" in machine.succeed("mount")
109 '';
110}