2014-04-14 10:26:48 -04:00
|
|
|
{ config, lib, pkgs, ... }:
|
2006-12-21 18:43:17 -05:00
|
|
|
|
2014-04-14 10:26:48 -04:00
|
|
|
with lib;
|
2009-03-06 07:26:19 -05:00
|
|
|
|
2009-10-12 13:09:38 -04:00
|
|
|
let
|
2009-03-06 07:26:19 -05:00
|
|
|
|
2009-07-15 07:34:55 -04:00
|
|
|
inherit (pkgs) ntp;
|
2006-12-21 18:43:17 -05:00
|
|
|
|
2015-09-10 04:56:18 -04:00
|
|
|
cfg = config.services.ntp;
|
|
|
|
|
2006-12-21 18:43:17 -05:00
|
|
|
stateDir = "/var/lib/ntp";
|
|
|
|
|
|
|
|
ntpUser = "ntp";
|
|
|
|
|
2009-07-15 07:34:55 -04:00
|
|
|
configFile = pkgs.writeText "ntp.conf" ''
|
2014-12-28 13:36:33 -05:00
|
|
|
driftfile ${stateDir}/ntp.drift
|
2006-12-21 18:43:17 -05:00
|
|
|
|
2014-03-06 05:54:02 -05:00
|
|
|
restrict 127.0.0.1
|
|
|
|
restrict -6 ::1
|
2014-02-03 17:41:35 -05:00
|
|
|
|
2015-09-10 04:56:18 -04:00
|
|
|
${toString (map (server: "server " + server + " iburst\n") cfg.servers)}
|
2008-03-20 10:38:49 -04:00
|
|
|
'';
|
2006-12-21 18:43:17 -05:00
|
|
|
|
2015-09-10 04:56:18 -04:00
|
|
|
ntpFlags = "-c ${configFile} -u ${ntpUser}:nogroup ${toString cfg.extraFlags}";
|
2006-12-22 14:23:19 -05:00
|
|
|
|
2006-12-21 18:43:17 -05:00
|
|
|
in
|
|
|
|
|
2009-07-15 07:34:55 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 14:20:50 -04:00
|
|
|
|
2009-07-15 07:34:55 -04:00
|
|
|
options = {
|
2011-09-14 14:20:50 -04:00
|
|
|
|
2009-07-15 07:34:55 -04:00
|
|
|
services.ntp = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
2014-04-10 06:29:46 -04:00
|
|
|
default = !config.boot.isContainer;
|
2009-07-15 07:34:55 -04:00
|
|
|
description = ''
|
|
|
|
Whether to synchronise your machine's time using the NTP
|
|
|
|
protocol.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
servers = mkOption {
|
|
|
|
default = [
|
2014-11-28 13:37:03 -05:00
|
|
|
"0.nixos.pool.ntp.org"
|
|
|
|
"1.nixos.pool.ntp.org"
|
|
|
|
"2.nixos.pool.ntp.org"
|
|
|
|
"3.nixos.pool.ntp.org"
|
2009-07-15 07:34:55 -04:00
|
|
|
];
|
|
|
|
description = ''
|
|
|
|
The set of NTP servers from which to synchronise.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-09-10 04:56:18 -04:00
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = "Extra flags passed to the ntpd command.";
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
|
2009-07-15 07:34:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
2009-03-06 07:26:19 -05:00
|
|
|
|
2009-07-15 07:34:55 -04:00
|
|
|
config = mkIf config.services.ntp.enable {
|
2011-09-14 14:20:50 -04:00
|
|
|
|
2014-12-28 13:36:33 -05:00
|
|
|
# Make tools such as ntpq available in the system path.
|
2013-05-22 22:07:49 -04:00
|
|
|
environment.systemPackages = [ pkgs.ntp ];
|
|
|
|
|
2009-07-15 07:34:55 -04:00
|
|
|
users.extraUsers = singleton
|
|
|
|
{ name = ntpUser;
|
|
|
|
uid = config.ids.uids.ntp;
|
|
|
|
description = "NTP daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
2006-12-21 18:43:17 -05:00
|
|
|
|
2014-12-28 13:36:33 -05:00
|
|
|
systemd.services.ntpd =
|
2013-01-10 07:59:41 -05:00
|
|
|
{ description = "NTP Daemon";
|
2006-12-21 18:43:17 -05:00
|
|
|
|
2014-11-26 14:19:31 -05:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-05-26 10:25:36 -04:00
|
|
|
wants = [ "time-sync.target" ];
|
|
|
|
before = [ "time-sync.target" ];
|
2006-12-22 14:23:19 -05:00
|
|
|
|
2009-10-12 13:09:38 -04:00
|
|
|
preStart =
|
|
|
|
''
|
2009-03-06 07:26:19 -05:00
|
|
|
mkdir -m 0755 -p ${stateDir}
|
|
|
|
chown ${ntpUser} ${stateDir}
|
2009-10-12 13:09:38 -04:00
|
|
|
'';
|
2009-03-06 07:26:19 -05:00
|
|
|
|
2014-12-28 13:36:33 -05:00
|
|
|
serviceConfig = {
|
2015-01-28 08:48:01 -05:00
|
|
|
ExecStart = "@${ntp}/bin/ntpd ntpd -g ${ntpFlags}";
|
|
|
|
Type = "forking";
|
2014-12-28 13:36:33 -05:00
|
|
|
};
|
2009-10-12 13:09:38 -04:00
|
|
|
};
|
2011-09-14 14:20:50 -04:00
|
|
|
|
2009-03-06 07:26:19 -05:00
|
|
|
};
|
2011-09-14 14:20:50 -04:00
|
|
|
|
2006-12-21 18:43:17 -05:00
|
|
|
}
|