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 = { systemd.services.waybar.settings = {
description = "Highly customizable Wayland bar for Sway and Wlroots based compositors."; Unit = {
documentation = ["https://github.com/Alexays/Waybar/wiki/"]; Description = "Highly customizable Wayland bar for Sway and Wlroots based compositors.";
after = ["graphical-session.target"]; Documentation = ["https://github.com/Alexays/Waybar/wiki/"];
partOf = ["graphical-session.target"]; After = ["graphical-session.target"];
requisite = ["graphical-session.target"]; PartOf = ["graphical-session.target"];
wantedBy = ["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}"]; Service = {
serviceConfig = { ExecStart = "${pkgs.waybar}/bin/waybar";
ExecStart = "${pkgs.waybar}/bin/waybar"; ExecReload = "kill -SIGUSR2 $MAINPID";
ExecReload = "kill -SIGUSR2 $MAINPID"; Restart = "on-failure";
Restart = "on-failure"; Slice = "app-graphical.slice";
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;
};
}