aboutsummaryrefslogtreecommitdiff
path: root/flakes/files-watcher/flake.nix
blob: 29ea428dba533a64b88f1cf73dd0ccc32f164954 (plain) (blame)
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
{
  description = "Module to watch fo file changes to force restart systemd service";
  outputs = { self }: {
    nixosModule = { config, lib, pkgs, ... }: let cfg = config.services.filesWatcher; in with lib; {
      options = {
        services.filesWatcher = with lib.types; mkOption {
          default = {};
          description = ''
            Files to watch and trigger service reload or restart of service
            when changed.
          '';
          type = attrsOf (submodule {
            options = {
              restart = mkEnableOption "Restart service rather than reloading it";
              paths = mkOption {
                type = listOf str;
                description = ''
                  Paths to watch that should trigger a reload of the
                  service
                '';
              };
              waitTime = mkOption {
                type = int;
                default = 5;
                description = ''
                  Time to wait before reloading/restarting the service.
                  Set 0 to not wait.
                '';
              };
            };
          });
        };
      };

      config = {
        systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair "${name}Watcher" {
          description = "${name} reloader";
          after = [ "network.target" ];
          script = let
            action = if icfg.restart then "restart" else "reload";
          in ''
            # Service may be stopped during file modification (e.g. activationScripts)
            if ${pkgs.systemd}/bin/systemctl --quiet is-active ${name}.service; then
              ${pkgs.coreutils}/bin/sleep ${toString icfg.waitTime}
              ${pkgs.systemd}/bin/systemctl ${action} ${name}.service
            fi
          '';
          serviceConfig.Type = "oneshot";
        }) cfg;

        systemd.paths = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair "${name}Watcher" {
          wantedBy = [ "multi-user.target" ];
          pathConfig.PathChanged = icfg.paths;
        }) cfg;
      };
    };
  };
}