2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-05-23 03:07:49 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2013-05-23 03:07:49 +01:00
|
|
|
|
|
|
|
let
|
2018-09-24 06:21:52 +01:00
|
|
|
cfg = config.services.chrony;
|
2021-01-06 10:42:28 +00:00
|
|
|
chronyPkg = cfg.package;
|
2013-05-23 03:07:49 +01:00
|
|
|
|
2021-01-06 10:40:48 +00:00
|
|
|
stateDir = cfg.directory;
|
2020-11-02 20:29:43 +00:00
|
|
|
driftFile = "${stateDir}/chrony.drift";
|
2018-09-24 06:21:52 +01:00
|
|
|
keyFile = "${stateDir}/chrony.keys";
|
2013-05-23 04:00:09 +01:00
|
|
|
|
2017-02-07 12:16:17 +00:00
|
|
|
configFile = pkgs.writeText "chrony.conf" ''
|
2021-01-06 10:38:56 +00:00
|
|
|
${concatMapStringsSep "\n" (server: "server " + server + " " + cfg.serverOption + optionalString (cfg.enableNTS) " nts") cfg.servers}
|
2017-02-07 12:16:17 +00:00
|
|
|
|
|
|
|
${optionalString
|
2018-12-01 00:46:04 +00:00
|
|
|
(cfg.initstepslew.enabled && (cfg.servers != []))
|
2019-02-23 07:21:18 +00:00
|
|
|
"initstepslew ${toString cfg.initstepslew.threshold} ${concatStringsSep " " cfg.servers}"
|
2017-02-07 12:16:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:29:43 +00:00
|
|
|
driftfile ${driftFile}
|
2017-02-07 12:16:17 +00:00
|
|
|
keyfile ${keyFile}
|
2021-01-06 10:33:39 +00:00
|
|
|
${optionalString (cfg.enableNTS) "ntsdumpdir ${stateDir}"}
|
2017-02-07 12:16:17 +00:00
|
|
|
|
|
|
|
${optionalString (!config.time.hardwareClockInLocalTime) "rtconutc"}
|
|
|
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
|
2019-02-23 15:04:26 +00:00
|
|
|
chronyFlags = "-n -m -u chrony -f ${configFile} ${toString cfg.extraFlags}";
|
2013-05-23 03:07:49 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.chrony = {
|
|
|
|
enable = mkOption {
|
2020-04-20 19:05:26 +01:00
|
|
|
type = types.bool;
|
2013-05-23 03:07:49 +01:00
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to synchronise your machine's time using chrony.
|
|
|
|
Make sure you disable NTP if you enable this service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-01-06 10:42:28 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.chrony;
|
|
|
|
defaultText = "pkgs.chrony";
|
|
|
|
description = ''
|
|
|
|
Which chrony package to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-05-23 03:07:49 +01:00
|
|
|
servers = mkOption {
|
2016-12-14 22:49:14 +00:00
|
|
|
default = config.networking.timeServers;
|
2021-01-20 09:54:24 +00:00
|
|
|
type = types.listOf types.str;
|
2013-05-23 03:07:49 +01:00
|
|
|
description = ''
|
|
|
|
The set of NTP servers from which to synchronise.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-01-06 10:38:56 +00:00
|
|
|
serverOption = mkOption {
|
|
|
|
default = "iburst";
|
|
|
|
type = types.enum [ "iburst" "offline" ];
|
|
|
|
description = ''
|
|
|
|
Set option for server directives.
|
|
|
|
|
|
|
|
Use "iburst" to rapidly poll on startup. Recommended if your machine
|
|
|
|
is consistently online.
|
|
|
|
|
|
|
|
Use "offline" to prevent polling on startup. Recommended if your
|
|
|
|
machine boots offline or is otherwise frequently offline.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-01-06 10:33:39 +00:00
|
|
|
enableNTS = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable Network Time Security authentication.
|
|
|
|
Make sure it is supported by your selected NTP server(s).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-05-23 04:00:09 +01:00
|
|
|
initstepslew = mkOption {
|
|
|
|
default = {
|
|
|
|
enabled = true;
|
|
|
|
threshold = 1000; # by default, same threshold as 'ntpd -g' (1000s)
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
Allow chronyd to make a rapid measurement of the system clock error at
|
|
|
|
boot time, and to correct the system clock by stepping before normal
|
|
|
|
operation begins.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-01-06 10:40:48 +00:00
|
|
|
directory = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/var/lib/chrony";
|
|
|
|
description = "Directory where chrony state is stored.";
|
|
|
|
};
|
|
|
|
|
2013-05-23 04:00:09 +01:00
|
|
|
extraConfig = mkOption {
|
2016-10-23 18:33:41 +01:00
|
|
|
type = types.lines;
|
2013-05-23 04:00:09 +01:00
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Extra configuration directives that should be added to
|
|
|
|
<literal>chrony.conf</literal>
|
|
|
|
'';
|
|
|
|
};
|
2017-02-07 16:07:20 +00:00
|
|
|
|
|
|
|
extraFlags = mkOption {
|
|
|
|
default = [];
|
|
|
|
example = [ "-s" ];
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = "Extra flags passed to the chronyd command.";
|
|
|
|
};
|
2013-05-23 03:07:49 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-05-26 15:25:36 +01:00
|
|
|
config = mkIf cfg.enable {
|
2018-12-01 18:55:31 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ thoughtpolice ];
|
|
|
|
|
2021-01-06 10:42:28 +00:00
|
|
|
environment.systemPackages = [ chronyPkg ];
|
2013-05-23 03:07:49 +01:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.groups.chrony.gid = config.ids.gids.chrony;
|
2015-07-12 05:01:41 +01:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users.chrony =
|
|
|
|
{ uid = config.ids.uids.chrony;
|
2015-07-12 06:13:04 +01:00
|
|
|
group = "chrony";
|
2013-05-23 03:07:49 +01:00
|
|
|
description = "chrony daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
|
|
|
|
2018-06-22 11:02:35 +01:00
|
|
|
services.timesyncd.enable = mkForce false;
|
2015-07-12 06:13:04 +01:00
|
|
|
|
2018-10-23 21:49:42 +01:00
|
|
|
systemd.services.systemd-timedated.environment = { SYSTEMD_TIMEDATED_NTP_SERVICES = "chronyd.service"; };
|
|
|
|
|
2019-11-23 15:28:26 +00:00
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d ${stateDir} 0755 chrony chrony - -"
|
2020-11-02 20:29:43 +00:00
|
|
|
"f ${driftFile} 0640 chrony chrony -"
|
2019-11-23 15:28:26 +00:00
|
|
|
"f ${keyFile} 0640 chrony chrony -"
|
|
|
|
];
|
|
|
|
|
2015-07-12 06:13:04 +01:00
|
|
|
systemd.services.chronyd =
|
|
|
|
{ description = "chrony NTP daemon";
|
2013-05-23 03:07:49 +01:00
|
|
|
|
2014-11-26 19:19:31 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2018-09-24 06:21:52 +01:00
|
|
|
wants = [ "time-sync.target" ];
|
|
|
|
before = [ "time-sync.target" ];
|
|
|
|
after = [ "network.target" ];
|
2015-07-12 05:01:41 +01:00
|
|
|
conflicts = [ "ntpd.service" "systemd-timesyncd.service" ];
|
2013-05-23 03:07:49 +01:00
|
|
|
|
2021-01-06 10:42:28 +00:00
|
|
|
path = [ chronyPkg ];
|
2013-05-23 03:07:49 +01:00
|
|
|
|
2018-12-03 02:14:48 +00:00
|
|
|
unitConfig.ConditionCapability = "CAP_SYS_TIME";
|
2015-07-12 06:13:04 +01:00
|
|
|
serviceConfig =
|
2019-02-23 15:04:26 +00:00
|
|
|
{ Type = "simple";
|
2021-01-06 10:42:28 +00:00
|
|
|
ExecStart = "${chronyPkg}/bin/chronyd ${chronyFlags}";
|
2018-09-24 06:21:52 +01:00
|
|
|
|
|
|
|
ProtectHome = "yes";
|
|
|
|
ProtectSystem = "full";
|
|
|
|
PrivateTmp = "yes";
|
2015-07-12 06:13:04 +01:00
|
|
|
};
|
2018-12-03 02:14:48 +00:00
|
|
|
|
2013-05-23 03:07:49 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|