1{ config, lib, ... }:
2
3with lib;
4
5{
6
7 options = {
8
9 services.timesyncd = {
10 enable = mkOption {
11 default = !config.boot.isContainer;
12 type = types.bool;
13 description = ''
14 Enables the systemd NTP client daemon.
15 '';
16 };
17 servers = mkOption {
18 default = config.networking.timeServers;
19 type = types.listOf types.str;
20 description = ''
21 The set of NTP servers from which to synchronise.
22 '';
23 };
24 extraConfig = mkOption {
25 default = "";
26 type = types.lines;
27 example = ''
28 PollIntervalMaxSec=180
29 '';
30 description = ''
31 Extra config options for systemd-timesyncd. See
32 <link xlink:href="https://www.freedesktop.org/software/systemd/man/timesyncd.conf.html">
33 timesyncd.conf(5)</link> for available options.
34 '';
35 };
36 };
37 };
38
39 config = mkIf config.services.timesyncd.enable {
40
41 systemd.additionalUpstreamSystemUnits = [ "systemd-timesyncd.service" ];
42
43 systemd.services.systemd-timesyncd = {
44 wantedBy = [ "sysinit.target" ];
45 aliases = [ "dbus-org.freedesktop.timesync1.service" ];
46 restartTriggers = [ config.environment.etc."systemd/timesyncd.conf".source ];
47 };
48
49 environment.etc."systemd/timesyncd.conf".text = ''
50 [Time]
51 NTP=${concatStringsSep " " config.services.timesyncd.servers}
52 ${config.services.timesyncd.extraConfig}
53 '';
54
55 users.users.systemd-timesync = {
56 uid = config.ids.uids.systemd-timesync;
57 group = "systemd-timesync";
58 };
59 users.groups.systemd-timesync.gid = config.ids.gids.systemd-timesync;
60
61 system.activationScripts.systemd-timesyncd-migration = mkIf (versionOlder config.system.stateVersion "19.09") ''
62 # workaround an issue of systemd-timesyncd not starting due to upstream systemd reverting their dynamic users changes
63 # - https://github.com/NixOS/nixpkgs/pull/61321#issuecomment-492423742
64 # - https://github.com/systemd/systemd/issues/12131
65 if [ -L /var/lib/systemd/timesync ]; then
66 rm /var/lib/systemd/timesync
67 mv /var/lib/private/systemd/timesync /var/lib/systemd/timesync
68 fi
69 '';
70 };
71
72}