2018-07-20 22:56:59 +02:00
|
|
|
{ config, lib, ... }:
|
2015-04-19 21:05:12 +02:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2016-12-14 23:49:14 +01:00
|
|
|
services.timesyncd = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = !config.boot.isContainer;
|
2021-11-26 01:16:05 +01:00
|
|
|
defaultText = literalExpression "!config.boot.isContainer";
|
2016-12-14 23:49:14 +01:00
|
|
|
type = types.bool;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2016-12-14 23:49:14 +01:00
|
|
|
Enables the systemd NTP client daemon.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
servers = mkOption {
|
|
|
|
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 ''
|
2016-12-14 23:49:14 +01:00
|
|
|
The set of NTP servers from which to synchronise.
|
|
|
|
'';
|
|
|
|
};
|
2019-11-20 06:57:02 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
example = ''
|
|
|
|
PollIntervalMaxSec=180
|
|
|
|
'';
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2019-11-20 06:57:02 +01:00
|
|
|
Extra config options for systemd-timesyncd. See
|
2022-07-28 23:19:15 +02:00
|
|
|
[
|
|
|
|
timesyncd.conf(5)](https://www.freedesktop.org/software/systemd/man/timesyncd.conf.html) for available options.
|
2019-11-20 06:57:02 +01:00
|
|
|
'';
|
|
|
|
};
|
2015-04-19 21:05:12 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf config.services.timesyncd.enable {
|
|
|
|
|
2015-04-19 21:11:14 +02:00
|
|
|
systemd.additionalUpstreamSystemUnits = [ "systemd-timesyncd.service" ];
|
|
|
|
|
2015-04-19 21:05:12 +02:00
|
|
|
systemd.services.systemd-timesyncd = {
|
|
|
|
wantedBy = [ "sysinit.target" ];
|
2020-05-19 11:06:32 +02:00
|
|
|
aliases = [ "dbus-org.freedesktop.timesync1.service" ];
|
2015-04-19 21:05:12 +02:00
|
|
|
restartTriggers = [ config.environment.etc."systemd/timesyncd.conf".source ];
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.etc."systemd/timesyncd.conf".text = ''
|
|
|
|
[Time]
|
2018-05-25 00:44:04 +02:00
|
|
|
NTP=${concatStringsSep " " config.services.timesyncd.servers}
|
2019-11-20 06:57:02 +01:00
|
|
|
${config.services.timesyncd.extraConfig}
|
2015-04-19 21:05:12 +02:00
|
|
|
'';
|
|
|
|
|
2019-11-24 22:46:49 +01:00
|
|
|
users.users.systemd-timesync = {
|
|
|
|
uid = config.ids.uids.systemd-timesync;
|
|
|
|
group = "systemd-timesync";
|
|
|
|
};
|
2018-06-30 01:58:35 +02:00
|
|
|
users.groups.systemd-timesync.gid = config.ids.gids.systemd-timesync;
|
2015-04-19 21:05:12 +02:00
|
|
|
|
2021-12-31 14:12:56 +01:00
|
|
|
system.activationScripts.systemd-timesyncd-migration =
|
2019-05-15 11:05:37 +02:00
|
|
|
# workaround an issue of systemd-timesyncd not starting due to upstream systemd reverting their dynamic users changes
|
|
|
|
# - https://github.com/NixOS/nixpkgs/pull/61321#issuecomment-492423742
|
|
|
|
# - https://github.com/systemd/systemd/issues/12131
|
2021-12-31 14:12:56 +01:00
|
|
|
mkIf (versionOlder config.system.stateVersion "19.09") ''
|
|
|
|
if [ -L /var/lib/systemd/timesync ]; then
|
|
|
|
rm /var/lib/systemd/timesync
|
|
|
|
mv /var/lib/private/systemd/timesync /var/lib/systemd/timesync
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
system.activationScripts.systemd-timesyncd-init-clock =
|
|
|
|
# Ensure that we have some stored time to prevent systemd-timesyncd to
|
|
|
|
# resort back to the fallback time.
|
|
|
|
# If the file doesn't exist we assume that our current system clock is
|
|
|
|
# good enough to provide an initial value.
|
|
|
|
''
|
|
|
|
if ! [ -f /var/lib/systemd/timesync/clock ]; then
|
|
|
|
test -d /var/lib/systemd/timesync || mkdir -p /var/lib/systemd/timesync
|
|
|
|
touch /var/lib/systemd/timesync/clock
|
2019-05-15 11:05:37 +02:00
|
|
|
fi
|
2021-12-31 14:12:56 +01:00
|
|
|
'';
|
2015-04-19 21:05:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|