14 cfg = config.services.ntp;
16 configFile = pkgs.writeText "ntp.conf" ''
17 driftfile /var/lib/ntp/ntp.drift
19 restrict default ${toString cfg.restrictDefault}
20 restrict -6 default ${toString cfg.restrictDefault}
21 restrict source ${toString cfg.restrictSource}
26 ${toString (map (server: "server " + server + " iburst\n") cfg.servers)}
52 Whether to synchronise your machine's time using ntpd, as a peer in
55 Disables `systemd.timesyncd` if enabled.
59 restrictDefault = mkOption {
60 type = types.listOf types.str;
62 The restriction flags to be set by default.
64 The default flags prevent external hosts from using ntpd as a DDoS
65 reflector, setting system time, and querying OS/ntpd version. As
66 recommended in section 6.5.1.1.3, answer "No" of
67 https://support.ntp.org/Support/AccessRestrictions
79 restrictSource = mkOption {
80 type = types.listOf types.str;
82 The restriction flags to be set on source.
84 The default flags allow peers to be added by ntpd from configured
85 pool(s), but not by other means.
97 default = config.networking.timeServers;
98 defaultText = literalExpression "config.networking.timeServers";
99 type = types.listOf types.str;
101 The set of NTP servers from which to synchronise.
105 extraConfig = mkOption {
109 fudge 127.127.1.0 stratum 10
112 Additional text appended to {file}`ntp.conf`.
116 extraFlags = mkOption {
117 type = types.listOf types.str;
118 description = "Extra flags passed to the ntpd command.";
119 example = literalExpression ''[ "--interface=eth0" ]'';
127 ###### implementation
129 config = mkIf config.services.ntp.enable {
130 meta.maintainers = with lib.maintainers; [ thoughtpolice ];
132 # Make tools such as ntpq available in the system path.
133 environment.systemPackages = [ pkgs.ntp ];
134 services.timesyncd.enable = mkForce false;
136 systemd.services.systemd-timedated.environment = {
137 SYSTEMD_TIMEDATED_NTP_SERVICES = "ntpd.service";
143 description = "NTP daemon user";
144 home = "/var/lib/ntp";
147 users.groups.ntp = { };
149 systemd.services.ntpd = {
150 description = "NTP Daemon";
152 wantedBy = [ "multi-user.target" ];
153 wants = [ "time-sync.target" ];
154 before = [ "time-sync.target" ];
157 ExecStart = "@${ntp}/bin/ntpd ntpd -g ${builtins.toString ntpFlags}";
161 PrivateDevices = true;
164 ProtectClock = false;
167 ProtectHostname = true;
168 ProtectKernelLogs = true;
169 ProtectKernelModules = true;
170 ProtectKernelTunables = true;
171 ProtectSystem = true;
173 RestrictNamespaces = true;
174 RestrictRealtime = true;
175 LockPersonality = true;
176 MemoryDenyWriteExecute = true;
177 AmbientCapabilities = [
181 ProtectControlGroups = true;
182 ProtectProc = "invisible";
184 RestrictSUIDSGID = true;