aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/nixos/modules/services/logging/logrotate.nix
blob: 565618b27a87bbeb217d022f73e9060baf5920a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.logrotate;

  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;
          };
        };
      };

      extraConfig = mkOption {
        default = "";
        type = types.lines;
        description = ''
          Extra contents to add to the logrotate config file.
          See https://linux.die.net/man/8/logrotate
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    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}
      '';
    };
  };
}