From d60b2b5e001f7f476acba9ec2eeceb70bc9e759d Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Tue, 19 May 2026 09:11:05 +0200 Subject: [PATCH 1/5] luks: improve idempotency for enrolling FIDO2 Using FIDO2 automatically generates a temporary password for setting up LUKS which is not available on subsequent runs of the script causing the format script to crash with `password: unbound variable`. --- lib/types/luks.nix | 64 +++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/lib/types/luks.nix b/lib/types/luks.nix index 67a9420..8ed3225 100644 --- a/lib/types/luks.nix +++ b/lib/types/luks.nix @@ -236,44 +236,50 @@ in ${toString ( lib.forEach config.additionalKeyFiles (keyFile: '' - cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs} + if ! cryptsetup open --test-passphrase --key-file=${keyFile} "${config.device}" 2>/dev/null; then + cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs} + fi '') )} ${lib.optionalString config.enrollRecovery '' - systemd-cryptenroll \ - --recovery-key \ - --unlock-key-file=${formatKeyFile} \ - "${config.device}" - if [ -z ''${IN_DISKO_TEST+x} ]; then - set +x; read -p "Press Enter when you scanned the QR code offscreen or that the recovery key is stored securely."; set -x + if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw recovery; then + systemd-cryptenroll \ + --recovery-key \ + --unlock-key-file=${formatKeyFile} \ + "${config.device}" + if [ -z ''${IN_DISKO_TEST+x} ]; then + set +x; read -p "Press Enter when you scanned the QR code offscreen or that the recovery key is stored securely."; set -x + fi fi ''} ${lib.optionalString config.enrollFido2 '' - wait_for_token() { - set +f - echo "Waiting for FIDO2 token insertion..." + if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw fido2; then + wait_for_token() { + set +f + echo "Waiting for FIDO2 token insertion..." - # Check if any FIDO2 device is available via /dev/hidraw* - while true; do - if ls /dev/hidraw* &>/dev/null; then - echo "FIDO2 device detected." - break - else - echo "FIDO2 device not detected, waiting..." - sleep 2 - fi - done - set -f - } + # Check if any FIDO2 device is available via /dev/hidraw* + while true; do + if ls /dev/hidraw* &>/dev/null; then + echo "FIDO2 device detected." + break + else + echo "FIDO2 device not detected, waiting..." + sleep 2 + fi + done + set -f + } - wait_for_token - systemd-cryptenroll \ - --fido2-device=auto \ - ''${SLOT_ZERO_TO_DELETE:+--wipe-slot=0} \ - --unlock-key-file=${formatKeyFile} \ - ${toString config.extraFido2EnrollArgs} \ - "${config.device}" + wait_for_token + systemd-cryptenroll \ + --fido2-device=auto \ + ''${SLOT_ZERO_TO_DELETE:+--wipe-slot=0} \ + --unlock-key-file=${formatKeyFile} \ + ${toString config.extraFido2EnrollArgs} \ + "${config.device}" + fi ''} ${lib.optionalString (config.content != null) config.content._create} ''; From 0d15976fce51cac59449ebf663070055bb809fcc Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Tue, 19 May 2026 09:46:10 +0200 Subject: [PATCH 2/5] luks: don't depend on autogenerated password --- lib/types/luks.nix | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/types/luks.nix b/lib/types/luks.nix index 8ed3225..cf9e631 100644 --- a/lib/types/luks.nix +++ b/lib/types/luks.nix @@ -227,13 +227,17 @@ in export SLOT_ZERO_TO_DELETE=true ''} cryptsetup -q luksFormat "${config.device}" ${toString config.extraFormatArgs} ${formatKeyFileArgs} - fi - if ! cryptsetup status "${config.name}" >/dev/null; then ${formatCryptsetupOpen} \ --persistent fi + # Unlock without relying on the autogenerated password when someone + # calls this script after unmounting or rebooting + if ! cryptsetup status "${config.name}" >/dev/null; then + ${cryptsetupOpen} + fi + ${toString ( lib.forEach config.additionalKeyFiles (keyFile: '' if ! cryptsetup open --test-passphrase --key-file=${keyFile} "${config.device}" 2>/dev/null; then From 7da3d737f71783d224c94096d517d51632002c93 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Tue, 19 May 2026 16:40:14 +0200 Subject: [PATCH 3/5] luks: fix not asking for password in format script --- lib/types/luks.nix | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/types/luks.nix b/lib/types/luks.nix index cf9e631..9fc0b02 100644 --- a/lib/types/luks.nix +++ b/lib/types/luks.nix @@ -62,10 +62,25 @@ let ''; # Use this open command when you want to open it after full enrollment, e.g. at mount time or in standard enrollments. - cryptsetupOpen = createOpenCommand { - inherit keyFileArgs; - tokenType = if config.enrollFido2 then "systemd-fido2" else null; - }; + cryptsetupOpen = '' + ${lib.optionalString config.askPassword '' + if [ -z "''${password:-}" ]; then + if [ -z ''${IN_DISKO_TEST+x} ]; then + set +x + echo "Enter password for ${config.device}" + IFS= read -r -s password + export password + set -x + else + export password=disko + fi + fi + ''} + ${createOpenCommand { + inherit keyFileArgs; + tokenType = if config.enrollFido2 then "systemd-fido2" else null; + }} + ''; # Use this open command when you want to open it immediately after the formatting and before the stage 2 process is finished (i.e. the wipe slot). formatCryptsetupOpen = createOpenCommand { @@ -297,17 +312,6 @@ in { dev = '' if ! cryptsetup status "${config.name}" >/dev/null 2>/dev/null; then - ${lib.optionalString config.askPassword '' - if [ -z ''${IN_DISKO_TEST+x} ]; then - set +x - echo "Enter password for ${config.device}" - IFS= read -r -s password - export password - set -x - else - export password=disko - fi - ''} ${cryptsetupOpen} fi ${lib.optionalString (config.content != null) contentMount.dev or ""} From 640d1dd8e00f1522b4f34cdddfcce199d0159c07 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Mon, 18 May 2026 14:13:51 +0200 Subject: [PATCH 4/5] tests: fix enabling Canokey --- lib/tests.nix | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/tests.nix b/lib/tests.nix index 4c7d7ed..7d96372 100644 --- a/lib/tests.nix +++ b/lib/tests.nix @@ -96,8 +96,13 @@ let # Get qemu-common library functions for this pkgs qemu-common = qemu-common-lib pkgs; + + qemuPkg = pkgs.qemu_test.override { + canokeySupport = enableCanokey; + }; + # qemuBinary returns a string like: "/nix/store/.../qemu-system-x86_64 -machine accel=kvm:tcg -cpu max" - qemuBinaryString = qemu-common.qemuBinary pkgs.qemu_test; + qemuBinaryString = qemu-common.qemuBinary qemuPkg; # for installation we skip /dev/vda because it is the test runner disk @@ -301,6 +306,9 @@ let "-device pci-ohci,id=usb-bus" "-device canokey,bus=usb-bus.0,file=/tmp/canokey-file" ]; + + # REMOVEME when Canokey support is enabled upstream + qemu.package = lib.mkForce qemuPkg; }; # useful for debugging via repl From a1b9822f032747189a83688253043f563619d7ce Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Mon, 18 May 2026 14:13:51 +0200 Subject: [PATCH 5/5] luks: add test for FIDO2 --- tests/luks-fido2.nix | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/luks-fido2.nix diff --git a/tests/luks-fido2.nix b/tests/luks-fido2.nix new file mode 100644 index 0000000..35b7d74 --- /dev/null +++ b/tests/luks-fido2.nix @@ -0,0 +1,18 @@ +{ + pkgs ? import { }, + diskoLib ? pkgs.callPackage ../lib { }, +}: +diskoLib.testLib.makeDiskoTest { + inherit pkgs; + name = "luks-fido2"; + disko-config = ../example/luks-fido2.nix; + enableCanokey = true; + extraTestScript = '' + machine.succeed("cryptsetup isLuks /dev/vda2"); + machine.succeed("mountpoint /"); + + machine.succeed("systemd-cryptenroll /dev/vda2 | grep -qw fido2") + # Recovery should be disabled + machine.fail("systemd-cryptenroll /dev/vda2 | grep -qw recovery") + ''; +}