2009-03-06 12:26:19 +00:00
|
|
|
{pkgs, config, ...}:
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
###### interface
|
2006-12-21 23:43:17 +00:00
|
|
|
let
|
2009-03-06 12:26:19 +00:00
|
|
|
inherit (pkgs.lib) mkOption mkIf;
|
|
|
|
|
|
|
|
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.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
inherit (pkgs) writeText ntp;
|
2006-12-21 23:43:17 +00:00
|
|
|
|
|
|
|
stateDir = "/var/lib/ntp";
|
|
|
|
|
|
|
|
ntpUser = "ntp";
|
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
servers = config.services.ntp.servers;
|
|
|
|
|
|
|
|
modprobe = config.system.sbin.modprobe;
|
|
|
|
|
|
|
|
configFile = writeText "ntp.conf" ''
|
2006-12-21 23:43:17 +00:00
|
|
|
driftfile ${stateDir}/ntp.drift
|
|
|
|
|
|
|
|
${toString (map (server: "server " + server + "\n") servers)}
|
2008-03-20 14:38:49 +00:00
|
|
|
'';
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
ntpFlags = "-c ${configFile} -u ${ntpUser}:nogroup -i ${stateDir}";
|
2006-12-22 19:23:19 +00:00
|
|
|
|
2006-12-21 23:43:17 +00:00
|
|
|
in
|
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
|
|
|
|
mkIf config.services.ntp.enable {
|
|
|
|
require = [
|
|
|
|
options
|
2007-06-08 16:41:12 +01:00
|
|
|
];
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
services = {
|
|
|
|
extraJobs = [{
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
name = "ntpd";
|
|
|
|
|
|
|
|
users = [
|
|
|
|
{ name = ntpUser;
|
|
|
|
uid = (import ../system/ids.nix).uids.ntp;
|
|
|
|
description = "NTP daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
job = ''
|
|
|
|
description "NTP daemon"
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
start on ip-up
|
|
|
|
stop on ip-down
|
|
|
|
stop on shutdown
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
start script
|
2006-12-22 19:23:19 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
mkdir -m 0755 -p ${stateDir}
|
|
|
|
chown ${ntpUser} ${stateDir}
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
# Needed to run ntpd as an unprivileged user.
|
|
|
|
${modprobe}/sbin/modprobe capability || true
|
2008-03-20 14:38:49 +00:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
${ntp}/bin/ntpd -q -g ${ntpFlags}
|
|
|
|
|
|
|
|
end script
|
|
|
|
|
|
|
|
respawn ${ntp}/bin/ntpd -n ${ntpFlags}
|
|
|
|
'';
|
|
|
|
}];
|
|
|
|
};
|
2006-12-21 23:43:17 +00:00
|
|
|
}
|