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