1 { config, lib, pkgs, ... }:
6 cfg = config.services.logrotate;
9 if builtins.elem n [ "files" "priority" "enable" "global" ] || v == null then null
10 else if builtins.elem n [ "frequency" ] then "${v}\n"
11 else if builtins.elem n [ "firstaction" "lastaction" "prerotate" "postrotate" "preremove" ]
12 then "${n}\n ${v}\n endscript\n"
13 else if isInt v then "${n} ${toString v}\n"
14 else if v == true then "${n}\n"
15 else if v == false then "no${n}\n"
17 generateSection = indent: settings: concatStringsSep (fixedWidthString indent " " "") (
18 filter (x: x != null) (mapAttrsToList generateLine settings)
21 # generateSection includes a final newline hence weird closing brace
23 if settings.global or false then generateSection 0 settings
25 ${concatMapStringsSep "\n" (files: ''"${files}"'') (toList settings.files)} {
26 ${generateSection 2 settings}}
29 settings = sortProperties (attrValues (filterAttrs (_: settings: settings.enable) (
30 foldAttrs recursiveUpdate { } [
41 { header = { global = true; priority = 100; }; }
44 configFile = pkgs.writeTextFile {
45 name = "logrotate.conf";
46 text = concatStringsSep "\n" (
49 checkPhase = optionalString cfg.checkConfig ''
50 # logrotate --debug also checks that users specified in config
51 # file exist, but we only have sandboxed users here so brown these
52 # out. according to man page that means su, create and createolddir.
53 # files required to exist also won't be present, so missingok is forced.
54 user=$(${pkgs.buildPackages.coreutils}/bin/id -un)
55 group=$(${pkgs.buildPackages.coreutils}/bin/id -gn)
56 sed -e "s/\bsu\s.*/su $user $group/" \
57 -e "s/\b\(create\s\+[0-9]*\s*\|createolddir\s\+[0-9]*\s\+\).*/\1$user $group/" \
58 -e "1imissingok" -e "s/\bnomissingok\b//" \
60 # Since this makes for very verbose builds only show real error.
61 # There is no way to control log level, but logrotate hardcodes
62 # 'error:' at common log level, so we can use grep, taking care
65 if ! ${pkgs.buildPackages.logrotate}/sbin/logrotate -s logrotate.status \
66 --debug logrotate.conf 2>&1 \
67 | ( ! grep "error:" ) > logrotate-error; then
68 echo "Logrotate configuration check failed."
69 echo "The failing configuration (after adjustments to pass tests in sandbox) was:"
70 printf "%s\n" "-------"
72 printf "%s\n" "-------"
73 echo "The error reported by logrotate was as follow:"
74 printf "%s\n" "-------"
76 printf "%s\n" "-------"
77 echo "You can disable this check with services.logrotate.checkConfig = false,"
78 echo "but if you think it should work please report this failure along with"
79 echo "the config file being tested!"
86 if foldr (n: a: a || (n.mail or false) != false) false (attrValues cfg.settings)
87 then "--mail=${pkgs.mailutils}/bin/mail"
92 (mkRemovedOptionModule [ "services" "logrotate" "config" ] "Modify services.logrotate.settings.header instead")
93 (mkRemovedOptionModule [ "services" "logrotate" "extraConfig" ] "Modify services.logrotate.settings.header instead")
94 (mkRemovedOptionModule [ "services" "logrotate" "paths" ] "Add attributes to services.logrotate.settings instead")
98 services.logrotate = {
99 enable = mkEnableOption (lib.mdDoc "the logrotate systemd service") // {
100 default = foldr (n: a: a || n.enable) false (attrValues cfg.settings);
101 defaultText = literalExpression "cfg.settings != {}";
104 settings = mkOption {
106 description = lib.mdDoc ''
107 logrotate freeform settings: each attribute here will define its own section,
108 ordered by priority, which can either define files to rotate with their settings
109 or settings common to all further files settings.
110 Refer to <https://linux.die.net/man/8/logrotate> for details.
112 example = literalExpression ''
118 # example custom files
119 "/var/log/mylog.log" = {
125 "/var/log/first*.log"
126 "/var/log/second.log"
131 type = types.attrsOf (types.submodule ({ name, ... }: {
132 freeformType = with types; attrsOf (nullOr (oneOf [ int bool str ]));
135 enable = mkEnableOption (lib.mdDoc "setting individual kill switch") // {
142 description = lib.mdDoc ''
143 Whether this setting is a global option or not: set to have these
144 settings apply to all files settings with a higher priority.
148 type = with types; either str (listOf str);
151 The attrset name if not specified
153 description = lib.mdDoc ''
154 Single or list of files for which rules are defined.
155 The files are quoted with double-quotes in logrotate configuration,
156 so globs and spaces are supported.
157 Note this setting is ignored if globals is true.
161 frequency = mkOption {
162 type = types.nullOr types.str;
164 description = lib.mdDoc ''
165 How often to rotate the logs. Defaults to previously set global setting,
166 which itself defauts to weekly.
170 priority = mkOption {
173 description = lib.mdDoc ''
174 Order of this logrotate block in relation to the others. The semantics are
175 the same as with `lib.mkOrder`. Smaller values are inserted first.
183 configFile = mkOption {
185 default = configFile;
187 A configuration file automatically generated by NixOS.
189 description = lib.mdDoc ''
190 Override the configuration file used by MySQL. By default,
191 NixOS generates one automatically from [](#opt-services.logrotate.settings).
193 example = literalExpression ''
194 pkgs.writeText "logrotate.conf" '''
204 checkConfig = mkOption {
207 description = lib.mdDoc ''
208 Whether the config should be checked at build time.
210 Some options are not checkable at build time because of the build sandbox:
211 for example, the test does not know about existing files and system users are
213 These limitations mean we must adjust the file for tests (missingok is forced
214 and users are replaced by dummy users), so tests are complemented by a
215 logrotate-checkconf service that is enabled by default.
216 This extra check can be disabled by disabling it at the systemd level with the
217 {option}`services.systemd.services.logrotate-checkconf.enable` option.
219 Conversely there are still things that might make this check fail incorrectly
220 (e.g. a file path where we don't have access to intermediate directories):
221 in this case you can disable the failing check with this option.
227 config = mkIf cfg.enable {
228 systemd.services.logrotate = {
229 description = "Logrotate Service";
235 ExecStart = "${pkgs.logrotate}/sbin/logrotate ${mailOption} ${cfg.configFile}";
238 systemd.services.logrotate-checkconf = {
239 description = "Logrotate configuration check";
240 wantedBy = [ "multi-user.target" ];
243 RemainAfterExit = true;
244 ExecStart = "${pkgs.logrotate}/sbin/logrotate --debug ${cfg.configFile}";