|
8 | 8 |
|
9 | 9 | serviceName = builtins.unsafeDiscardStringContext (config.binName or "unknown"); |
10 | 10 |
|
11 | | - mkService = |
| 11 | + mkNixosEval = |
12 | 12 | type: |
13 | 13 | let |
14 | 14 | nixosPath = |
|
23 | 23 | "systemd" |
24 | 24 | "services" |
25 | 25 | ]; |
| 26 | + in |
| 27 | + import (config.pkgs.path + "/nixos/lib/eval-config.nix") { |
| 28 | + inherit lib; |
| 29 | + system = null; |
| 30 | + modules = [ |
| 31 | + { config.nixpkgs.hostPlatform = config.pkgs.stdenv.hostPlatform.system; } |
| 32 | + { config.systemd.globalEnvironment = lib.mkForce { }; } |
| 33 | + { config = lib.setAttrByPath nixosPath { ${serviceName} = cfg; }; } |
| 34 | + ]; |
| 35 | + }; |
26 | 36 |
|
27 | | - nixos = import (config.pkgs.path + "/nixos/lib/eval-config.nix") { |
28 | | - inherit lib; |
29 | | - system = null; |
30 | | - modules = [ |
31 | | - { config.nixpkgs.hostPlatform = config.pkgs.stdenv.hostPlatform.system; } |
32 | | - { config.systemd.globalEnvironment = lib.mkForce { }; } |
33 | | - { config = lib.setAttrByPath nixosPath { ${serviceName} = cfg; }; } |
34 | | - ]; |
35 | | - }; |
36 | | - |
37 | | - utils = import (config.pkgs.path + "/nixos/lib/utils.nix") { |
38 | | - inherit lib; |
39 | | - inherit (config) pkgs; |
40 | | - inherit (nixos) config; |
41 | | - }; |
42 | | - |
43 | | - evaluated = lib.getAttrFromPath nixosPath nixos.config; |
44 | | - unit = utils.systemdUtils.lib.serviceToUnit evaluated.${serviceName}; |
| 37 | + mkUtils = |
| 38 | + nixos: |
| 39 | + import (config.pkgs.path + "/nixos/lib/utils.nix") { |
| 40 | + inherit lib; |
| 41 | + inherit (config) pkgs; |
| 42 | + inherit (nixos) config; |
| 43 | + }; |
| 44 | + |
| 45 | + mkOutputs = |
| 46 | + type: |
| 47 | + let |
| 48 | + nixos = mkNixosEval type; |
| 49 | + utils = mkUtils nixos; |
| 50 | + |
| 51 | + servicePath = |
| 52 | + if type == "user" then |
| 53 | + [ |
| 54 | + "systemd" |
| 55 | + "user" |
| 56 | + "services" |
| 57 | + ] |
| 58 | + else |
| 59 | + [ |
| 60 | + "systemd" |
| 61 | + "services" |
| 62 | + ]; |
| 63 | + |
| 64 | + timerPath = |
| 65 | + if type == "user" then |
| 66 | + [ |
| 67 | + "systemd" |
| 68 | + "user" |
| 69 | + "timers" |
| 70 | + ] |
| 71 | + else |
| 72 | + [ |
| 73 | + "systemd" |
| 74 | + "timers" |
| 75 | + ]; |
45 | 76 |
|
46 | 77 | unitDir = if type == "user" then "systemd/user" else "systemd/system"; |
| 78 | + |
| 79 | + evaluatedService = (lib.getAttrFromPath servicePath nixos.config).${serviceName}; |
| 80 | + serviceUnit = utils.systemdUtils.lib.serviceToUnit evaluatedService; |
| 81 | + |
| 82 | + evaluatedTimers = lib.getAttrFromPath timerPath nixos.config; |
| 83 | + hasTimer = evaluatedTimers ? ${serviceName}; |
| 84 | + timerUnit = utils.systemdUtils.lib.timerToUnit evaluatedTimers.${serviceName}; |
47 | 85 | in |
48 | | - config.pkgs.writeTextDir "${unitDir}/${serviceName}.service" unit.text; |
| 86 | + { |
| 87 | + service = config.pkgs.writeTextDir "${unitDir}/${serviceName}.service" serviceUnit.text; |
| 88 | + timer = |
| 89 | + if hasTimer then |
| 90 | + config.pkgs.writeTextDir "${unitDir}/${serviceName}.timer" timerUnit.text |
| 91 | + else |
| 92 | + null; |
| 93 | + }; |
| 94 | + |
| 95 | + userOutputs = mkOutputs "user"; |
| 96 | + systemOutputs = mkOutputs "system"; |
49 | 97 | in |
50 | 98 | { |
51 | 99 | _file = "lib/modules/systemd.nix"; |
|
61 | 109 | ExecStart, Environment, PATH, preStart and postStop are set from the |
62 | 110 | wrapper by default. |
63 | 111 |
|
| 112 | + If startAt is set, a corresponding .timer unit is generated alongside |
| 113 | + the service. |
| 114 | +
|
64 | 115 | The generated unit files are available at outputs.systemd-user and |
65 | 116 | outputs.systemd-system. |
66 | 117 | ''; |
|
80 | 131 | options.outputs.systemd-user = lib.mkOption { |
81 | 132 | type = lib.types.package; |
82 | 133 | readOnly = true; |
83 | | - description = "The generated systemd user service file."; |
84 | | - default = mkService "user"; |
| 134 | + description = "The generated systemd user service file (and timer if startAt is set)."; |
| 135 | + default = |
| 136 | + if userOutputs.timer != null then |
| 137 | + config.pkgs.symlinkJoin { |
| 138 | + name = "${serviceName}-user-units"; |
| 139 | + paths = [ |
| 140 | + userOutputs.service |
| 141 | + userOutputs.timer |
| 142 | + ]; |
| 143 | + } |
| 144 | + else |
| 145 | + userOutputs.service; |
85 | 146 | }; |
86 | 147 |
|
87 | 148 | options.outputs.systemd-system = lib.mkOption { |
88 | 149 | type = lib.types.package; |
89 | 150 | readOnly = true; |
90 | | - description = "The generated systemd system service file."; |
91 | | - default = mkService "system"; |
| 151 | + description = "The generated systemd system service file (and timer if startAt is set)."; |
| 152 | + default = |
| 153 | + if systemOutputs.timer != null then |
| 154 | + config.pkgs.symlinkJoin { |
| 155 | + name = "${serviceName}-system-units"; |
| 156 | + paths = [ |
| 157 | + systemOutputs.service |
| 158 | + systemOutputs.timer |
| 159 | + ]; |
| 160 | + } |
| 161 | + else |
| 162 | + systemOutputs.service; |
92 | 163 | }; |
93 | 164 | } |
0 commit comments