Handling systemd unit files inside mkDerivation / ignoring them

Hi Everybody,

I noticed for example in Syncthing derivation:

substitute cmd/strelaysrv/etc/linux-systemd/strelaysrv.service \
           $out/lib/systemd/system/strelaysrv.service \
           --replace /usr/bin/strelaysrv $out/bin/strelaysrv

That systemd Service file is moved to $out/lib/systemd/system/.

============

If I want to package an app that has systemd files, should I:

a) Move the systemd files to $out and then just add some minimal configuration like in the Syncthing NixOS module.

What is the minimal config. in this case?

b) Ignore app.service files provided by the app author and create a new systemd service in NixOS module by looking at app.service to get understanding how the app should run.

c) Something else?

Thank you.

If I want to package an app that has systemd files, should I:

  1. Move the systemd files to $out and then just add some minimal configuration like in the Syncthing NixOS module.
  1. move the unit file(s) into $out/lib/systemd/system (or user)
  2. patch the binary paths

What is the minimal config. in this case?

{
  systemd.packages = [ pkgs.your-package]; # this pulls in the .service file

  systemd.services = {
    your-unit.wantedBy = [ "multi-user.target"]; # this makes it run
  };
}

2 Likes

I was wondering how this is linked. Thank you!

Yes, I like the example the Syncthing derivation - using substitute so the result is:

[Unit]
Description=Restart Syncthing after resume
Documentation=man:syncthing(1)
After=sleep.target

[Service]
Type=oneshot
ExecStart=-/nix/store/pi05gf5p4p1p1j41p12sha27zgjfs767-procps-3.3.17/bin/pkill -HUP -x syncthing

[Install]
WantedBy=sleep.target

Thank you!

systemd.packages = [ pkgs.your-package]

I was wondering how this is linked. Thank you!

Keep in mind that this is not enough to actually make it run. You will need to specify something like regardless of what WantedBy says in the file.

{
  systemd.services.your-service.wantedBy = [ "multi-user.target" ];
}

For a timer, it would be something like this:

{
  systemd.timers.your-service.wantedBy = [ "timers.target" ];
}

Thank you. I actually tried

systemd.packages = [ mypackage ];
systemd.services.somethinginmypackage.enable = true;

And it didn’t work so I realized that wantedBy is needed. Thank you.