1 { config, lib, pkgs, ... }:
6 cfg = config.services.logrotate;
14 Whether to enable log rotation for this path. This can be used to explicitly disable
15 logging that has been configured by NixOS.
20 type = with types; either str (listOf str);
22 The path to log files to be rotated.
27 type = with types; nullOr str;
30 The user account to use for rotation.
35 type = with types; nullOr str;
38 The group to use for rotation.
42 frequency = mkOption {
43 type = types.enum [ "hourly" "daily" "weekly" "monthly" "yearly" ];
46 How often to rotate the logs.
54 How many rotations to keep.
58 extraConfig = mkOption {
62 Extra logrotate config options for this path. Refer to
63 <link xlink:href="https://linux.die.net/man/8/logrotate"/> for details.
71 Order of this logrotate block in relation to the others. The semantics are
72 the same as with `lib.mkOrder`. Smaller values have a greater priority.
77 config.extraConfig = ''
84 # generated by NixOS using the `services.logrotate.paths.${pathOpts.name}` attribute set
85 ${concatMapString " " (path: ''"${path}"'') (if isList pathOpts.path then pathOpts.path else [pathOpts.path]} {
86 ${optionalString (pathOpts.user != null || pathOpts.group != null) "su ${pathOpts.user} ${pathOpts.group}"}
88 rotate ${toString pathOpts.keep}
89 ${pathOpts.extraConfig}
93 paths = sortProperties (mapAttrsToList (name: pathOpts: pathOpts // { name = name; }) (filterAttrs (_: pathOpts: pathOpts.enable) cfg.paths));
94 configFile = pkgs.writeText "logrotate.conf" (concatStringsSep "\n" ((map mkConf paths) ++ [ cfg.extraConfig ]));
99 (mkRenamedOptionModule [ "services" "logrotate" "config" ] [ "services" "logrotate" "extraConfig" ])
103 services.logrotate = {
104 enable = mkEnableOption "the logrotate systemd service";
107 type = with types; attrsOf (submodule pathOpts);
110 Attribute set of paths to rotate. The order each block appears in the generated configuration file
111 can be controlled by the <link linkend="opt-services.logrotate.paths._name_.priority">priority</link> option
112 using the same semantics as `lib.mkOrder`. Smaller values have a greater priority.
114 example = literalExpression ''
117 path = "/var/log/httpd/*.log";
118 user = config.services.httpd.user;
119 group = config.services.httpd.group;
124 path = "/var/log/myapp/*.log";
127 frequency = "weekly";
135 extraConfig = mkOption {
139 Extra contents to append to the logrotate configuration file. Refer to
140 <link xlink:href="https://linux.die.net/man/8/logrotate"/> for details.
146 config = mkIf cfg.enable {
147 assertions = mapAttrsToList (name: pathOpts:
148 { assertion = (pathOpts.user != null) == (pathOpts.group != null);
150 If either of `services.logrotate.paths.${name}.user` or `services.logrotate.paths.${name}.group` are specified then *both* must be specified.
155 systemd.services.logrotate = {
156 description = "Logrotate Service";
157 wantedBy = [ "multi-user.target" ];
160 exec ${pkgs.logrotate}/sbin/logrotate ${configFile}