nixos-rebuild switch was successful.
However, when I reboot, I get stuck at the NixOS Stage 1 with this error:
Waiting 10 seconds for device /dev/disks/by-uuid/2b723313-e459-4a53-b209-cd82c8274fb1 to appear.......
An error occured in stage 1 of the boot process, which must mount the
root filesystem on `/mnt-root` and then start stage 2. Press one
of the following keys:
r) to reboot immediately
*) to ignore the error and continue
It seems that the system can’t find or mount the encrypted disk during boot. What am I missing in my configuration? How can I properly set up NixOS to mount this LUKS-encrypted LVM disk on boot?
I had actually already tried the method you suggested. Unfortunately, it didn’t resolve the issue in my case. The system still fails to find the encrypted disk during boot.
Waiting 10 seconds for device /dev/vg_crypt/lv_crypt .....
Long shot here, in case you are flexible in this regard; I’ve been using encrypted ZFS and never had difficulty setting up boot up passphrase prompt. It kind of just works. If you like, I can share my related config.
I tried ZFS but failed. After entering the ZFS password at the boot screen, it showed “zfs mountpoint or dataset is busy” and entered emergency root mode. Even if I succeeded, I wouldn’t use ZFS because it cannot reuse the LUKS password entered during NixOS Stage1, meaning I would have to enter the password twice to boot successfully.
I’ve decided that if I can’t successfully use a LUKS-encrypted LVM disk, I won’t decrypt and mount the disk at boot. Instead, I will mount it when needed, writing a script to handle this task.
I understand, thanks for clarifying. My main concern is to minimize the number of passwords I need to enter at boot. Since ZFS encryption and LUKS would require separate passwords, it complicates things for my setup.
I’ve managed to achieve what I want with a service:
What you had originally would have been fine. All you were missing was boot.initrd.luks.devices."crypt_disk1".preLVM = false;. Basically, stage 1 has distinct phases in which it attempts to do things, and by default encrypted devices are found before LVM devices.
This is one thing that the systemd-based initrd will improve upon. boot.initrd.systemd.enable = true; would have enabled this, and it would have handled this without any configuration.
Finally, initrd doesn’t even technically have to be involved at all in this, because the file system isn’t required to reach stage 2. Rather than using boot.initrd.luks.devices.crypt_disk1, you could just configure /etc/crypttab like this:
And this would have done the decryption in stage 2 instead (in exactly the same way that systemd-based initrd would have, just during stage 2 instead of the more limited stage 1 initrd environment)
So, TL;DR: There were sort of three different ways that could have solved the problem. 1) Do it with preLVM = false;, 2) Do it with systemd in stage 1, 3) Do it with systemd in stage 2 via /etc/crypttab.
I hope your methods would work, but I don’t want to try them at this point.
I have already configured my system and believe it offers better security than manually entering a passphrase. I generated a very secure key file and use agenix to automatically decrypt and mount the disk. This way, I don’t even know the password, but I know where the key file is and which other public key it is encrypted with.