Skip to content

Revert "systemd: drop 0001-Start-device-units-for-uninitialised-encrypted-devic.patch" - #525118

Closed
ipetkov wants to merge 2 commits into
NixOS:stagingfrom
ipetkov:push-sttmlnrrquwl
Closed

ipetkov wants to merge 2 commits into
NixOS:stagingfrom
ipetkov:push-sttmlnrrquwl

Conversation

@ipetkov

@ipetkov ipetkov commented May 28, 2026

Copy link
Copy Markdown
Contributor

The systemd Start-device-units-for-uninitialised-encrypted-devic.patch was recently removed in b5611f9 via #488508 (cc @nikstur ).

The cited reason was that this is no longer need due to b138917, however, dropping the patch breaks booting configurations where a password-encrypted luks volume is then used as key-file to decrypt another luks volume. I've added a test case/reproduction for this (breaks without the patch, fixed after reverting).

Note: the existing systemd patches were reflowed after this patch was dropped. As part of the revert I renamed the prefix to 0000 to avoid having to reflow patches again, but let me know if there's a preferred approach here

Things done

@ipetkov ipetkov mentioned this pull request May 28, 2026
3 of 13 tasks
@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-nixos-tests This PR causes rebuilds for all NixOS tests and should normally target the staging branches. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. labels May 28, 2026
@nikstur

nikstur commented May 28, 2026

Copy link
Copy Markdown
Contributor

If you simply put a filesystem on /dev/mapper/cryptkey and create the keyfile on this fs (e.g. as /key) this will satisfy your usecase.

IMO this doesn't justify patching systemd.

@ipetkov

ipetkov commented May 28, 2026 •

Copy link
Copy Markdown
Contributor Author

If you simply put a filesystem on /dev/mapper/cryptkey and create the keyfile on this fs (e.g. as /key) this will satisfy your usecase.

@nikstur which file system are you referring to?

I have a setup where I have some random data on a partition (called cryptkey) which is unlocked with a password. The contents of /dev/mapper/cryptkey are the keyfile which unlock my encrypted root (cryptroot) and encrypted swap (cryptswap). I've had this setup ever since I started using NixOS over 6 years ago.

I was quite surprised when this suddenly broke. Granted the old shell script boot worked with this just fine, and even when the default switched to systemd boot it also worked fine (albeit having systemd manage doing the unlock).

As of right now, I can no longer boot any of my machines with this setup. Sure I suppose leaning on password reuse is something I can try instead of using a key-file, but I just wanted to ask, is it intentional to break what has been a valid luks setup (as far as NixOS has supported it for years until now)?

If yes, is this documented anywhere with any suggested alternative? I don't mind rolling some systemd glue code but I'm not exactly sure what's the path forward. Hence the revert just to get things back in working order. Happy if this goes away in the long term as long as there's some suggested path on how to make keyfiles work!

EDIT: rereading I think I see what you mean, though this will require some surgery which may end up locking me out of my disks. Probably worth documenting this loudly for anyone else who gets bitten by this if the breakage is intentional

@lheckemann

Copy link
Copy Markdown
Member

Is adding the udev rules in services.udev.extraRules not enough to address this use case?

@ipetkov

ipetkov commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

Is adding the udev rules in services.udev.extraRules not enough to address this use case?

Maybe? Hadn't had a chance to try it out yet, not an expert in udev or systemd, just came across the patch as part of a bisect to figure out what caused the breakage.

Are you suggesting I add this to my own configs or make this a default setting for everyone?

@ipetkov

ipetkov commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

Also the patch appears to remove some rules, not add new ones, not sure if that can be done via services.udev.extraRules

@nikstur

nikstur commented May 28, 2026

Copy link
Copy Markdown
Contributor

You should be able to fix this by:

Before doing anything I suggest you to enroll a password into a new keyslot of all of your LUKS volumes that you can use to recover via systemd-cryptenroll.

