2 description = "Module to watch fo file changes to force restart systemd service";
4 nixosModule = { config, lib, pkgs, ... }: let cfg = config.services.filesWatcher; in with lib; {
6 services.filesWatcher = with lib.types; mkOption {
9 Files to watch and trigger service reload or restart of service
12 type = attrsOf (submodule {
14 restart = mkEnableOption "Restart service rather than reloading it";
18 Paths to watch that should trigger a reload of the
26 Time to wait before reloading/restarting the service.
36 systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair "${name}Watcher" {
37 description = "${name} reloader";
38 after = [ "network.target" ];
40 action = if icfg.restart then "restart" else "reload";
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
48 serviceConfig.Type = "oneshot";
51 systemd.paths = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair "${name}Watcher" {
52 wantedBy = [ "multi-user.target" ];
53 pathConfig.PathChanged = icfg.paths;