2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with 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
|
|
|
|
2015-09-10 10:56:18 +02:00
|
|
|
cfg = config.services.ntp;
|
|
|
|
|
2006-12-22 00:43:17 +01:00
|
|
|
stateDir = "/var/lib/ntp";
|
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
configFile = pkgs.writeText "ntp.conf" ''
|
2014-12-28 19:36:33 +01:00
|
|
|
driftfile ${stateDir}/ntp.drift
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2018-11-28 11:15:25 +01:00
|
|
|
restrict default ${toString cfg.restrictDefault}
|
|
|
|
restrict -6 default ${toString cfg.restrictDefault}
|
|
|
|
restrict source ${toString cfg.restrictSource}
|
|
|
|
|
2014-03-06 11:54:02 +01:00
|
|
|
restrict 127.0.0.1
|
|
|
|
restrict -6 ::1
|
2014-02-03 23:41:35 +01:00
|
|
|
|
2015-09-10 10:56:18 +02:00
|
|
|
${toString (map (server: "server " + server + " iburst\n") cfg.servers)}
|
2020-03-12 15:12:48 +01:00
|
|
|
|
|
|
|
${cfg.extraConfig}
|
2008-03-20 15:38:49 +01:00
|
|
|
'';
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2021-09-17 14:00:00 +02:00
|
|
|
ntpFlags = "-c ${configFile} -u ntp:ntp ${toString cfg.extraFlags}";
|
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
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
services.ntp = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
2020-04-20 20:05:26 +02:00
|
|
|
type = types.bool;
|
2016-12-14 23:49:14 +01:00
|
|
|
default = false;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
2018-11-28 11:15:25 +01:00
|
|
|
Whether to synchronise your machine's time using ntpd, as a peer in
|
|
|
|
the NTP network.
|
2022-08-02 17:34:22 +02:00
|
|
|
|
2022-08-03 22:46:41 +02:00
|
|
|
Disables `systemd.timesyncd` if enabled.
|
2018-11-28 11:15:25 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
restrictDefault = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
2018-11-28 11:15:25 +01:00
|
|
|
The restriction flags to be set by default.
|
2022-08-02 17:34:22 +02:00
|
|
|
|
2018-11-28 11:15:25 +01:00
|
|
|
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;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
2018-11-28 11:15:25 +01:00
|
|
|
The restriction flags to be set on source.
|
2022-08-02 17:34:22 +02:00
|
|
|
|
2018-11-28 11:15:25 +01:00
|
|
|
The default flags allow peers to be added by ntpd from configured
|
|
|
|
pool(s), but not by other means.
|
2009-07-15 13:34:55 +02:00
|
|
|
'';
|
2018-11-28 11:15:25 +01:00
|
|
|
default = [ "limited" "kod" "nomodify" "notrap" "noquery" ];
|
2009-07-15 13:34:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
servers = mkOption {
|
2016-12-14 23:49:14 +01:00
|
|
|
default = config.networking.timeServers;
|
2021-11-26 01:16:05 +01:00
|
|
|
defaultText = literalExpression "config.networking.timeServers";
|
2021-01-20 10:54:24 +01:00
|
|
|
type = types.listOf types.str;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2009-07-15 13:34:55 +02:00
|
|
|
The set of NTP servers from which to synchronise.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-03-12 15:12:48 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
fudge 127.127.1.0 stratum 10
|
|
|
|
'';
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Additional text appended to {file}`ntp.conf`.
|
2020-03-12 15:12:48 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-09-10 10:56:18 +02:00
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Extra flags passed to the ntpd command.";
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''[ "--interface=eth0" ]'';
|
2015-09-10 10:56:18 +02:00
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
2009-03-06 13:26:19 +01:00
|
|
|
|
2009-07-15 13:34:55 +02:00
|
|
|
config = mkIf config.services.ntp.enable {
|
2018-12-01 19:55:31 +01:00
|
|
|
meta.maintainers = with lib.maintainers; [ thoughtpolice ];
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2014-12-28 19:36:33 +01:00
|
|
|
# Make tools such as ntpq available in the system path.
|
2013-05-23 04:07:49 +02:00
|
|
|
environment.systemPackages = [ pkgs.ntp ];
|
2016-12-14 23:49:14 +01:00
|
|
|
services.timesyncd.enable = mkForce false;
|
2013-05-23 04:07:49 +02:00
|
|
|
|
2018-10-23 22:49:42 +02:00
|
|
|
systemd.services.systemd-timedated.environment = { SYSTEMD_TIMEDATED_NTP_SERVICES = "ntpd.service"; };
|
|
|
|
|
2021-09-17 14:00:00 +02:00
|
|
|
users.users.ntp =
|
|
|
|
{ isSystemUser = true;
|
|
|
|
group = "ntp";
|
2009-07-15 13:34:55 +02:00
|
|
|
description = "NTP daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
2021-09-17 14:00:00 +02:00
|
|
|
users.groups.ntp = {};
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2014-12-28 19:36:33 +01:00
|
|
|
systemd.services.ntpd =
|
2013-01-10 13:59:41 +01:00
|
|
|
{ description = "NTP Daemon";
|
2006-12-22 00:43:17 +01:00
|
|
|
|
2014-11-26 20:19:31 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-05-26 16:25:36 +02:00
|
|
|
wants = [ "time-sync.target" ];
|
|
|
|
before = [ "time-sync.target" ];
|
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}
|
2021-09-17 14:00:00 +02:00
|
|
|
chown ntp ${stateDir}
|
2009-10-12 19:09:38 +02:00
|
|
|
'';
|
2009-03-06 13:26:19 +01:00
|
|
|
|
2014-12-28 19:36:33 +01:00
|
|
|
serviceConfig = {
|
2015-01-28 14:48:01 +01:00
|
|
|
ExecStart = "@${ntp}/bin/ntpd ntpd -g ${ntpFlags}";
|
|
|
|
Type = "forking";
|
2014-12-28 19:36:33 +01:00
|
|
|
};
|
2009-10-12 19:09:38 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-03-06 13:26:19 +01:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2006-12-22 00:43:17 +01:00
|
|
|
}
|