1{ lib, pkgs, ... }: 2{ 3 name = "systemd-initrd-luks-fido2"; 4 5 nodes.machine = 6 { pkgs, config, ... }: 7 { 8 # Use systemd-boot 9 virtualisation = { 10 emptyDiskImages = [ 512 ]; 11 useBootLoader = true; 12 # Booting off the encrypted disk requires having a Nix store available for the init script 13 mountHostNixStore = true; 14 useEFIBoot = true; 15 qemu.options = [ 16 "-device pci-ohci,id=usb-bus" 17 "-device canokey,bus=usb-bus.0,file=/tmp/canokey-file" 18 ]; 19 }; 20 boot.loader.systemd-boot.enable = true; 21 22 boot.initrd.systemd.enable = true; 23 24 environment.systemPackages = with pkgs; [ cryptsetup ]; 25 26 specialisation.boot-luks.configuration = { 27 boot.initrd.luks.devices = lib.mkVMOverride { 28 cryptroot = { 29 device = "/dev/vdb"; 30 crypttabExtraOpts = [ "fido2-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 --fido2-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}