{ config, pkgs, ... }@all: with all; let target = "1.1.1.1"; timeoutMinutes = 5; logFile = "/var/log/stay-up.log"; in { systemd.services.wifi-watchdog = { description = "Wi-Fi Watchdog Service"; after = [ "network.target" "NetworkManager.service" ]; wantedBy = [ "multi-user.target" ]; path = with pkgs; [ iputils networkmanager coreutils ]; script = /* bash */ '' FAIL_COUNT=0 touch "${logFile}" while true; do if ping -c 1 -W 5 "${target}" > /dev/null 2>&1; then FAIL_COUNT=0 else FAIL_COUNT=$((FAIL_COUNT + 1)) if [ "$FAIL_COUNT" -ge "${toString timeoutMinutes}" ]; then echo "$(date -Iseconds): Network unreachable for ${toString timeoutMinutes} minutes. Restarting Wi-Fi." >> "${logFile}" nmcli radio wifi off sleep 5 nmcli radio wifi on FAIL_COUNT=0 fi fi sleep 60 done ''; serviceConfig = { Restart = "always"; RestartSec = "10"; User = "root"; # Root is required to execute nmcli radio commands and write to /var/log/ }; }; }