1 { config, lib, pkgs, ... }:
9 cfg = config.services.ntp;
11 stateDir = "/var/lib/ntp";
13 configFile = pkgs.writeText "ntp.conf" ''
14 driftfile ${stateDir}/ntp.drift
16 restrict default ${toString cfg.restrictDefault}
17 restrict -6 default ${toString cfg.restrictDefault}
18 restrict source ${toString cfg.restrictSource}
23 ${toString (map (server: "server " + server + " iburst\n") cfg.servers)}
28 ntpFlags = [ "-c" "${configFile}" "-u" "ntp:ntp" ] ++ cfg.extraFlags;
44 Whether to synchronise your machine's time using ntpd, as a peer in
47 Disables `systemd.timesyncd` if enabled.
51 restrictDefault = mkOption {
52 type = types.listOf types.str;
54 The restriction flags to be set by default.
56 The default flags prevent external hosts from using ntpd as a DDoS
57 reflector, setting system time, and querying OS/ntpd version. As
58 recommended in section 6.5.1.1.3, answer "No" of
59 https://support.ntp.org/Support/AccessRestrictions
61 default = [ "limited" "kod" "nomodify" "notrap" "noquery" "nopeer" ];
64 restrictSource = mkOption {
65 type = types.listOf types.str;
67 The restriction flags to be set on source.
69 The default flags allow peers to be added by ntpd from configured
70 pool(s), but not by other means.
72 default = [ "limited" "kod" "nomodify" "notrap" "noquery" ];
76 default = config.networking.timeServers;
77 defaultText = literalExpression "config.networking.timeServers";
78 type = types.listOf types.str;
80 The set of NTP servers from which to synchronise.
84 extraConfig = mkOption {
88 fudge 127.127.1.0 stratum 10
91 Additional text appended to {file}`ntp.conf`.
95 extraFlags = mkOption {
96 type = types.listOf types.str;
97 description = "Extra flags passed to the ntpd command.";
98 example = literalExpression ''[ "--interface=eth0" ]'';
107 ###### implementation
109 config = mkIf config.services.ntp.enable {
110 meta.maintainers = with lib.maintainers; [ thoughtpolice ];
112 # Make tools such as ntpq available in the system path.
113 environment.systemPackages = [ pkgs.ntp ];
114 services.timesyncd.enable = mkForce false;
116 systemd.services.systemd-timedated.environment = { SYSTEMD_TIMEDATED_NTP_SERVICES = "ntpd.service"; };
119 { isSystemUser = true;
121 description = "NTP daemon user";
124 users.groups.ntp = {};
126 systemd.services.ntpd =
127 { description = "NTP Daemon";
129 wantedBy = [ "multi-user.target" ];
130 wants = [ "time-sync.target" ];
131 before = [ "time-sync.target" ];
135 mkdir -m 0755 -p ${stateDir}
136 chown ntp ${stateDir}
140 ExecStart = "@${ntp}/bin/ntpd ntpd -g ${builtins.toString ntpFlags}";