~ruther/nix-tmpactivator

ref: 58dd1f391141cb0b92f9582d803e9510a13e1bbe nix-tmpactivator/modules/systemd.nix -rw-r--r-- 3.0 KiB
58dd1f39 — Rutherther feat: add self management (auto activation, deactivation) a year ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{ config, lib, utils, ... }:

let
  inherit (utils) systemdUtils;

  inherit
    (systemdUtils.lib)
    makeUnit
    generateUnits
    targetToUnit
    serviceToUnit
    sliceToUnit
    socketToUnit
    timerToUnit
    pathToUnit;

  cfg = config.systemd.user;

  wantedPaths = lib.unique (lib.flatten (lib.attrValues (lib.mapAttrs (name: conf: (builtins.map (wanted: "${wanted}.wants/${name}") (conf.wantedBy or []))) cfg.units)));
  requiredPaths = lib.unique (lib.flatten (lib.attrValues (lib.mapAttrs (name: conf: (builtins.map (required: "${required}.requires/${name}") (conf.requiredBy or []))) cfg.units)));

in {
  options = {
    systemd = {
      user = {
        units = lib.mkOption {
          default = {};
          type = systemdUtils.types.units;
        };
        services = lib.mkOption {
          default = {};
          type = systemdUtils.types.services;
        };
        slices = lib.mkOption {
          default = {};
          type = systemdUtils.types.slices;
        };
        paths = lib.mkOption {
          default = {};
          type = systemdUtils.types.paths;
        };
        sockets = lib.mkOption {
          default = {};
          type = systemdUtils.types.sockets;
        };
        targets = lib.mkOption {
          default = {};
          type = systemdUtils.types.targets;
        };
        timers = lib.mkOption {
          default = {};
          type = systemdUtils.types.timers;
        };

        unitsPackage = lib.mkOption {
          type = lib.types.package;
        };
      };

      package = lib.mkOption {
        default = "systemd";
      };
      globalEnvironment = lib.mkOption {
        default = {};
      };
    };

    a.wantedServices = lib.mkOption { type = lib.types.listOf lib.types.str; };
  };

  config = {
    systemd.user.units = with lib;
         mapAttrs' (n: v: nameValuePair "${n}.path"    (pathToUnit    n v)) cfg.paths
      // mapAttrs' (n: v: nameValuePair "${n}.service" (serviceToUnit n v)) cfg.services
      // mapAttrs' (n: v: nameValuePair "${n}.slice"   (sliceToUnit   n v)) cfg.slices
      // mapAttrs' (n: v: nameValuePair "${n}.socket"  (socketToUnit  n v)) cfg.sockets
      // mapAttrs' (n: v: nameValuePair "${n}.target"  (targetToUnit  n v)) cfg.targets
      // mapAttrs' (n: v: nameValuePair "${n}.timer"   (timerToUnit   n v)) cfg.timers;

    systemd.user.unitsPackage = generateUnits {
      type = "user";
      inherit (cfg) units;
      upstreamUnits = [];
      upstreamWants = [];
      package = "systemd";
      packages = [];
    };

    # home.file.".config/systemd/user".source = generateUnits {
    #   type = "user";
    #   inherit (cfg) units;
    # };
    #
    home.file = lib.mkMerge [
      (lib.mapAttrs' (name: conf: (lib.nameValuePair ".config/systemd/user/${name}" {
        source = "${cfg.unitsPackage}/${name}";
      })) cfg.units)

      (lib.listToAttrs (builtins.map (path:
        (lib.nameValuePair
          ".config/systemd/user/${path}"
          { source = "${cfg.unitsPackage}/${path}"; })
      ) (wantedPaths ++ requiredPaths)))
    ];
  };
}
Do not follow this link