How to use toplevel-overrides for systemd

For the sake of creating a notification system for failed systemd services I wanted to use toplevel-overrides that were introduced with systemd v244.

I tried using environment.etc."systemd/service.d/toplevel-overrides.conf" to create the necessary config file, but it error-ed out with:

> mkdir: cannot create directory '/nix/store/8xyqpq5s548w0b3xgy5ycw7qy3bifpg9-etc/etc/systemd/system/service.d': Permission denied

System info:
nixos-unstable system managed with github.com/divnix/devos

Is there any way to make this work with the existing nixos options or do we need something new?

Probably going to need something new. I believe /etc/systemd/system is the output of its own derivation, so that derivation would need to be modified.

You can add your own packages to systemd.

{ config, lib, pkgs, ... }: {

  systemd.packages = [
    (pkgs.runCommandNoCC "toplevel-overrides.conf" {
      preferLocalBuild = true;
      allowSubstitutes = false;
    } ''
      mkdir -p $out/etc/systemd/system/service.d/
      echo "# Dummy" > $out/etc/systemd/system/service.d/toplevel-overrides.conf
    '')
  ];

}

If you want to use this with template units you have to change it a bit, see Declaring instances of a generic systemd service e.g. `my-service@foo`? - #5 by hmenke

2 Likes

Thank you so much! Worked like a charm :grin:

1 Like