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`.
This commit is contained in:
Michael Hoang committed 2026-05-20 00:07:06 +02:00
1 parent 65fb947964
commit d60b2b5e00
1 file changed
+6
+6
View File
@@ -236,11 +236,14 @@ in
${toString ( ${toString (
lib.forEach config.additionalKeyFiles (keyFile: '' lib.forEach config.additionalKeyFiles (keyFile: ''
if ! cryptsetup open --test-passphrase --key-file=${keyFile} "${config.device}" 2>/dev/null; then
cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs} cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs}
fi
'') '')
)} )}
${lib.optionalString config.enrollRecovery '' ${lib.optionalString config.enrollRecovery ''
if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw recovery; then
systemd-cryptenroll \ systemd-cryptenroll \
--recovery-key \ --recovery-key \
--unlock-key-file=${formatKeyFile} \ --unlock-key-file=${formatKeyFile} \
@@ -248,8 +251,10 @@ in
if [ -z ''${IN_DISKO_TEST+x} ]; then 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 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
fi
''} ''}
${lib.optionalString config.enrollFido2 '' ${lib.optionalString config.enrollFido2 ''
if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw fido2; then
wait_for_token() { wait_for_token() {
set +f set +f
echo "Waiting for FIDO2 token insertion..." echo "Waiting for FIDO2 token insertion..."
@@ -274,6 +279,7 @@ in
--unlock-key-file=${formatKeyFile} \ --unlock-key-file=${formatKeyFile} \
${toString config.extraFido2EnrollArgs} \ ${toString config.extraFido2EnrollArgs} \
"${config.device}" "${config.device}"
fi
''} ''}
${lib.optionalString (config.content != null) config.content._create} ${lib.optionalString (config.content != null) config.content._create}
''; '';