6 cfg = config.services.timesyncd;
12 services.timesyncd = with types; {
14 default = !config.boot.isContainer;
15 defaultText = literalExpression "!config.boot.isContainer";
18 Enables the systemd NTP client daemon.
23 type = nullOr (listOf str);
25 The set of NTP servers from which to synchronise.
27 Setting this option to an empty list will write `NTP=` to the
28 `timesyncd.conf` file as opposed to setting this option to null which
29 will remove `NTP=` entirely.
31 See man:timesyncd.conf(5) for details.
34 fallbackServers = mkOption {
35 default = config.networking.timeServers;
36 defaultText = literalExpression "config.networking.timeServers";
37 type = nullOr (listOf str);
39 The set of fallback NTP servers from which to synchronise.
41 Setting this option to an empty list will write `FallbackNTP=` to the
42 `timesyncd.conf` file as opposed to setting this option to null which
43 will remove `FallbackNTP=` entirely.
45 See man:timesyncd.conf(5) for details.
48 extraConfig = mkOption {
52 PollIntervalMaxSec=180
55 Extra config options for systemd-timesyncd. See
57 timesyncd.conf(5)](https://www.freedesktop.org/software/systemd/man/timesyncd.conf.html) for available options.
63 config = mkIf cfg.enable {
65 systemd.additionalUpstreamSystemUnits = [ "systemd-timesyncd.service" ];
67 systemd.services.systemd-timesyncd = {
68 wantedBy = [ "sysinit.target" ];
69 aliases = [ "dbus-org.freedesktop.timesync1.service" ];
70 restartTriggers = [ config.environment.etc."systemd/timesyncd.conf".source ];
71 # systemd-timesyncd disables DNSSEC validation in the nss-resolve module by setting SYSTEMD_NSS_RESOLVE_VALIDATE to 0 in the unit file.
72 # This is required in order to solve the chicken-and-egg problem when DNSSEC validation needs the correct time to work, but to set the
73 # correct time, we need to connect to an NTP server, which usually requires resolving its hostname.
74 # In order for nss-resolve to be able to read this environment variable we patch systemd-timesyncd to disable NSCD and use NSS modules directly.
75 # This means that systemd-timesyncd needs to have NSS modules path in LD_LIBRARY_PATH. When systemd-resolved is disabled we still need to set
76 # NSS module path so that systemd-timesyncd keeps using other NSS modules that are configured in the system.
77 environment.LD_LIBRARY_PATH = config.system.nssModules.path;
80 # Ensure that we have some stored time to prevent
81 # systemd-timesyncd to resort back to the fallback time. If
82 # the file doesn't exist we assume that our current system
83 # clock is good enough to provide an initial value.
85 if ! [ -f /var/lib/systemd/timesync/clock ]; then
86 test -d /var/lib/systemd/timesync || mkdir -p /var/lib/systemd/timesync
87 touch /var/lib/systemd/timesync/clock
91 # workaround an issue of systemd-timesyncd not starting due to upstream systemd reverting their dynamic users changes
92 # - https://github.com/NixOS/nixpkgs/pull/61321#issuecomment-492423742
93 # - https://github.com/systemd/systemd/issues/12131
94 (lib.optionalString (versionOlder config.system.stateVersion "19.09") ''
95 if [ -L /var/lib/systemd/timesync ]; then
96 rm /var/lib/systemd/timesync
97 mv /var/lib/private/systemd/timesync /var/lib/systemd/timesync
103 environment.etc."systemd/timesyncd.conf".text =
107 + optionalString (cfg.servers != null) ''
108 NTP=${concatStringsSep " " cfg.servers}
110 + optionalString (cfg.fallbackServers != null) ''
111 FallbackNTP=${concatStringsSep " " cfg.fallbackServers}
115 users.users.systemd-timesync = {
116 uid = config.ids.uids.systemd-timesync;
117 group = "systemd-timesync";
119 users.groups.systemd-timesync.gid = config.ids.gids.systemd-timesync;