2014-04-14 18:26:48 +04:00
|
|
|
{ config, lib, pkgs, ... }:
|
2006-12-22 02:43:17 +03:00
|
|
|
|
2014-04-14 18:26:48 +04:00
|
|
|
with lib;
|
2009-03-06 15:26:19 +03:00
|
|
|
|
2009-10-12 21:09:38 +04:00
|
|
|
let
|
2009-03-06 15:26:19 +03:00
|
|
|
|
2009-07-15 15:34:55 +04:00
|
|
|
inherit (pkgs) ntp;
|
2006-12-22 02:43:17 +03:00
|
|
|
|
|
|
|
stateDir = "/var/lib/ntp";
|
|
|
|
|
|
|
|
ntpUser = "ntp";
|
|
|
|
|
2009-07-15 15:34:55 +04:00
|
|
|
configFile = pkgs.writeText "ntp.conf" ''
|
2009-03-11 18:01:13 +03: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 02:43:17 +03:00
|
|
|
|
2014-02-04 02:41:35 +04:00
|
|
|
restrict default kod nomodify notrap nopeer noquery
|
|
|
|
restrict -6 default kod nomodify notrap nopeer noquery
|
2014-03-06 14:54:02 +04:00
|
|
|
restrict 127.0.0.1
|
|
|
|
restrict -6 ::1
|
2014-02-04 02:41:35 +04:00
|
|
|
|
2012-03-17 21:26:17 +04:00
|
|
|
${toString (map (server: "server " + server + " iburst\n") config.services.ntp.servers)}
|
2008-03-20 17:38:49 +03:00
|
|
|
'';
|
2006-12-22 02:43:17 +03:00
|
|
|
|
2009-03-06 15:26:19 +03:00
|
|
|
ntpFlags = "-c ${configFile} -u ${ntpUser}:nogroup -i ${stateDir}";
|
2006-12-22 22:23:19 +03:00
|
|
|
|
2006-12-22 02:43:17 +03:00
|
|
|
in
|
|
|
|
|
2009-07-15 15:34:55 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 22:20:50 +04:00
|
|
|
|
2009-07-15 15:34:55 +04:00
|
|
|
options = {
|
2011-09-14 22:20:50 +04:00
|
|
|
|
2009-07-15 15:34:55 +04:00
|
|
|
services.ntp = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
2014-04-10 14:29:46 +04:00
|
|
|
default = !config.boot.isContainer;
|
2009-07-15 15:34:55 +04:00
|
|
|
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 15:26:19 +03:00
|
|
|
|
2009-07-15 15:34:55 +04:00
|
|
|
config = mkIf config.services.ntp.enable {
|
2011-09-14 22:20:50 +04:00
|
|
|
|
2013-05-23 06:07:49 +04:00
|
|
|
# Make tools such as ntpq available in the system path
|
|
|
|
environment.systemPackages = [ pkgs.ntp ];
|
|
|
|
|
2009-07-15 15:34:55 +04:00
|
|
|
users.extraUsers = singleton
|
|
|
|
{ name = ntpUser;
|
|
|
|
uid = config.ids.uids.ntp;
|
|
|
|
description = "NTP daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
2006-12-22 02:43:17 +03:00
|
|
|
|
2009-10-12 22:09:34 +04:00
|
|
|
jobs.ntpd =
|
2013-01-10 16:59:41 +04:00
|
|
|
{ description = "NTP Daemon";
|
2006-12-22 02:43:17 +03:00
|
|
|
|
2012-08-15 23:38:52 +04:00
|
|
|
wantedBy = [ "ip-up.target" ];
|
|
|
|
partOf = [ "ip-up.target" ];
|
2006-12-22 22:23:19 +03:00
|
|
|
|
2011-11-25 20:32:54 +04:00
|
|
|
path = [ ntp ];
|
|
|
|
|
2009-10-12 21:09:38 +04:00
|
|
|
preStart =
|
|
|
|
''
|
2009-03-06 15:26:19 +03:00
|
|
|
mkdir -m 0755 -p ${stateDir}
|
|
|
|
chown ${ntpUser} ${stateDir}
|
2009-10-12 21:09:38 +04:00
|
|
|
'';
|
2009-03-06 15:26:19 +03:00
|
|
|
|
2011-11-25 20:32:54 +04:00
|
|
|
exec = "ntpd -g -n ${ntpFlags}";
|
2009-10-12 21:09:38 +04:00
|
|
|
};
|
2011-09-14 22:20:50 +04:00
|
|
|
|
2009-03-06 15:26:19 +03:00
|
|
|
};
|
2011-09-14 22:20:50 +04:00
|
|
|
|
2006-12-22 02:43:17 +03:00
|
|
|
}
|