2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2009-03-06 12:26:19 +00:00
|
|
|
|
2009-10-12 18:09:38 +01:00
|
|
|
let
|
2009-03-06 12:26:19 +00:00
|
|
|
|
2009-07-15 12:34:55 +01:00
|
|
|
inherit (pkgs) ntp;
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2015-09-10 09:56:18 +01:00
|
|
|
cfg = config.services.ntp;
|
|
|
|
|
2006-12-21 23:43:17 +00:00
|
|
|
stateDir = "/var/lib/ntp";
|
|
|
|
|
|
|
|
ntpUser = "ntp";
|
|
|
|
|
2009-07-15 12:34:55 +01:00
|
|
|
configFile = pkgs.writeText "ntp.conf" ''
|
2014-12-28 18:36:33 +00:00
|
|
|
driftfile ${stateDir}/ntp.drift
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2018-11-28 10:15:25 +00:00
|
|
|
restrict default ${toString cfg.restrictDefault}
|
|
|
|
restrict -6 default ${toString cfg.restrictDefault}
|
|
|
|
restrict source ${toString cfg.restrictSource}
|
|
|
|
|
2014-03-06 10:54:02 +00:00
|
|
|
restrict 127.0.0.1
|
|
|
|
restrict -6 ::1
|
2014-02-03 22:41:35 +00:00
|
|
|
|
2015-09-10 09:56:18 +01:00
|
|
|
${toString (map (server: "server " + server + " iburst\n") cfg.servers)}
|
2008-03-20 14:38:49 +00:00
|
|
|
'';
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2015-09-10 09:56:18 +01:00
|
|
|
ntpFlags = "-c ${configFile} -u ${ntpUser}:nogroup ${toString cfg.extraFlags}";
|
2006-12-22 19:23:19 +00:00
|
|
|
|
2006-12-21 23:43:17 +00:00
|
|
|
in
|
|
|
|
|
2009-07-15 12:34:55 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-07-15 12:34:55 +01:00
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-07-15 12:34:55 +01:00
|
|
|
services.ntp = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
2016-12-14 22:49:14 +00:00
|
|
|
default = false;
|
2009-07-15 12:34:55 +01:00
|
|
|
description = ''
|
2018-11-28 10:15:25 +00:00
|
|
|
Whether to synchronise your machine's time using ntpd, as a peer in
|
|
|
|
the NTP network.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Disables <literal>systemd.timesyncd</literal> if enabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
restrictDefault = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = ''
|
|
|
|
The restriction flags to be set by default.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The default flags prevent external hosts from using ntpd as a DDoS
|
|
|
|
reflector, setting system time, and querying OS/ntpd version. As
|
|
|
|
recommended in section 6.5.1.1.3, answer "No" of
|
|
|
|
http://support.ntp.org/bin/view/Support/AccessRestrictions
|
|
|
|
'';
|
|
|
|
default = [ "limited" "kod" "nomodify" "notrap" "noquery" "nopeer" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
restrictSource = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = ''
|
|
|
|
The restriction flags to be set on source.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The default flags allow peers to be added by ntpd from configured
|
|
|
|
pool(s), but not by other means.
|
2009-07-15 12:34:55 +01:00
|
|
|
'';
|
2018-11-28 10:15:25 +00:00
|
|
|
default = [ "limited" "kod" "nomodify" "notrap" "noquery" ];
|
2009-07-15 12:34:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
servers = mkOption {
|
2016-12-14 22:49:14 +00:00
|
|
|
default = config.networking.timeServers;
|
2009-07-15 12:34:55 +01:00
|
|
|
description = ''
|
|
|
|
The set of NTP servers from which to synchronise.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-09-10 09:56:18 +01:00
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = "Extra flags passed to the ntpd command.";
|
2018-11-28 10:15:25 +00:00
|
|
|
example = literalExample ''[ "--interface=eth0" ]'';
|
2015-09-10 09:56:18 +01:00
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
|
2009-07-15 12:34:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
2009-03-06 12:26:19 +00:00
|
|
|
|
2009-07-15 12:34:55 +01:00
|
|
|
config = mkIf config.services.ntp.enable {
|
2018-12-01 18:55:31 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ thoughtpolice ];
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2014-12-28 18:36:33 +00:00
|
|
|
# Make tools such as ntpq available in the system path.
|
2013-05-23 03:07:49 +01:00
|
|
|
environment.systemPackages = [ pkgs.ntp ];
|
2016-12-14 22:49:14 +00:00
|
|
|
services.timesyncd.enable = mkForce false;
|
2013-05-23 03:07:49 +01:00
|
|
|
|
2018-10-23 21:49:42 +01:00
|
|
|
systemd.services.systemd-timedated.environment = { SYSTEMD_TIMEDATED_NTP_SERVICES = "ntpd.service"; };
|
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.users = singleton
|
2009-07-15 12:34:55 +01:00
|
|
|
{ name = ntpUser;
|
|
|
|
uid = config.ids.uids.ntp;
|
|
|
|
description = "NTP daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2014-12-28 18:36:33 +00:00
|
|
|
systemd.services.ntpd =
|
2013-01-10 12:59:41 +00:00
|
|
|
{ description = "NTP Daemon";
|
2006-12-21 23:43:17 +00:00
|
|
|
|
2014-11-26 19:19:31 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-05-26 15:25:36 +01:00
|
|
|
wants = [ "time-sync.target" ];
|
|
|
|
before = [ "time-sync.target" ];
|
2006-12-22 19:23:19 +00:00
|
|
|
|
2009-10-12 18:09:38 +01:00
|
|
|
preStart =
|
|
|
|
''
|
2009-03-06 12:26:19 +00:00
|
|
|
mkdir -m 0755 -p ${stateDir}
|
|
|
|
chown ${ntpUser} ${stateDir}
|
2009-10-12 18:09:38 +01:00
|
|
|
'';
|
2009-03-06 12:26:19 +00:00
|
|
|
|
2014-12-28 18:36:33 +00:00
|
|
|
serviceConfig = {
|
2015-01-28 13:48:01 +00:00
|
|
|
ExecStart = "@${ntp}/bin/ntpd ntpd -g ${ntpFlags}";
|
|
|
|
Type = "forking";
|
2014-12-28 18:36:33 +00:00
|
|
|
};
|
2009-10-12 18:09:38 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-03-06 12:26:19 +00:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2006-12-21 23:43:17 +00:00
|
|
|
}
|