diff --git a/hosts/laptop/configuration.nix b/hosts/laptop/configuration.nix index 0992291..baa3f6b 100755 --- a/hosts/laptop/configuration.nix +++ b/hosts/laptop/configuration.nix @@ -22,7 +22,8 @@ ../../hostsModules/laptop/nixos/disk.nix ../../modules/nixos/printer.nix ../../hostsModules/laptop/nixos/ollama.nix # llm config - ../../modules/nixos/mullvad.nix # vpn config + ../../modules/nixos/mullvad.nix # vpn config + ../../hostsModules/laptop/nixos/autoUpdate.nix # auto update the flakes. Handles notification via libnotify and matrix-commander-rs ]; diff --git a/hosts/laptop/home.nix b/hosts/laptop/home.nix index d23d07e..2b5d28e 100755 --- a/hosts/laptop/home.nix +++ b/hosts/laptop/home.nix @@ -71,8 +71,6 @@ # checks if matrix-commander-rs is installed and logged in (pkgs.callPackage ../../hostsModules/laptop/scripts/checkMatrix.nix {}) - # autoupdate the flake and send notification to system and to matrix - (pkgs.callPackage ../../hostsModules/laptop/scripts/autoUpdate.nix {}) (pkgs.callPackage ../../modules/scripts/weather.nix {}) (pkgs.callPackage ../../modules/scripts/wallpaper.nix {}) diff --git a/hostsModules/laptop/nixos/autoUpdate.nix b/hostsModules/laptop/nixos/autoUpdate.nix new file mode 100644 index 0000000..eb46b5d --- /dev/null +++ b/hostsModules/laptop/nixos/autoUpdate.nix @@ -0,0 +1,47 @@ +{ pkgs, ... }: + +{ + # Ensure your script is available system-wide + environment.systemPackages = [ + (pkgs.callPackage ../scripts/autoUpdate.nix {}) + ]; + users.users.tomasr = { + linger = true; # lingering is required + }; + + systemd.user.services.custom-autoupdate = { + description = "NixOS flake auto update"; + + serviceConfig = { + Type = "oneshot"; + + ExecStart = "/run/current-system/sw/bin/custom-autoupdate"; + + # safety for long rebuilds + TimeoutStartSec = "45min"; + TimeoutStopSec = "10min"; + + # avoid overlap + RemainAfterExit = true; + + # tweaks that should make the system run normally during the rebuilds + Nice = 10; + IOSchedulingClass = "best-effort"; + IOSchedulingPriority = 7; + }; + }; + + # Systemd USER timer + systemd.user.timers.custom-autoupdate = { + wantedBy = [ "timers.target" ]; + + timerConfig = { + OnCalendar = "Fri *-*-* 20:00:00"; # runs friday night. If for whatever reason something breaks. I have whole week-end to fix it. + + Persistent = true; # if it happens during shutted down + + # avoids thundering herd on boot + RandomizedDelaySec = "2h"; + }; + }; +} diff --git a/modules/nixos/autoUpdate.nix b/modules/nixos/autoUpdate.nix new file mode 100644 index 0000000..30f743f --- /dev/null +++ b/modules/nixos/autoUpdate.nix @@ -0,0 +1,62 @@ +{ pkgs, ... }: + +{ + # Ensure your script is available system-wide + environment.systemPackages = [ + (pkgs.callPackage ./custom-autoupdate.nix {}) + ]; + + # ---------------------------- + # 🔥 Enable lingering for user services (IMPORTANT) + # ---------------------------- + users.users.tomasr = { + isNormalUser = true; + + # other config... + + linger = true; # <-- declarative equivalent of `loginctl enable-linger` + }; + + # ---------------------------- + # ✅ 2. Systemd USER service + # ---------------------------- + systemd.user.services.custom-autoupdate = { + description = "NixOS flake auto update"; + + serviceConfig = { + Type = "oneshot"; + + ExecStart = "/run/current-system/sw/bin/custom-autoupdate"; + + # safety for long rebuilds + TimeoutStartSec = "45min"; + TimeoutStopSec = "10min"; + + # avoid overlap + RemainAfterExit = true; + + # 🔥 recommended reliability tweaks + Nice = 10; + IOSchedulingClass = "best-effort"; + IOSchedulingPriority = 7; + }; + }; + + # ---------------------------- + # ⏱️ 3. Systemd USER timer + # ---------------------------- + systemd.user.timers.custom-autoupdate = { + wantedBy = [ "timers.target" ]; + + timerConfig = { + OnBootSec = "10min"; # run after boot + OnUnitActiveSec = "6h"; # every 6 hours + + # 🔥 ensures missed runs after shutdown happen + Persistent = true; + + # avoids thundering herd on boot + RandomizedDelaySec = "30min"; + }; + }; +}