1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.tzupdate;
9in
10{
11 options.services.tzupdate = {
12 enable = lib.mkOption {
13 type = lib.types.bool;
14 default = false;
15 description = ''
16 Enable the tzupdate timezone updating service. This provides
17 a one-shot service which can be activated with systemctl to
18 update the timezone.
19 '';
20 };
21
22 package = lib.mkPackageOption pkgs "tzupdate" { };
23
24 timer.enable = lib.mkOption {
25 type = lib.types.bool;
26 default = true;
27 description = ''
28 Enable the tzupdate timer to update the timezone automatically.
29 '';
30 };
31
32 timer.interval = lib.mkOption {
33 type = lib.types.str;
34 default = "hourly";
35 description = ''
36 The interval at which the tzupdate timer should run. See
37 {manpage}`systemd.time(7)` to understand the format.
38 '';
39 };
40 };
41
42 config = lib.mkIf cfg.enable {
43 # We need to have imperative time zone management for this to work.
44 # This will give users an error if they have set an explicit time
45 # zone, which is better than silently overriding it.
46 time.timeZone = null;
47
48 # We provide a one-shot service that runs at startup once network
49 # interfaces are up, but we can’t ensure we actually have Internet access
50 # at that point. It can also be run manually with `systemctl start tzupdate`.
51 systemd.services.tzupdate = {
52 description = "tzupdate timezone update service";
53 wantedBy = [ "multi-user.target" ];
54 wants = [ "network-online.target" ];
55 after = [ "network-online.target" ];
56 script = ''
57 timezone="$(${lib.getExe cfg.package} --print-only)"
58 if [[ -n "$timezone" ]]; then
59 echo "Setting timezone to '$timezone'"
60 timedatectl set-timezone "$timezone"
61 fi
62 '';
63
64 serviceConfig = {
65 Type = "oneshot";
66 };
67 };
68
69 systemd.timers.tzupdate = {
70 enable = cfg.timer.enable;
71 timerConfig = {
72 OnStartupSec = "30s";
73 OnCalendar = cfg.timer.interval;
74 Persistent = true;
75 };
76 wantedBy = [ "timers.target" ];
77 };
78 };
79
80 meta.maintainers = with lib.maintainers; [ doronbehar ];
81}