How does Nix manage SystemD modules on a non-NixOS

nixsystemd

Suppose I use nix-env to install a package that uses SystemD, on a Ubuntu host. What needs to be done to make Ubuntu's SystemD aware of the SystemD modules that come from Nix packages?

Let's try finding the Nix installed .service files, and symlinking them from /lib/systemd/system/, for the Apache Kafka package.

nix-env -i apache-kafka
sudo systemctl start apache-kafka # Failed to start apache-kafka.service: Unit apache-kafka.service not found.
sudo updatedb && locate apache-kafka.service # No dice
locate kafka | grep service # Just a bunch of `.nix` files

Here, I'm guessing the service name based on the service configuration's name in the Nix package definition. I haven't been able to find any documentation describing how and where that config becomes a SystemD service file.

When that didn't work, I started really digging around, on the assumption that somewhere, Nix must have created this service file. But now, I am starting to doubt that it exists. So, are SystemD modules installed by the Nix package manager supposed to work outside of NixOS, and if so, how do we make them work?

Best Answer

On NixOS it is possible to use environment.systemPackages = [ package ]; to install package's systemd units into system. Proof

Units in /nix/store/hash-package/lib/systemd/system are copied to /run/current-system/sw/lib/systemd/system, which is then used by systemd as an extra service dir.

So, if you want to use service units when installing package as root, be sure the path /root/.nix-profile/lib/systemd/system is used by systemd in addition to /etc/systemd/system. Also, be sure the derivation provides the units.

Completely untested, because I'm on NixOS

Related Question