diff options
| author | aristote <quentin.aristote@irif.fr> | 2025-11-21 09:49:24 +0100 |
|---|---|---|
| committer | aristote <quentin.aristote@irif.fr> | 2025-11-21 10:15:47 +0100 |
| commit | c032f4c09c83feb933d520ccce80e70a1516ca35 (patch) | |
| tree | 6c4a861632f3979c9c287b5983a36776317a5932 /pkgs/lib/services | |
| parent | 4d3eeda464341243be3fc6c3fbb4a5f1d0ead906 (diff) | |
home: check network before daily services
Diffstat (limited to 'pkgs/lib/services')
| -rw-r--r-- | pkgs/lib/services/default.nix | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/pkgs/lib/services/default.nix b/pkgs/lib/services/default.nix new file mode 100644 index 0000000..148126c --- /dev/null +++ b/pkgs/lib/services/default.nix @@ -0,0 +1,78 @@ +{ lib, pkgs }: + +let + checkNetwork = + hosts: + let + pkg = pkgs.writeShellApplication { + name = "check-network"; + runtimeInputs = [ pkgs.unixtools.ping ]; + text = '' + (${lib.concatMapStringsSep " && " (host: "ping -c 1 ${host}") hosts}) || kill -s SIGUSR1 $$ + ''; + }; + in + "${pkg}/bin/check-network"; +in +{ + home.serviceWithTimer = + name: + { + Unit, + Service, + # Timer, + Install, + ... + }@config: + { + services.${name} = { inherit (config) Unit Service; }; + timers.${name} = { + inherit (config) Unit Install; + Timer = config.Timer // { + Unit = "${name}.service"; + }; + }; + }; + + home.checkNetwork = + { + hosts, + restart ? true, + }: + { + # Check network connectivity + Unit = { + StartLimitIntervalSec = 300; + StartLimitBurst = 5; + }; + Service = lib.mkMerge [ + { + ExecStartPre = checkNetwork hosts; + } + (lib.mkIf restart { + Restart = "on-abort"; + RestartSec = 30; + RestartMode = "direct"; # dependent units will not fail + }) + ]; + }; + + checkNetwork = + { + hosts, + restart ? true, + }: + { + # Check network connectivity + preStart = checkNetwork hosts; + unitConfig = { + StartLimitIntervalSec = 300; + StartLimitBurst = 5; + }; + serviceConfig = lib.mkIf restart { + Restart = "on-abort"; + RestartSec = 30; + RestartMode = "direct"; # dependent units will not fail + }; + }; +} |
