From e2b65c023983cd579e1f9eb050504fd5959ecb41 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Tue, 3 Dec 2024 09:30:45 +0100 Subject: [PATCH 1/2] nixos/tzupdate: make enabled module actually be enabled Without this fix, when setting `services.tzupdate.enable = true`, the service would never run automatically. Now, it's actually enabled in systemd and it actually gets executed. Still, it could be improved with a timer as explained in https://github.com/NixOS/nixpkgs/issues/127984#issuecomment-2512059143, but this makes it at least work out of the box when rebooting the system. --- nixos/modules/services/misc/tzupdate.nix | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nixos/modules/services/misc/tzupdate.nix b/nixos/modules/services/misc/tzupdate.nix index 18fee37ddd1b..65a6f7d72098 100644 --- a/nixos/modules/services/misc/tzupdate.nix +++ b/nixos/modules/services/misc/tzupdate.nix @@ -26,11 +26,12 @@ in # zone, which is better than silently overriding it. time.timeZone = null; - # We provide a one-shot service which can be manually run. We could - # provide a service that runs on startup, but it's tricky to get - # a service to run after you have *internet* access. + # We provide a one-shot service that runs at startup once network + # interfaces are up, but we can’t ensure we actually have Internet access + # at that point. It can also be run manually with `systemctl start tzupdate`. systemd.services.tzupdate = { description = "tzupdate timezone update service"; + wantedBy = [ "multi-user.target" ]; wants = [ "network-online.target" ]; after = [ "network-online.target" ]; script = '' From a6c31c856b44058079de808793f762aca38dadca Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Mon, 16 Dec 2024 11:22:00 +0000 Subject: [PATCH 2/2] nixos/tzupdate: add timer and package options --- nixos/modules/services/misc/tzupdate.nix | 32 +++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/misc/tzupdate.nix b/nixos/modules/services/misc/tzupdate.nix index 65a6f7d72098..454b47620e11 100644 --- a/nixos/modules/services/misc/tzupdate.nix +++ b/nixos/modules/services/misc/tzupdate.nix @@ -18,6 +18,25 @@ in update the timezone. ''; }; + + package = lib.mkPackageOption pkgs "tzupdate" { }; + + timer.enable = lib.mkOption { + type = lib.types.bool; + default = true; + description = '' + Enable the tzupdate timer to update the timezone automatically. + ''; + }; + + timer.interval = lib.mkOption { + type = lib.types.str; + default = "hourly"; + description = '' + The interval at which the tzupdate timer should run. See + {manpage}`systemd.time(7)` to understand the format. + ''; + }; }; config = lib.mkIf cfg.enable { @@ -35,7 +54,7 @@ in wants = [ "network-online.target" ]; after = [ "network-online.target" ]; script = '' - timezone="$(${lib.getExe pkgs.tzupdate} --print-only)" + timezone="$(${lib.getExe cfg.package} --print-only)" if [[ -n "$timezone" ]]; then echo "Setting timezone to '$timezone'" timedatectl set-timezone "$timezone" @@ -46,6 +65,17 @@ in Type = "oneshot"; }; }; + + systemd.timers.tzupdate = { + enable = cfg.timer.enable; + interval = cfg.timer.interval; + timerConfig = { + OnStartupSec = "30s"; + OnCalendar = cfg.timer.interval; + Persistent = true; + }; + wantedBy = [ "timers.target" ]; + }; }; meta.maintainers = with lib.maintainers; [ doronbehar ];