|
570 | 570 | systemctl reboot |
571 | 571 | ''; |
572 | 572 | }; |
| 573 | + |
| 574 | + cryptsetupPreCheckScript = pkgs.writeShellApplication { |
| 575 | + name = "cryptsetup-pre-check"; |
| 576 | + runtimeInputs = [ |
| 577 | + pkgs.cryptsetup |
| 578 | + pkgs.systemd |
| 579 | + pkgs.util-linux |
| 580 | + ]; |
| 581 | + text = '' |
| 582 | + DEVICE="${lvmPartition}" |
| 583 | +
|
| 584 | + # Wait for device to appear |
| 585 | + for _ in {1..30}; do |
| 586 | + if [ -e "$DEVICE" ]; then |
| 587 | + break |
| 588 | + fi |
| 589 | + sleep 1 |
| 590 | + done |
| 591 | +
|
| 592 | + udevadm settle || true |
| 593 | +
|
| 594 | + # Check if the device is LUKS — retry a few times for transient read errors |
| 595 | + for _ in {1..3}; do |
| 596 | + if cryptsetup isLuks "$DEVICE"; then |
| 597 | + mkdir -p /run |
| 598 | + touch /run/cryptsetup-pre-checked |
| 599 | + exit 0 |
| 600 | + fi |
| 601 | + sleep 1 |
| 602 | + done |
| 603 | +
|
| 604 | + # Device is not encrypted; do NOT create marker so cryptsetup is skipped |
| 605 | + ''; |
| 606 | + }; |
573 | 607 | in |
574 | 608 | { |
575 | 609 | _file = ./deferred-disk-encryption.nix; |
|
616 | 650 | pkgs.kmod |
617 | 651 | pkgs.pcsclite.lib |
618 | 652 | firstBootEncryptScript |
| 653 | + cryptsetupPreCheckScript |
619 | 654 | ]; |
620 | 655 |
|
621 | 656 | services = { |
|
708 | 743 | }; |
709 | 744 | }; |
710 | 745 |
|
| 746 | + # Lightweight pre-check service that runs before cryptsetup-pre.target. |
| 747 | + # Creates the /run/cryptsetup-pre-checked marker if the device is LUKS, |
| 748 | + # allowing systemd-cryptsetup@crypted to start through the normal systemd |
| 749 | + # path without first-boot-encrypt having to mediate (which causes a TTY |
| 750 | + # ownership conflict and ~2min input delay). |
| 751 | + cryptsetup-pre-check = { |
| 752 | + description = "Check if device is LUKS before cryptsetup"; |
| 753 | + unitConfig.DefaultDependencies = false; |
| 754 | + before = [ |
| 755 | + "cryptsetup-pre.target" |
| 756 | + "systemd-cryptsetup@crypted.service" |
| 757 | + ]; |
| 758 | + wantedBy = [ "cryptsetup-pre.target" ]; |
| 759 | + after = [ "${utils.escapeSystemdPath lvmPartition}.device" ]; |
| 760 | + serviceConfig = { |
| 761 | + Type = "oneshot"; |
| 762 | + RemainAfterExit = true; |
| 763 | + ExecStart = getExe cryptsetupPreCheckScript; |
| 764 | + }; |
| 765 | + }; |
| 766 | + |
711 | 767 | }; |
712 | 768 | }; |
713 | 769 |
|
|
0 commit comments