From c94911c5b77eec303bdd8310c4c2b069c1b5fdad Mon Sep 17 00:00:00 2001 From: Jeff Slight Date: Tue, 12 May 2020 13:55:09 -0700 Subject: [PATCH 1/3] nixos/logrotate: use lib.mkEnableOption --- nixos/modules/services/logging/logrotate.nix | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/nixos/modules/services/logging/logrotate.nix b/nixos/modules/services/logging/logrotate.nix index fdd9f0f3e5c..17eb1ddf6ac 100644 --- a/nixos/modules/services/logging/logrotate.nix +++ b/nixos/modules/services/logging/logrotate.nix @@ -12,12 +12,7 @@ in { options = { services.logrotate = { - enable = mkOption { - type = lib.types.bool; - default = false; - description = '' - Enable the logrotate cron job - ''; + enable = mkEnableOption "the logrotate systemd service"; }; config = mkOption { From 90ce7f508a8d2a40b64fb0430faf8f5ec9695277 Mon Sep 17 00:00:00 2001 From: Jeff Slight Date: Wed, 13 May 2020 11:59:28 -0700 Subject: [PATCH 2/3] nixos/logrotate: add options for basic paths --- nixos/modules/services/logging/logrotate.nix | 73 ++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/nixos/modules/services/logging/logrotate.nix b/nixos/modules/services/logging/logrotate.nix index 17eb1ddf6ac..5b0af8e3349 100644 --- a/nixos/modules/services/logging/logrotate.nix +++ b/nixos/modules/services/logging/logrotate.nix @@ -5,21 +5,86 @@ with lib; let cfg = config.services.logrotate; - configFile = pkgs.writeText "logrotate.conf" - cfg.config; + 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" ( + (concatStringsSep "\n" (map pathConfig cfg.paths)) + + cfg.extraConfig + ); in { + imports = [ + (mkRenamedOptionModule [ "services" "logrotate" "config" ] [ "services" "logrotate" "extraConfig" ]) + ]; + options = { services.logrotate = { enable = mkEnableOption "the logrotate systemd service"; + + 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; + }; + }; }; - config = mkOption { + extraConfig = mkOption { default = ""; type = types.lines; description = '' - The contents of the logrotate config file + Extra contents to add to the logrotate config file. + See https://linux.die.net/man/8/logrotate ''; }; }; From fe07adef7f4a4053a32d61351f0bc5f2ecbb3a80 Mon Sep 17 00:00:00 2001 From: Jeff Slight Date: Wed, 13 May 2020 20:52:26 -0700 Subject: [PATCH 3/3] nixos/logrotate: add newline before extraConfig Co-authored-by: Ryan Mulligan --- nixos/modules/services/logging/logrotate.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nixos/modules/services/logging/logrotate.nix b/nixos/modules/services/logging/logrotate.nix index 5b0af8e3349..565618b27a8 100644 --- a/nixos/modules/services/logging/logrotate.nix +++ b/nixos/modules/services/logging/logrotate.nix @@ -51,8 +51,7 @@ let ''; configFile = pkgs.writeText "logrotate.conf" ( - (concatStringsSep "\n" (map pathConfig cfg.paths)) + - cfg.extraConfig + (concatStringsSep "\n" ((map pathConfig cfg.paths) ++ [cfg.extraConfig])) ); in