summaryrefslogtreecommitdiff
path: root/modules/tzupdate.nix
blob: 6465d33f1107da496c9b8acb0487bcc235fbf84d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.tzupdate;
in
{
  disabledModules = [ "services/misc/tzupdate.nix" ];

  options.services.tzupdate = {
    enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Enable the tzupdate timezone updating service. This provides
        a one-shot service which can be activated with systemctl to
        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 {
    # We need to have imperative time zone management for this to work.
    # This will give users an error if they have set an explicit time
    # zone, which is better than silently overriding it.
    time.timeZone = null;

    # 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 = ''
        timezone="$(${lib.getExe cfg.package} --print-only)"
        if [[ -n "$timezone" ]]; then
          echo "Setting timezone to '$timezone'"
          ${lib.getExe' config.systemd.package "timedatectl"} set-timezone "$timezone"
        fi
      '';

      serviceConfig = {
        Type = "oneshot";
      };
    };

    systemd.timers.tzupdate = {
      enable = cfg.timer.enable;
      timerConfig = {
        OnStartupSec = "30s";
        OnCalendar = cfg.timer.interval;
        Persistent = true;
      };
      wantedBy = [ "timers.target" ];
    };
  };
}