2009-10-12 19:09:38 +02:00
|
|
|
{ config, pkgs, ... }:
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2009-10-12 19:09:38 +02:00
|
|
|
with pkgs.lib;
|
2009-03-06 13:26:19 +01:00
|
|
|
|
2009-10-12 19:09:38 +02:00
|
|
|
let
|
2009-03-06 13:26:19 +01:00
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
inherit (pkgs) ntp;
|
2006-12-22 00:43:17 +01:00
|
|
|
|
|
|
|
stateDir = "/var/lib/ntp";
|
|
|
|
|
|
|
|
ntpUser = "ntp";
|
|
|
|
|
2009-03-06 13:26:19 +01:00
|
|
|
servers = config.services.ntp.servers;
|
|
|
|
|
|
|
|
modprobe = config.system.sbin.modprobe;
|
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
configFile = pkgs.writeText "ntp.conf" ''
|
2006-12-22 00:43:17 +01:00
|
|
|
driftfile ${stateDir}/ntp.drift
|
2009-03-11 16:01:13 +01:00
|
|
|
# Keep the drift file in ${stateDir}/ntp.drift. However, since we
|
|
|
|
# chroot to ${stateDir}, we have to specify it as /ntp.drift.
|
|
|
|
driftfile /ntp.drift
|
2006-12-22 00:43:17 +01:00
|
|
|
|
|
|
|
${toString (map (server: "server " + server + "\n") servers)}
|
2008-03-20 15:38:49 +01:00
|
|
|
'';
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2009-03-06 13:26:19 +01:00
|
|
|
ntpFlags = "-c ${configFile} -u ${ntpUser}:nogroup -i ${stateDir}";
|
2006-12-22 20:23:19 +01:00
|
|
|
|
2006-12-22 00:43:17 +01:00
|
|
|
in
|
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.ntp = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to synchronise your machine's time using the NTP
|
|
|
|
protocol.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
servers = mkOption {
|
|
|
|
default = [
|
|
|
|
"0.pool.ntp.org"
|
|
|
|
"1.pool.ntp.org"
|
|
|
|
"2.pool.ntp.org"
|
|
|
|
];
|
|
|
|
description = ''
|
|
|
|
The set of NTP servers from which to synchronise.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
2009-03-06 13:26:19 +01:00
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
config = mkIf config.services.ntp.enable {
|
|
|
|
|
|
|
|
users.extraUsers = singleton
|
|
|
|
{ name = ntpUser;
|
|
|
|
uid = config.ids.uids.ntp;
|
|
|
|
description = "NTP daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2009-10-12 20:09:34 +02:00
|
|
|
jobs.ntpd =
|
2009-10-12 19:09:38 +02:00
|
|
|
{ description = "NTP daemon";
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2009-10-12 19:09:38 +02:00
|
|
|
startOn = "ip-up";
|
|
|
|
stopOn = "ip-down";
|
2006-12-22 20:23:19 +01:00
|
|
|
|
2009-10-12 19:09:38 +02:00
|
|
|
preStart =
|
|
|
|
''
|
2009-03-06 13:26:19 +01:00
|
|
|
mkdir -m 0755 -p ${stateDir}
|
|
|
|
chown ${ntpUser} ${stateDir}
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2009-03-06 13:26:19 +01:00
|
|
|
# Needed to run ntpd as an unprivileged user.
|
|
|
|
${modprobe}/sbin/modprobe capability || true
|
2008-03-20 15:38:49 +01:00
|
|
|
|
2009-06-02 21:40:14 +02:00
|
|
|
# !!! This can hang indefinitely if the network is down or
|
|
|
|
# the servers are unreachable. This is particularly bad
|
|
|
|
# because Upstart cannot kill jobs stuck in the start
|
|
|
|
# phase. Thus a hanging ntpd job can block system
|
|
|
|
# shutdown.
|
|
|
|
# ${ntp}/bin/ntpd -q -g ${ntpFlags}
|
2009-10-12 19:09:38 +02:00
|
|
|
'';
|
2009-03-06 13:26:19 +01:00
|
|
|
|
2009-10-12 19:09:38 +02:00
|
|
|
exec = "${ntp}/bin/ntpd -g -n ${ntpFlags}";
|
|
|
|
};
|
2009-07-15 13:34:55 +02:00
|
|
|
|
2009-03-06 13:26:19 +01:00
|
|
|
};
|
2009-07-15 13:34:55 +02:00
|
|
|
|
2006-12-22 00:43:17 +01:00
|
|
|
}
|