Skip to content

Commit

Permalink
modules/nixos: add systemd services
Browse files Browse the repository at this point in the history
This adds systemd services configuration through hjem, under
`users.<username>.systemd.services`.
  • Loading branch information
nezia1 committed Feb 14, 2025
1 parent 48cfa21 commit 6be5d7a
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions modules/nixos/systemd/services.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
lib,
config,
...
}: let
inherit (builtins) toString;
inherit (lib.attrsets) nameValuePair mapAttrs';
inherit (lib.modules) mkIf;
inherit (lib.options) mkOption;
inherit (lib.types) attrs attrsOf submoduleWith;
inherit (lib.trivial) isBool boolToString;
inherit (lib.lists) singleton;
inherit (lib.generators) toINI;

cfg = config.systemd.services;

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 (submoduleWith {
modules = singleton {
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 != {}) {
files = builtins.trace (toSystemdUnitFiles cfg.services) toSystemdUnitFiles cfg.services;
};
}

0 comments on commit 6be5d7a

Please sign in to comment.