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 | |
| parent | 4d3eeda464341243be3fc6c3fbb4a5f1d0ead906 (diff) | |
home: check network before daily services
| -rw-r--r-- | modules/home-manager/personal/programs/devenv.nix | 48 | ||||
| -rw-r--r-- | modules/home-manager/personal/programs/emacs.nix | 14 | ||||
| -rw-r--r-- | modules/nixos/personal/system.nix | 26 | ||||
| -rw-r--r-- | pkgs/default.nix | 2 | ||||
| -rw-r--r-- | pkgs/lib/default.nix | 4 | ||||
| -rw-r--r-- | pkgs/lib/home-manager/default.nix | 22 | ||||
| -rw-r--r-- | pkgs/lib/services/default.nix | 78 |
7 files changed, 127 insertions, 67 deletions
diff --git a/modules/home-manager/personal/programs/devenv.nix b/modules/home-manager/personal/programs/devenv.nix index 62f2cc2..225425b 100644 --- a/modules/home-manager/personal/programs/devenv.nix +++ b/modules/home-manager/personal/programs/devenv.nix @@ -56,25 +56,33 @@ in ]; home.packages = lib.optional importedDevenv pkgs.devenv; - systemd.user = pkgs.personal.lib.homeManager.serviceWithTimer "devenv-update" { - Unit = { - Description = "Update devenv shells"; - After = [ - "network-online.target" - ]; - }; - Service = { - Type = "oneshot"; - WorkingDirectory = "${config.home.homeDirectory}"; - ExecStart = "${devenvUpdateScript}/bin/devenv-update"; - }; - Timer = { - Persistent = true; - OnCalendar = "daily"; - }; - Install = { - WantedBy = [ "default.target" ]; - }; - }; + systemd.user = lib.mkMerge [ + (pkgs.lib.personal.services.home.serviceWithTimer "devenv-update" { + Unit = { + Description = "Update devenv shells"; + After = [ + "network-online.target" + ]; + }; + Service = { + Type = "oneshot"; + WorkingDirectory = "${config.home.homeDirectory}"; + ExecStart = "${devenvUpdateScript}/bin/devenv-update"; + }; + Timer = { + Persistent = true; + OnCalendar = "daily"; + }; + Install = { + WantedBy = [ "default.target" ]; + }; + }) + ({ + services.devenv-update = pkgs.lib.personal.services.home.checkNetwork { + hosts = [ "github.com" ]; + restart = true; + }; + }) + ]; }; } diff --git a/modules/home-manager/personal/programs/emacs.nix b/modules/home-manager/personal/programs/emacs.nix index 3b00555..5b579c4 100644 --- a/modules/home-manager/personal/programs/emacs.nix +++ b/modules/home-manager/personal/programs/emacs.nix @@ -75,8 +75,8 @@ in home.file.".spacemacs.d/init.el".source = lib.mkDefault config.personal.home.dotfiles.spacemacs; # service to update spacemacs - systemd.user = ( - pkgs.personal.lib.homeManager.serviceWithTimer "spacemacs-update" { + systemd.user = lib.mkMerge [ + (pkgs.lib.personal.services.home.serviceWithTimer "spacemacs-update" { Unit = { Description = "Update Spacemacs by pulling the develop branch"; After = [ @@ -96,7 +96,13 @@ in Install = { WantedBy = [ "default.target" ]; }; - } - ); + }) + ({ + services.spacemacs-update = pkgs.lib.personal.services.home.checkNetwork { + hosts = [ "github.com" ]; + restart = true; + }; + }) + ]; }; } diff --git a/modules/nixos/personal/system.nix b/modules/nixos/personal/system.nix index 3337b96..559b3fc 100644 --- a/modules/nixos/personal/system.nix +++ b/modules/nixos/personal/system.nix @@ -68,29 +68,16 @@ in remoteBuilder = with cfgRemote.builder; "${hostName}.${domain}"; - checkNetwork = { - path = [ pkgs.unixtools.ping ]; - # Check network connectivity - preStart = "(${ - lib.concatMapStringsSep " && " (host: "ping -c 1 ${host}") cfg.autoUpgrade.checkHosts - }) || kill -s SIGUSR1 $$"; - unitConfig = { - StartLimitIntervalSec = 300; - StartLimitBurst = 5; - }; - serviceConfig = lib.mkIf (!config.personal.monitoring.enable) { - Restart = "on-abort"; - RestartSec = 30; - RestartMode = "direct"; # dependent units will not fail - }; - }; in lib.mkMerge [ (lib.mkIf hasFlake { system.autoUpgrade.flake = cfg.flake; systemd.services.flake-update = lib.mkIf hasFlakeInputs ( lib.mkMerge [ - checkNetwork + (pkgs.lib.personal.services.checkNetwork { + hosts = cfg.autoUpgrade.checkHosts; + restart = !config.personal.monitoring.enable; + }) { description = "Update flake inputs"; serviceConfig.Type = "oneshot"; @@ -146,7 +133,10 @@ in flags = lib.optional (!hasFlake) "--upgrade-all"; }; systemd.services.nixos-upgrade = lib.mkMerge [ - checkNetwork + (pkgs.lib.personal.services.checkNetwork { + hosts = cfg.autoUpgrade.checkHosts; + restart = !config.personal.monitoring.enable; + }) { path = lib.optional reboot pkgs.coreutils diff --git a/pkgs/default.nix b/pkgs/default.nix index 2fdae90..01b3713 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -1,7 +1,7 @@ super: let self = { - lib = import ./lib { inherit (super) lib; }; + lib = import ./lib { inherit (super) lib pkgs; }; lockscreen = super.callPackage ./lockscreen { }; diff --git a/pkgs/lib/default.nix b/pkgs/lib/default.nix index 22c9e07..7b11956 100644 --- a/pkgs/lib/default.nix +++ b/pkgs/lib/default.nix @@ -1,8 +1,8 @@ -{ lib }: +{ lib, pkgs }: let self = { - homeManager = import ./home-manager { }; + services = import ./services { inherit lib pkgs; }; toUserJS = prefs: '' ${lib.concatStrings ( lib.mapAttrsToList (name: value: '' diff --git a/pkgs/lib/home-manager/default.nix b/pkgs/lib/home-manager/default.nix deleted file mode 100644 index 91149d6..0000000 --- a/pkgs/lib/home-manager/default.nix +++ /dev/null @@ -1,22 +0,0 @@ -{ }: - -{ - 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"; - }; - }; - }; -} 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 + }; + }; +} |
