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