git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #152223 from ju1m/logrotate

nixos/logrotate: enable multiple paths per entry

commitbf607abf73714bed1661bc0d54db06f19c9390d5 authorAaron Andersen <aaron@fosslib.net> dateMon 3 Jan 2022, 12:11 (5 years ago) parentae04063f7ff512
1 file changed+35−7
nixos/modules/services/logging/logrotate.nix+35−7
nixos/modules/services/logging/logrotate.nix+35−7
@@ -4,8 +4,9 @@ with lib;
44
55let
66 cfg = config.services.logrotate;
7+ inherit (config.users) groups;
78
8− pathOpts = {
9+ pathOpts = { name, ... }: {
910 options = {
1011 enable = mkOption {
1112 type = types.bool;
@@ -16,10 +17,19 @@ let
1617 '';
1718 };
1819
19− path = mkOption {
20+ name = mkOption {
2021 type = types.str;
22+ internal = true;
23+ };
24+
25+ path = mkOption {
26+ type = with types; either str (listOf str);
27+ default = name;
28+ defaultText = "attribute name";
2129 description = ''
2230 The path to log files to be rotated.
31+ Spaces are allowed and normal shell quoting rules apply,
32+ with ', ", and \ characters supported.
2333 '';
2434 };
2535
@@ -74,6 +84,7 @@ let
7484 };
7585 };
7686
87+ config.name = name;
7788 config.extraConfig = ''
7889 missingok
7990 notifempty
@@ -82,7 +93,7 @@ let
8293
8394 mkConf = pathOpts: ''
8495 # generated by NixOS using the `services.logrotate.paths.${pathOpts.name}` attribute set
85− "${pathOpts.path}" {
96+ ${concatMapStringsSep " " (path: ''"${path}"'') (toList pathOpts.path)} {
8697 ${optionalString (pathOpts.user != null || pathOpts.group != null) "su ${pathOpts.user} ${pathOpts.group}"}
8798 ${pathOpts.frequency}
8899 rotate ${toString pathOpts.keep}
@@ -90,7 +101,7 @@ let
90101 }
91102 '';
92103
93− paths = sortProperties (mapAttrsToList (name: pathOpts: pathOpts // { name = name; }) (filterAttrs (_: pathOpts: pathOpts.enable) cfg.paths));
104+ paths = sortProperties (attrValues (filterAttrs (_: pathOpts: pathOpts.enable) cfg.paths));
94105 configFile = pkgs.writeText "logrotate.conf" (concatStringsSep "\n" ((map mkConf paths) ++ [ cfg.extraConfig ]));
95106
96107in
@@ -152,17 +163,34 @@ in
152163 }
153164 ) cfg.paths;
154165
166+ services.logrotate = {
167+ paths = {
168+ "/var/log/btmp" = {
169+ frequency = mkDefault "monthly";
170+ keep = mkDefault 1;
171+ extraConfig = ''
172+ create 0660 root ${groups.utmp.name}
173+ '';
174+ };
175+ "/var/log/wtmp" = {
176+ frequency = mkDefault "monthly";
177+ keep = mkDefault 1;
178+ extraConfig = ''
179+ create 0664 root ${groups.utmp.name}
180+ '';
181+ };
182+ };
183+ };
184+
155185 systemd.services.logrotate = {
156186 description = "Logrotate Service";
157187 wantedBy = [ "multi-user.target" ];
158188 startAt = "hourly";
159− script = ''
160− exec ${pkgs.logrotate}/sbin/logrotate ${configFile}
161− '';
162189
163190 serviceConfig = {
164191 Restart = "no";
165192 User = "root";
193+ ExecStart = "${pkgs.logrotate}/sbin/logrotate ${configFile}";
166194 };
167195 };
168196 };