diff --git a/configuration/options.nix b/configuration/options.nix index ead48d030aa1..d128f6c8e331 100644 --- a/configuration/options.nix +++ b/configuration/options.nix @@ -239,6 +239,29 @@ } + { + name = ["services" "ntp" "enable"]; + default = true; + description = " + Whether to synchronise your machine's time using the NTP + protocol. + "; + } + + + { + name = ["services" "ntp" "servers"]; + default = [ + "0.pool.ntp.org" + "1.pool.ntp.org" + "2.pool.ntp.org" + ]; + description = " + The set of NTP servers from which to synchronise. + "; + } + + { name = ["services" "xserver" "enable"]; default = false; diff --git a/configuration/upstart.nix b/configuration/upstart.nix index 4065a0305935..46e6ea3aa6ab 100644 --- a/configuration/upstart.nix +++ b/configuration/upstart.nix @@ -81,6 +81,13 @@ import ../upstart-jobs/gather.nix { inherit (pkgs) openssh glibc pwdutils; }) + # NTP daemon. + ++ optional ["services" "ntp" "enable"] + (import ../upstart-jobs/ntpd.nix { + inherit (pkgs) ntp glibc pwdutils writeText; + servers = config.get ["services" "ntp" "servers"]; + }) + # X server. ++ optional ["services" "xserver" "enable"] (import ../upstart-jobs/xserver.nix { diff --git a/upstart-jobs/ntpd.nix b/upstart-jobs/ntpd.nix new file mode 100644 index 000000000000..a8c90c31fd09 --- /dev/null +++ b/upstart-jobs/ntpd.nix @@ -0,0 +1,45 @@ +{ntp, glibc, pwdutils, writeText, servers}: + +let + + stateDir = "/var/lib/ntp"; + + ntpUser = "ntp"; + + config = writeText "ntp.conf" " + driftfile ${stateDir}/ntp.drift + + ${toString (map (server: "server " + server + "\n") servers)} + "; + +in + +{ + name = "ntpd"; + + job = " +description \"NTP daemon\" + +start on network-interfaces/started +stop on network-interfaces/stop + +start script + + if ! ${glibc}/bin/getent passwd ${ntpUser} > /dev/null; then + ${pwdutils}/sbin/useradd -g nogroup -d ${stateDir} -s /noshell \\ + -c 'NTP daemon user' ${ntpUser} + fi + + mkdir -m 0755 -p ${stateDir} + chown ${ntpUser} ${stateDir} + + date + ${ntp}/bin/ntpd -ddd -c ${config} -q -g + date + +end script + +respawn ${ntp}/bin/ntpd -ddd -n -c ${config} + "; + +}