From d11fb3a0e226c3c4a56b974d21e593a4dcd9d3c4 Mon Sep 17 00:00:00 2001 From: Anthony Rodriguez Date: Sat, 15 Feb 2025 00:13:58 +0100 Subject: [PATCH] hjem/collection: add systemd module This adds a systemd module locally, allowing to define your own services at hjem level rather than systemd level. --- modules/programs/waybar.nix | 33 ++++++----- .../hjem/collection/systemd-services.nix | 57 +++++++++++++++++++ 2 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 shared/modules/hjem/collection/systemd-services.nix diff --git a/modules/programs/waybar.nix b/modules/programs/waybar.nix index 666d0cc..62d599c 100644 --- a/modules/programs/waybar.nix +++ b/modules/programs/waybar.nix @@ -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"]; + }; }; }; }; diff --git a/shared/modules/hjem/collection/systemd-services.nix b/shared/modules/hjem/collection/systemd-services.nix new file mode 100644 index 0000000..4323920 --- /dev/null +++ b/shared/modules/hjem/collection/systemd-services.nix @@ -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; + }; +}