hjem/collection: add systemd module

This adds a systemd module locally, allowing to define your own services
at hjem level rather than systemd level.
This commit is contained in:
Anthony Rodriguez 2025-02-15 00:13:58 +01:00
parent 174b6251e8
commit d11fb3a0e2
Signed by: nezia
SSH key fingerprint: SHA256:R/ue1eTzTHUoo77lJD/3fSUsyL4AwvcHImU5BAZai+8
2 changed files with 76 additions and 14 deletions

View file

@ -270,22 +270,27 @@ in {
}
'';
};
};
systemd.user.services.waybar = {
description = "Highly customizable Wayland bar for Sway and Wlroots based compositors.";
documentation = ["https://github.com/Alexays/Waybar/wiki/"];
after = ["graphical-session.target"];
partOf = ["graphical-session.target"];
requisite = ["graphical-session.target"];
wantedBy = ["graphical-session.target"];
systemd.services.waybar.settings = {
Unit = {
Description = "Highly customizable Wayland bar for Sway and Wlroots based compositors.";
Documentation = ["https://github.com/Alexays/Waybar/wiki/"];
After = ["graphical-session.target"];
PartOf = ["graphical-session.target"];
Requisite = ["graphical-session.target"];
X-Reload-Triggers = ["${config.hjem.users.${username}.files.".config/waybar/config".text}"];
};
reloadTriggers = ["${config.hjem.users.${username}.files.".config/waybar/config".text}"];
serviceConfig = {
ExecStart = "${pkgs.waybar}/bin/waybar";
ExecReload = "kill -SIGUSR2 $MAINPID";
Restart = "on-failure";
Slice = "app-graphical.slice";
Service = {
ExecStart = "${pkgs.waybar}/bin/waybar";
ExecReload = "kill -SIGUSR2 $MAINPID";
Restart = "on-failure";
Slice = "app-graphical.slice";
};
Install = {
WantedBy = ["graphical-session.target"];
};
};
};
};

View file

@ -0,0 +1,57 @@
{
lib,
config,
...
}: let
inherit (builtins) toString;
inherit (lib.attrsets) nameValuePair mapAttrs';
inherit (lib.modules) mkIf;
inherit (lib.options) mkOption;
inherit (lib.types) attrs attrsOf submodule;
inherit (lib.trivial) isBool boolToString;
inherit (lib.generators) toINI;
cfg = config.systemd;
toSystemdUnitFiles = services: let
toSystemdUnit = arg:
toINI {
listsAsDuplicateKeys = true;
mkKeyValue = key: value: let
value' =
if isBool value
then boolToString value
else toString value;
in "${key}=${value'}";
}
arg;
in
mapAttrs' (name: service:
nameValuePair ".config/systemd/user/${name}.service" {text = toSystemdUnit service.settings;})
services;
in {
options.systemd = {
services = mkOption {
type = attrsOf (submodule {
options = {
settings = mkOption {
type = attrs;
default = {};
description = ''
The configuration of this unit. Each attribute in this set specifies an option
(documentation for available options can be found [here](https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html)).
'';
};
};
});
default = {};
description = ''
Definition of systemd user service units.
'';
};
};
config = mkIf (cfg.services != {}) {
files = toSystemdUnitFiles cfg.services;
};
}