1import ./make-test-python.nix ({ lib, pkgs, ... }: {
2 name = "systemd-initrd-luks-tpm2";
3
4 nodes.machine = { pkgs, ... }: {
5 # Use systemd-boot
6 virtualisation = {
7 emptyDiskImages = [ 512 ];
8 useBootLoader = true;
9 # Booting off the TPM2-encrypted device requires an available init script
10 mountHostNixStore = true;
11 useEFIBoot = true;
12 tpm.enable = true;
13 };
14 boot.loader.systemd-boot.enable = true;
15
16 boot.initrd.availableKernelModules = [ "tpm_tis" ];
17
18 environment.systemPackages = with pkgs; [ cryptsetup ];
19 boot.initrd.systemd = {
20 enable = true;
21 };
22
23 specialisation.boot-luks.configuration = {
24 boot.initrd.luks.devices = lib.mkVMOverride {
25 cryptroot = {
26 device = "/dev/vdb";
27 crypttabExtraOpts = [ "tpm2-device=auto" ];
28 };
29 };
30 virtualisation.rootDevice = "/dev/mapper/cryptroot";
31 virtualisation.fileSystems."/".autoFormat = true;
32 };
33 };
34
35 testScript = ''
36 # Create encrypted volume
37 machine.wait_for_unit("multi-user.target")
38 machine.succeed("echo -n supersecret | cryptsetup luksFormat -q --iter-time=1 /dev/vdb -")
39 machine.succeed("PASSWORD=supersecret SYSTEMD_LOG_LEVEL=debug systemd-cryptenroll --tpm2-pcrs= --tpm2-device=auto /dev/vdb |& systemd-cat")
40
41 # Boot from the encrypted disk
42 machine.succeed("bootctl set-default nixos-generation-1-specialisation-boot-luks.conf")
43 machine.succeed("sync")
44 machine.crash()
45
46 # Boot and decrypt the disk
47 machine.wait_for_unit("multi-user.target")
48 assert "/dev/mapper/cryptroot on / type ext4" in machine.succeed("mount")
49 '';
50})