(1) On a booted system write they keyfile to a backup location on your rootfs (something like cat /dev/mapper/cryptkey > /backup.key.
(2) Either put an fs on /dev/mapper/cryptkey via mkfs.ext4 /dev/mapper/cryptkey and copy the keyfile to this new fs (cp /backup.key /dev/mapper/cryptkey/key) OR just leave the key on your rootfs and add a password via systemd-cryptenroll to your root volume.
(3) Adjust your config to either pick up the key from they newly formatted /dev/mapper/cryptkey (initrd.luks.devices.swap.keyFile = "/dev/mapper/cryptkey/key") or directly from the rootfs (e.g. swap.keyFile = "/sysroot/backup.key")

Re-adding this patch doesn't make sense as this is not a particularly useful use-case and violates our patch policy. The "normal" way (for which we also have a test) to unlock something like a swap partition without typing multiple passwords or relying on password re-use by systemd-cryptsetup is to store keyfiles on your rootfs. There is no need to go via this indirection of a separate LUKS volume just for the key. What you're doing wouldn't have worked on any other distro.

Additionally, re-adding this patch would only land in unstable in around a month as that's our typical staging cycle right now.

@flokli

flokli commented May 28, 2026

Copy link
Copy Markdown
Member

I think this was also at least partially dropped because the actual usecase / implications of the patch were not really documented in / visible from the commit message.

Is there a reason this is not upstream? If this is generally desirable behaviour, upstream might be receptive to it, and it might make more sense to get it into systemd proper, rather than shipping it only in the NixOS systemd. And if not, upstream might have a suggestion with how to accomplish the same goal differently.

@arianvp

arianvp commented May 28, 2026 •

Copy link
Copy Markdown
Member

There are probably children out there holding down spacebar to stay warm in the winter! YOUR UPDATE MURDERS CHILDREN.
xkcd: Workflow

Edit: tongue in cheek joke. Didn't mean to say this is not a valid usecase.

@ElvishJerricco

Copy link
Copy Markdown
Contributor

Is there a reason this is not upstream? If this is generally desirable behaviour, upstream might be receptive to it, and it might make more sense to get it into systemd proper, rather than shipping it only in the NixOS systemd. And if not, upstream might have a suggestion with how to accomplish the same goal differently.

It is my opinion that the patch should be upstream, and that systemd only ever had this udev rule to work around some rather clumsy behavior. The comment suggests that the device isn't ready because "we are probably still calling mke2fs or mkswap on it", but this is sort of a nonsense reason. For a systemd unit to be doing mkfs on the device, either it would have to depend on the device or it would have to be the one that did cryptsetup open in the first place. Well it can't be something that depends on the device, because the rule explicitly prevents that. So it would have to be the unit doing cryptsetup open, but systemd-cryptsetup doesn't format devices, so that's not really a good reason for this behavior either.

It doesn't really make sense, and it certainly violates the principle of least astonishment. It must exist because of some clumsy tooling that existed long ago that no one uses now, like something that does cryptsetup open ... && mkfs... not wanting to race with a mount unit but inexplicably not being ordered before said mount unit. It seems very unlikely to be relevant nowadays to me.

All that said, there are plenty of ways to work around it. @ipetkov, for one thing you can set keyFile to "/foo:${device}";, and that tells systemd-cryptsetup-generator to depend on a file that's on another device as the key file. It'll wait for device to appear, mount it privately, and use /foo on that file system as the key file. If that device is just /dev/mapper/foo from some other LUKS device, you get exactly the two stage behavior you want; first decrypt /dev/mapper/foo, then use a file on the contained file system to decrypt another drive.

So I agree that NixOS shouldn't be carrying this patch. It's easily worked around, and it's a way that NixOS's behavior inexplicably differs from other distros. But I do think this patch should be upstream.

@ipetkov

ipetkov commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed writeups on alternatives I can take. I'm going to triple check my backups and carefully rework my configs!

In the mean time I'm going to close this as its unlikely to get this landed back in nixpkgs. Will leave it to someone better versed to attempt at upstreaming this as well

@ipetkov ipetkov closed this May 29, 2026
@infinisil infinisil moved this from Needs Review to Reviewed in Nixpkgs security review May 29, 2026
ipetkov added a commit to ipetkov/dotfiles that referenced this pull request May 30, 2026
When I originally installed NixOS (all those years ago) I'll admit I
didn't fully know what I was doing (first "real" Linux install and all,
much less NixOS specifics) so I was following whatever random guides I
found online on how to set up disk encryption.

At the time I ended up just making an encrypted partition, blasting some
random bytes on to it and using that as the keyfile for the other
(root/swap) partitions. Perhaps at the time password caching/reuse was
annoying so this was a good way to have to type in the password only
once at boot?

Anyway, this apparently only worked on NixOS on the happenstance that
systemd was patched up to make it work. Recently that patch is being
dropped from NixOS (to minimize carrying non-upstreamed patches) which
ends up breaking this setup (systemd will decrypt the device but other
units depending on it end up hanging).

One easy solution (besides just using the same password on every other
encrypted partition and remembering to change them all in sync if
necessary) is to back up the cryptkey contents, reinitialize the
partition with a filesystem, and put the contents back in as a regular
file. With some special syntax, systemd will end up wiring things up
correctly at runtime.

NixOS/nixpkgs#525118 (comment)
@MMesch

MMesch commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Ran into this today as well. First time NixOS hangs on boot after update in 7 years :)

The fix below worked for me:

  boot.initrd.services.udev.packages = [
    (pkgs.writeTextDir "etc/udev/rules.d/99-zz-cryptkey-ready.rules" ''
      SUBSYSTEM=="block", ENV{DM_UUID}=="CRYPT-*", ENV{DM_NAME}=="cryptkey", ENV{SYSTEMD_READY}="1"
    '')
  ];

@flokli

flokli commented Jun 8, 2026

Copy link
Copy Markdown
Member

@nikstur I guess we should retroactively add this to the release notes. noone really was aware this was a legitimate (very race) usecase). It would still help people running into this when migrating to 26.05 now.

@MMesch the udev rule could be racy, upstream is not aware of it either and this is even less likely to stay working.

It's probably safer to migrate to simply using the same passphrase for all partitions. That usecase is known and used by lots of people. If you absolutely want two layers of luks, you can put the keys as a literal file on a filesystem on the encrypted keyfile volume, as ipetkov/dotfiles@2de353c did.

@MMesch

MMesch commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Yes that makes sense. The above is more a temporary workaround.

Regarding putting the key in an actual file on a file system, I'm a bit scared of the migration. @ipetkov how did you safely get to the new state from the old?

Using same passphrase everywhere seems relatively simple in comparison.

@ipetkov

ipetkov commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

@MMesch I wrote about my process here: https://ipetkov.dev/blog/correcting-my-nixos-luks-mistakes/

@ipetkov
ipetkov deleted the push-sttmlnrrquwl branch June 9, 2026 02:38
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/full-disk-encryption-with-ordering/64110/2

@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/boot-initrd-luks-dependent-devices-not-working/79623/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-nixos-tests This PR causes rebuilds for all NixOS tests and should normally target the staging branches.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

8 participants