]>
Commit | Line | Data |
---|---|---|
17f6eae9 IB |
1 | { lib, config, pkgs, ... }: |
2 | with lib; | |
3 | let | |
4 | cfg = config.services.filesWatcher; | |
5 | in | |
6 | { | |
7 | options = { | |
8 | services.filesWatcher = with types; mkOption { | |
9 | default = {}; | |
10 | description = '' | |
11 | Files to watch and trigger service reload or restart of service | |
12 | when changed. | |
13 | ''; | |
14 | type = attrsOf (submodule { | |
15 | options = { | |
16 | restart = mkEnableOption "Restart service rather than reloading it"; | |
17 | paths = mkOption { | |
18 | type = listOf str; | |
19 | description = '' | |
20 | Paths to watch that should trigger a reload of the | |
21 | service | |
22 | ''; | |
23 | }; | |
24 | waitTime = mkOption { | |
25 | type = int; | |
26 | default = 5; | |
27 | description = '' | |
28 | Time to wait before reloading/restarting the service. | |
29 | Set 0 to not wait. | |
30 | ''; | |
31 | }; | |
32 | }; | |
33 | }); | |
34 | }; | |
35 | }; | |
36 | ||
37 | config.systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair | |
38 | "${name}Watcher" { | |
39 | description = "${name} reloader"; | |
40 | after = [ "network.target" ]; | |
41 | script = let | |
42 | action = if icfg.restart then "restart" else "reload"; | |
43 | in '' | |
44 | # Service may be stopped during file modification (e.g. activationScripts) | |
45 | if ${pkgs.systemd}/bin/systemctl --quiet is-active ${name}.service; then | |
46 | ${pkgs.coreutils}/bin/sleep ${toString icfg.waitTime} | |
47 | ${pkgs.systemd}/bin/systemctl ${action} ${name}.service | |
48 | fi | |
49 | ''; | |
50 | serviceConfig = { | |
51 | Type = "oneshot"; | |
52 | }; | |
53 | } | |
54 | ) cfg; | |
55 | config.systemd.paths = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair | |
56 | "${name}Watcher" { | |
57 | wantedBy = [ "multi-user.target" ]; | |
58 | pathConfig.PathChanged = icfg.paths; | |
59 | } | |
60 | ) cfg; | |
61 | } |