|
9 | 9 | stateDir = cfg.directory; |
10 | 10 | driftFile = "${stateDir}/chrony.drift"; |
11 | 11 | keyFile = "${stateDir}/chrony.keys"; |
| 12 | + rtcFile = "${stateDir}/chrony.rtc"; |
12 | 13 |
|
13 | 14 | configFile = pkgs.writeText "chrony.conf" '' |
14 | 15 | ${concatMapStringsSep "\n" (server: "server " + server + " " + cfg.serverOption + optionalString (cfg.enableNTS) " nts") cfg.servers} |
|
20 | 21 |
|
21 | 22 | driftfile ${driftFile} |
22 | 23 | keyfile ${keyFile} |
| 24 | + ${optionalString (cfg.enableRTCTrimming) "rtcfile ${rtcFile}"} |
23 | 25 | ${optionalString (cfg.enableNTS) "ntsdumpdir ${stateDir}"} |
24 | 26 |
|
| 27 | + ${optionalString (cfg.enableRTCTrimming) "rtcautotrim ${builtins.toString cfg.autotrimThreshold}"} |
25 | 28 | ${optionalString (!config.time.hardwareClockInLocalTime) "rtconutc"} |
26 | 29 |
|
27 | 30 | ${cfg.extraConfig} |
|
85 | 88 | ''; |
86 | 89 | }; |
87 | 90 |
|
| 91 | + enableRTCTrimming = mkOption { |
| 92 | + type = types.bool; |
| 93 | + default = true; |
| 94 | + description = lib.mdDoc '' |
| 95 | + Enable tracking of the RTC offset to the system clock and automatic trimming. |
| 96 | + See also [](#opt-services.chrony.autotrimThreshold) |
| 97 | +
|
| 98 | + ::: {.note} |
| 99 | + This is not compatible with the `rtcsync` directive, which naively syncs the RTC time every 11 minutes. |
| 100 | +
|
| 101 | + Tracking the RTC drift will allow more precise timekeeping, |
| 102 | + especially on intermittently running devices, where the RTC is very relevant. |
| 103 | + ::: |
| 104 | + ''; |
| 105 | + }; |
| 106 | + |
| 107 | + autotrimThreshold = mkOption { |
| 108 | + type = types.ints.positive; |
| 109 | + default = 30; |
| 110 | + example = 10; |
| 111 | + description = '' |
| 112 | + Maximum estimated error threshold for the `rtcautotrim` command. |
| 113 | + When reached, the RTC will be trimmed. |
| 114 | + Only used when [](#opt-services.chrony.enableRTCTrimming) is enabled. |
| 115 | + ''; |
| 116 | + }; |
| 117 | + |
88 | 118 | enableNTS = mkOption { |
89 | 119 | type = types.bool; |
90 | 120 | default = false; |
|
141 | 171 | }; |
142 | 172 |
|
143 | 173 | config = mkIf cfg.enable { |
144 | | - meta.maintainers = with lib.maintainers; [ thoughtpolice ]; |
| 174 | + meta.maintainers = with lib.maintainers; [ thoughtpolice vifino ]; |
145 | 175 |
|
146 | 176 | environment.systemPackages = [ chronyPkg ]; |
147 | 177 |
|
|
156 | 186 |
|
157 | 187 | services.timesyncd.enable = mkForce false; |
158 | 188 |
|
| 189 | + # If chrony controls and tracks the RTC, writing it externally causes clock error. |
| 190 | + systemd.services.save-hwclock = lib.mkIf cfg.enableRTCTrimming { |
| 191 | + enable = lib.mkForce false; |
| 192 | + }; |
| 193 | + |
159 | 194 | systemd.services.systemd-timedated.environment = { SYSTEMD_TIMEDATED_NTP_SERVICES = "chronyd.service"; }; |
160 | 195 |
|
161 | 196 | systemd.tmpfiles.rules = [ |
162 | 197 | "d ${stateDir} 0750 chrony chrony - -" |
163 | 198 | "f ${driftFile} 0640 chrony chrony - -" |
164 | 199 | "f ${keyFile} 0640 chrony chrony - -" |
| 200 | + ] ++ lib.optionals cfg.enableRTCTrimming [ |
| 201 | + "f ${rtcFile} 0640 chrony chrony - -" |
165 | 202 | ]; |
166 | 203 |
|
167 | 204 | systemd.services.chronyd = |
|
0 commit comments