2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.logrotate;
|
2022-01-03 16:56:52 +00:00
|
|
|
inherit (config.users) groups;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-01-03 16:56:52 +00:00
|
|
|
pathOpts = { name, ... }: {
|
2020-05-29 06:06:01 +00:00
|
|
|
options = {
|
2020-09-25 04:45:31 +00:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to enable log rotation for this path. This can be used to explicitly disable
|
|
|
|
logging that has been configured by NixOS.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-01-03 16:56:52 +00:00
|
|
|
name = mkOption {
|
2020-05-29 06:06:01 +00:00
|
|
|
type = types.str;
|
2022-01-03 16:56:52 +00:00
|
|
|
internal = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
path = mkOption {
|
|
|
|
type = with types; either str (listOf str);
|
|
|
|
default = name;
|
|
|
|
defaultText = "attribute name";
|
2020-09-25 04:45:31 +00:00
|
|
|
description = ''
|
|
|
|
The path to log files to be rotated.
|
2022-01-03 16:56:52 +00:00
|
|
|
Spaces are allowed and normal shell quoting rules apply,
|
|
|
|
with ', ", and \ characters supported.
|
2020-09-25 04:45:31 +00:00
|
|
|
'';
|
2020-05-29 06:06:01 +00:00
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2020-05-29 06:06:01 +00:00
|
|
|
user = mkOption {
|
2020-09-25 04:45:31 +00:00
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
The user account to use for rotation.
|
|
|
|
'';
|
2020-05-29 06:06:01 +00:00
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2020-05-29 06:06:01 +00:00
|
|
|
group = mkOption {
|
2020-09-25 04:45:31 +00:00
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
The group to use for rotation.
|
|
|
|
'';
|
2020-05-29 06:06:01 +00:00
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2020-05-29 06:06:01 +00:00
|
|
|
frequency = mkOption {
|
2021-12-06 16:07:01 +00:00
|
|
|
type = types.enum [ "hourly" "daily" "weekly" "monthly" "yearly" ];
|
2020-05-29 06:06:01 +00:00
|
|
|
default = "daily";
|
2020-09-25 04:45:31 +00:00
|
|
|
description = ''
|
|
|
|
How often to rotate the logs.
|
|
|
|
'';
|
2020-05-29 06:06:01 +00:00
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2020-05-29 06:06:01 +00:00
|
|
|
keep = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 20;
|
2020-09-25 04:45:31 +00:00
|
|
|
description = ''
|
|
|
|
How many rotations to keep.
|
|
|
|
'';
|
2020-05-29 06:06:01 +00:00
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2020-05-29 06:06:01 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2020-09-25 04:45:31 +00:00
|
|
|
description = ''
|
|
|
|
Extra logrotate config options for this path. Refer to
|
|
|
|
<link xlink:href="https://linux.die.net/man/8/logrotate"/> for details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
priority = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 1000;
|
|
|
|
description = ''
|
|
|
|
Order of this logrotate block in relation to the others. The semantics are
|
|
|
|
the same as with `lib.mkOrder`. Smaller values have a greater priority.
|
|
|
|
'';
|
2020-05-29 06:06:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-01-03 16:56:52 +00:00
|
|
|
config.name = name;
|
2020-09-25 04:45:31 +00:00
|
|
|
config.extraConfig = ''
|
2020-05-29 06:06:01 +00:00
|
|
|
missingok
|
|
|
|
notifempty
|
2020-09-25 04:45:31 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mkConf = pathOpts: ''
|
|
|
|
# generated by NixOS using the `services.logrotate.paths.${pathOpts.name}` attribute set
|
2022-01-03 16:56:52 +00:00
|
|
|
${concatMapStringsSep " " (path: ''"${path}"'') (toList pathOpts.path)} {
|
2020-09-25 04:45:31 +00:00
|
|
|
${optionalString (pathOpts.user != null || pathOpts.group != null) "su ${pathOpts.user} ${pathOpts.group}"}
|
|
|
|
${pathOpts.frequency}
|
|
|
|
rotate ${toString pathOpts.keep}
|
|
|
|
${pathOpts.extraConfig}
|
2020-05-29 06:06:01 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2022-01-03 16:56:52 +00:00
|
|
|
paths = sortProperties (attrValues (filterAttrs (_: pathOpts: pathOpts.enable) cfg.paths));
|
2020-09-25 04:45:31 +00:00
|
|
|
configFile = pkgs.writeText "logrotate.conf" (concatStringsSep "\n" ((map mkConf paths) ++ [ cfg.extraConfig ]));
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
{
|
2020-05-29 06:06:01 +00:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "logrotate" "config" ] [ "services" "logrotate" "extraConfig" ])
|
|
|
|
];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
options = {
|
|
|
|
services.logrotate = {
|
2020-05-29 06:06:01 +00:00
|
|
|
enable = mkEnableOption "the logrotate systemd service";
|
|
|
|
|
|
|
|
paths = mkOption {
|
2020-09-25 04:45:31 +00:00
|
|
|
type = with types; attrsOf (submodule pathOpts);
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
Attribute set of paths to rotate. The order each block appears in the generated configuration file
|
|
|
|
can be controlled by the <link linkend="opt-services.logrotate.paths._name_.priority">priority</link> option
|
|
|
|
using the same semantics as `lib.mkOrder`. Smaller values have a greater priority.
|
|
|
|
'';
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2020-09-25 04:45:31 +00:00
|
|
|
{
|
|
|
|
httpd = {
|
|
|
|
path = "/var/log/httpd/*.log";
|
|
|
|
user = config.services.httpd.user;
|
|
|
|
group = config.services.httpd.group;
|
|
|
|
keep = 7;
|
|
|
|
};
|
|
|
|
|
|
|
|
myapp = {
|
|
|
|
path = "/var/log/myapp/*.log";
|
|
|
|
user = "myuser";
|
|
|
|
group = "mygroup";
|
|
|
|
frequency = "weekly";
|
|
|
|
keep = 5;
|
|
|
|
priority = 1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2020-05-29 06:06:01 +00:00
|
|
|
extraConfig = mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
2020-09-25 04:45:31 +00:00
|
|
|
Extra contents to append to the logrotate configuration file. Refer to
|
|
|
|
<link xlink:href="https://linux.die.net/man/8/logrotate"/> for details.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2020-09-25 04:45:31 +00:00
|
|
|
assertions = mapAttrsToList (name: pathOpts:
|
|
|
|
{ assertion = (pathOpts.user != null) == (pathOpts.group != null);
|
|
|
|
message = ''
|
|
|
|
If either of `services.logrotate.paths.${name}.user` or `services.logrotate.paths.${name}.group` are specified then *both* must be specified.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
) cfg.paths;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-01-03 16:56:52 +00:00
|
|
|
services.logrotate = {
|
|
|
|
paths = {
|
|
|
|
"/var/log/btmp" = {
|
|
|
|
frequency = mkDefault "monthly";
|
|
|
|
keep = mkDefault 1;
|
|
|
|
extraConfig = ''
|
|
|
|
create 0660 root ${groups.utmp.name}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
"/var/log/wtmp" = {
|
|
|
|
frequency = mkDefault "monthly";
|
|
|
|
keep = mkDefault 1;
|
|
|
|
extraConfig = ''
|
|
|
|
create 0664 root ${groups.utmp.name}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
systemd.services.logrotate = {
|
|
|
|
description = "Logrotate Service";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-12-06 16:07:01 +00:00
|
|
|
startAt = "hourly";
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "no";
|
|
|
|
User = "root";
|
2022-01-03 16:56:52 +00:00
|
|
|
ExecStart = "${pkgs.logrotate}/sbin/logrotate ${configFile}";
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|