2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-05-23 02:07:49 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2013-05-23 02:07:49 +00:00
|
|
|
|
|
|
|
let
|
2018-09-24 05:21:52 +00:00
|
|
|
cfg = config.services.chrony;
|
2013-05-23 02:07:49 +00:00
|
|
|
|
|
|
|
stateDir = "/var/lib/chrony";
|
2018-09-24 05:21:52 +00:00
|
|
|
keyFile = "${stateDir}/chrony.keys";
|
2013-05-23 03:00:09 +00:00
|
|
|
|
2017-02-07 12:16:17 +00:00
|
|
|
configFile = pkgs.writeText "chrony.conf" ''
|
|
|
|
${concatMapStringsSep "\n" (server: "server " + server) cfg.servers}
|
|
|
|
|
|
|
|
${optionalString
|
2018-12-01 00:46:04 +00:00
|
|
|
(cfg.initstepslew.enabled && (cfg.servers != []))
|
2017-02-07 12:16:17 +00:00
|
|
|
"initstepslew ${toString cfg.initstepslew.threshold} ${concatStringsSep " " cfg.initstepslew.servers}"
|
|
|
|
}
|
|
|
|
|
|
|
|
driftfile ${stateDir}/chrony.drift
|
|
|
|
keyfile ${keyFile}
|
|
|
|
|
|
|
|
${optionalString (!config.time.hardwareClockInLocalTime) "rtconutc"}
|
|
|
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
|
2018-09-24 05:21:52 +00:00
|
|
|
chronyFlags = "-m -u chrony -f ${configFile} ${toString cfg.extraFlags}";
|
2013-05-23 02:07:49 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.chrony = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to synchronise your machine's time using chrony.
|
|
|
|
Make sure you disable NTP if you enable this service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
servers = mkOption {
|
2016-12-14 22:49:14 +00:00
|
|
|
default = config.networking.timeServers;
|
2013-05-23 02:07:49 +00:00
|
|
|
description = ''
|
|
|
|
The set of NTP servers from which to synchronise.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-05-23 03:00:09 +00:00
|
|
|
initstepslew = mkOption {
|
|
|
|
default = {
|
|
|
|
enabled = true;
|
|
|
|
threshold = 1000; # by default, same threshold as 'ntpd -g' (1000s)
|
|
|
|
servers = cfg.servers;
|
|
|
|
};
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
2016-10-23 17:33:41 +00:00
|
|
|
type = types.lines;
|
2013-05-23 03:00:09 +00: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 02:07:49 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-05-26 14:25:36 +00:00
|
|
|
config = mkIf cfg.enable {
|
2013-05-23 02:07:49 +00:00
|
|
|
environment.systemPackages = [ pkgs.chrony ];
|
|
|
|
|
2018-06-29 23:58:35 +00:00
|
|
|
users.groups = singleton
|
2015-07-12 05:13:04 +00:00
|
|
|
{ name = "chrony";
|
|
|
|
gid = config.ids.gids.chrony;
|
|
|
|
};
|
2015-07-12 04:01:41 +00:00
|
|
|
|
2018-06-29 23:58:35 +00:00
|
|
|
users.users = singleton
|
2015-07-12 05:13:04 +00:00
|
|
|
{ name = "chrony";
|
2013-05-23 02:07:49 +00:00
|
|
|
uid = config.ids.uids.chrony;
|
2015-07-12 05:13:04 +00:00
|
|
|
group = "chrony";
|
2013-05-23 02:07:49 +00:00
|
|
|
description = "chrony daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
|
|
|
|
2018-06-22 10:02:35 +00:00
|
|
|
services.timesyncd.enable = mkForce false;
|
2015-07-12 05:13:04 +00:00
|
|
|
|
2018-10-23 20:49:42 +00:00
|
|
|
systemd.services.systemd-timedated.environment = { SYSTEMD_TIMEDATED_NTP_SERVICES = "chronyd.service"; };
|
|
|
|
|
2015-07-12 05:13:04 +00:00
|
|
|
systemd.services.chronyd =
|
|
|
|
{ description = "chrony NTP daemon";
|
2013-05-23 02:07:49 +00:00
|
|
|
|
2014-11-26 19:19:31 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2018-09-24 05:21:52 +00:00
|
|
|
wants = [ "time-sync.target" ];
|
|
|
|
before = [ "time-sync.target" ];
|
|
|
|
after = [ "network.target" ];
|
2015-07-12 04:01:41 +00:00
|
|
|
conflicts = [ "ntpd.service" "systemd-timesyncd.service" ];
|
2013-05-23 02:07:49 +00:00
|
|
|
|
2015-07-12 05:13:04 +00:00
|
|
|
path = [ pkgs.chrony ];
|
2013-05-23 02:07:49 +00:00
|
|
|
|
2018-09-24 05:21:52 +00:00
|
|
|
preStart = ''
|
|
|
|
mkdir -m 0755 -p ${stateDir}
|
|
|
|
touch ${keyFile}
|
|
|
|
chmod 0640 ${keyFile}
|
|
|
|
chown chrony:chrony ${stateDir} ${keyFile}
|
|
|
|
'';
|
2013-05-23 02:07:49 +00:00
|
|
|
|
2018-12-03 02:14:48 +00:00
|
|
|
unitConfig.ConditionCapability = "CAP_SYS_TIME";
|
2015-07-12 05:13:04 +00:00
|
|
|
serviceConfig =
|
2018-09-24 05:21:52 +00:00
|
|
|
{ Type = "forking";
|
|
|
|
ExecStart = "${pkgs.chrony}/bin/chronyd ${chronyFlags}";
|
|
|
|
|
|
|
|
ProtectHome = "yes";
|
|
|
|
ProtectSystem = "full";
|
|
|
|
PrivateTmp = "yes";
|
|
|
|
|
2015-07-12 05:13:04 +00:00
|
|
|
};
|
2018-12-03 02:14:48 +00:00
|
|
|
|
2013-05-23 02:07:49 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|