TL;DR : systemd now ships eBPF programs (nsresourced, mountfsd, networkd helpers). eBPF programs are compiled against the Linux kernel they will run on, specifically kernel’s BTF informations, its vmlinux.h, its exact structure layouts. This is because the Linux kernel has no stable ABI for its internal data structures. eBPF CO-RE is unfortunately insufficient as a generic solution to this problem. This result into forcing systemd into the per-kernel package set (config.boot.kernelPackages.systemd on NixOS), the same way nvidia or zfs live in linuxPackages. The moment systemd is per-kernel, every package that depends on the full systemd will either have to inherit that coupling or migrate to a lighter package such as systemdLibs.
nsresourced and friends
Recent systemd releases have started to ship eBPF based tools such as nsresourced, mountfsd and BPF-LSM more generally which paves the path for unprivileged systemd-nspawn, unprivileged UID delegations, unprivileged mounting of disk images based on policies (dm-verity, etc.), policies to restrict binary execution from non-dm-verity partitions and more.
eBPF will probably see even more adoption in the future of the Linux userspace.
For nixpkgs, this has two consequences:
- systemd requires a
vmlinux.h(either provided or derived from the build host’s running kernel) to build these components. - The resulting systemd built is valid for a kernel whose type information (think: data structures offsets, etc.) matches the one they were built against, otherwise, horrible things may happen.
Why does eBPF requires such strong coupling to the kernel?
eBPF programs run inside the kernel. The types using in an eBPF kernel observe kernel internal data structures which are not part of the stable ABI promise that the Linux kernel offer to the userspace, for reminders: Linux kernel guarantee that the syscall API and the exported UAPI headers are stable. Everything else, e.g. struct task_struct* is not. Some of these structures even change across KConfig changes. See https://www.kernel.org/doc/Documentation/process/stable-api-nonsense.rst for more.
How portable eBPF is even supposed to work? One may ask.
BTF and CO-RE were developed to this end. BTF allow you to reconstruct the vmlinux.h from a kernel image via bpftool btf dump file vmlinux format c and CO-RE records relocations so fields offsets can be fixed up at load time.
But… CO-RE is a mitigation and is not finished. It can relocate a field that moved, it cannot conjure a field that doesn’t exist and many other scenarios can show up that CO-RE cannot repair. In practice, BPF objects must be built against a BTF that matches (or safely subsumes) the target kernel.
This lead to a debate among nixpkgs maintainers whether we should maintain our own vmlinux.h file (home grown) across the list of nixpkgs supported kernels — or abstain from such a maintenance load. We discussed this solution with systemd upstream and they do not advise it and would not want to share any maintenance load on this for all the distros out there.
It is now possible in unstable to provide vmlinux-btf to systemd and linuxPackages.ebpf-headers is exactly the output of bpftool btf dump file vmlinux format c which is plugged into vmlinux-btf’s systemd argument.
(Please do not reopen the debate on whether we should maintain our own vmlinux.h or not. Please do not pester the systemd upstream on this if you are not involved with systemd maintenance already.)
Hence… linuxPackages.systemd
As a logical conclusion, systemd is now observing the Linux kernel implementation details, thus it goes into the linuxPackages set.
Note that NixOS did not switch yet to a kernel-bound systemd: nixos: switch to kernel-bound systemd by default by RaitoBezarius · Pull Request #569868 · NixOS/nixpkgs · GitHub .
This means that the eBPF programs are still unavailable by default on NixOS but you can decide (today!) to run with systemd.package = config.boot.kernelPackages.systemd; and report whether this works out for you or not.
Moving to a kernel-bound systemd can only happen after remaining cleanups are taken care of, more on this below.
The first half of remaining chores: systemdLibs
Here is the part that is the most important for packagers and maintainers.
If systemd is now a per-kernel artifact, then “my package depends on systemd” means “my package depends on a Linux kernel”, writing:
buildInputs = [ systemd ];
is not a good idea anymore if you only need sd_notify for example.
So we have started a treewide migration moving as many programs as possible to systemdLibs which is the library only part of systemd.
As much as possible, consider:
buildInputs = [ systemdLibs ];
In addition to making your package not depend on the kernel, this also result in your package depending on less (!) stuff, i.e. 1k build-time dependencies vs. 2k build-time dependencies and save up to ~100MB of closure size.
The other half of remaining chores: late binding
systemdLibs handle packages that links against systemd. Packages that execute systemd binaries also need a solution.
Today the answer is “whatever systemd derivation this package was built with”, e.g.
substituteInPlace ... \
--replace-fail "systemd-escape" "${lib.getExe' systemd "systemd-escape}"
That is a hard build-time dependency on one systemd variant which might not even be related to the systemd running on your system because we upgrade systemd at runtime via switch-to-configuration.
Instead, the package should refer to the running system’s tools, resolved at runtime, from /run/current-system/sw/bin. This increases correctness and remove this dependency edge.
- On non-NixOS systems, the most accurate systemd tool is the one of the distro’s system and not ours, we should not push our own client which may be incompatible with the remote distro systemd server.
- On kernel-bound systemd NixOS systems, we can upgrade a systemd and have a desync with the client.
Here’s the replacements, ranked from the worst to the best:
substituteInPlace ... \
--replace-fail "systemd-escape" "/run/current-system/sw/bin/systemd-escape"
This breaks the program for non-NixOS systems.
wrapGApp $out/bin/miracle-wm \
--set PATH ${lib.makeBinPath [ coreutils dbus ]} # dropped systemd from here
--suffix PATH : "/run/current-system/sw/bin" # new
This also breaks program for non-NixOS systems because PATH is clobbered, if possible, proceed this way:
wrapGApp $out/bin/miracle-wm \
--prefix PATH : ${lib.makeBinPath [ coreutils dbus ]} # if coreutils and dbus must override the ambient PATH absolutely
--suffix PATH : "/run/current-system/sw/bin" # as a fallback location for NixOS.
This migration has not started yet.
Status of the migration
How to help
- Review open PRs if they affect your subsystems
- Fix your own package set. If a package you maintain use
systemd, consider migrating tosystemdLibsor late binding. - Help testing the packages like the case with
podmanor the logging subsystem which showed thatsystemdLibsneededwithCompression = true;to avoid breaking loggers.
Next steps
- Land on a solution for
udevCheckHook— we may be looking at removing it or replacing it with a separate udev parser (lighter in dependencies) - Complete the migration and ensure that things are working as intended wrt to the above described workflow (no mass rebuild events, no broken programs, etc.)
- Remove
pkgs.systemdor keep it for very peculiar cases - Switch by default to kernel-bound system on NixOS.
- Examine the case of
systemdMinimalwhich NixOS systemd maintainers would also like to get rid of, it is meant as a tool to break off dependency cycles with LVM2 and cryptsetup.