2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-04-29 19:54:08 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2010-04-29 19:54:08 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.logrotate;
|
|
|
|
|
2020-05-13 19:59:28 +01:00
|
|
|
pathOptions = {
|
|
|
|
options = {
|
|
|
|
path = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The path to log files to be rotated";
|
|
|
|
};
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The user account to use for rotation";
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The group to use for rotation";
|
|
|
|
};
|
|
|
|
frequency = mkOption {
|
|
|
|
type = types.enum [
|
|
|
|
"daily" "weekly" "monthly" "yearly"
|
|
|
|
];
|
|
|
|
default = "daily";
|
|
|
|
description = "How often to rotate the logs";
|
|
|
|
};
|
|
|
|
keep = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 20;
|
|
|
|
description = "How many rotations to keep";
|
|
|
|
};
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = "Extra logrotate config options for this path";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
pathConfig = options: ''
|
|
|
|
"${options.path}" {
|
|
|
|
su ${options.user} ${options.group}
|
|
|
|
${options.frequency}
|
|
|
|
missingok
|
|
|
|
notifempty
|
|
|
|
rotate ${toString options.keep}
|
|
|
|
${options.extraConfig}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
|
|
|
configFile = pkgs.writeText "logrotate.conf" (
|
2020-05-14 04:52:26 +01:00
|
|
|
(concatStringsSep "\n" ((map pathConfig cfg.paths) ++ [cfg.extraConfig]))
|
2020-05-13 19:59:28 +01:00
|
|
|
);
|
2010-04-29 19:54:08 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
{
|
2020-05-13 19:59:28 +01:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "logrotate" "config" ] [ "services" "logrotate" "extraConfig" ])
|
|
|
|
];
|
|
|
|
|
2010-04-29 19:54:08 +01:00
|
|
|
options = {
|
|
|
|
services.logrotate = {
|
2020-05-12 21:55:09 +01:00
|
|
|
enable = mkEnableOption "the logrotate systemd service";
|
2020-05-13 19:59:28 +01:00
|
|
|
|
|
|
|
paths = mkOption {
|
|
|
|
type = types.listOf (types.submodule pathOptions);
|
|
|
|
default = [];
|
|
|
|
description = "List of attribute sets with paths to rotate";
|
|
|
|
example = {
|
|
|
|
"/var/log/myapp/*.log" = {
|
|
|
|
user = "myuser";
|
|
|
|
group = "mygroup";
|
|
|
|
rotate = "weekly";
|
|
|
|
keep = 5;
|
|
|
|
};
|
|
|
|
};
|
2010-04-29 19:54:08 +01:00
|
|
|
};
|
|
|
|
|
2020-05-13 19:59:28 +01:00
|
|
|
extraConfig = mkOption {
|
2010-04-29 19:54:08 +01:00
|
|
|
default = "";
|
2014-10-16 07:20:43 +01:00
|
|
|
type = types.lines;
|
2010-04-29 19:54:08 +01:00
|
|
|
description = ''
|
2020-05-13 19:59:28 +01:00
|
|
|
Extra contents to add to the logrotate config file.
|
|
|
|
See https://linux.die.net/man/8/logrotate
|
2010-04-29 19:54:08 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2014-07-10 12:11:35 +01:00
|
|
|
systemd.services.logrotate = {
|
|
|
|
description = "Logrotate Service";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
startAt = "*-*-* *:05:00";
|
|
|
|
|
|
|
|
serviceConfig.Restart = "no";
|
|
|
|
serviceConfig.User = "root";
|
|
|
|
script = ''
|
|
|
|
exec ${pkgs.logrotate}/sbin/logrotate ${configFile}
|
|
|
|
'';
|
|
|
|
};
|
2010-04-29 19:54:08 +01:00
|
|
|
};
|
|
|
|
}
|