Skip to content

Commit

Permalink
Merge pull request #159187 from martinetd/logrotate
Browse files Browse the repository at this point in the history
logrotate service enhancements
  • Loading branch information
dasJ authored Feb 23, 2022
2 parents 14780cc + a05f1c9 commit e5823f7
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 26 deletions.
7 changes: 7 additions & 0 deletions nixos/doc/manual/from_md/release-notes/rl-2205.section.xml
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,13 @@
Plugins are automatically repackaged using autoPatchelf.
</para>
</listitem>
<listitem>
<para>
<literal>services.logrotate.enable</literal> now defaults to
true if any rotate path has been defined, and some paths have
been added by default.
</para>
</listitem>
<listitem>
<para>
The <literal>zrepl</literal> package has been updated from
Expand Down
3 changes: 3 additions & 0 deletions nixos/doc/manual/release-notes/rl-2205.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ In addition to numerous new and upgraded packages, this release has the followin
- `services.mattermost.plugins` has been added to allow the declarative installation of Mattermost plugins.
Plugins are automatically repackaged using autoPatchelf.

- `services.logrotate.enable` now defaults to true if any rotate path has
been defined, and some paths have been added by default.

- The `zrepl` package has been updated from 0.4.0 to 0.5:

- The RPC protocol version was bumped; all zrepl daemons in a setup must be updated and restarted before replication can resume.
Expand Down
35 changes: 9 additions & 26 deletions nixos/modules/services/logging/logrotate.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ with lib;

let
cfg = config.services.logrotate;
inherit (config.users) groups;

pathOpts = { name, ... }: {
options = {
Expand Down Expand Up @@ -85,10 +84,6 @@ let
};

config.name = name;
config.extraConfig = ''
missingok
notifempty
'';
};

mkConf = pathOpts: ''
Expand All @@ -102,7 +97,11 @@ let
'';

paths = sortProperties (attrValues (filterAttrs (_: pathOpts: pathOpts.enable) cfg.paths));
configFile = pkgs.writeText "logrotate.conf" (concatStringsSep "\n" ((map mkConf paths) ++ [ cfg.extraConfig ]));
configFile = pkgs.writeText "logrotate.conf" (
concatStringsSep "\n" (
[ "missingok" "notifempty" cfg.extraConfig ] ++ (map mkConf paths)
)
);

in
{
Expand All @@ -112,7 +111,10 @@ in

options = {
services.logrotate = {
enable = mkEnableOption "the logrotate systemd service";
enable = mkEnableOption "the logrotate systemd service" // {
default = foldr (n: a: a || n.enable) false (attrValues cfg.paths);
defaultText = literalExpression "cfg.paths != {}";
};

paths = mkOption {
type = with types; attrsOf (submodule pathOpts);
Expand Down Expand Up @@ -163,25 +165,6 @@ in
}
) cfg.paths;

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

systemd.services.logrotate = {
description = "Logrotate Service";
wantedBy = [ "multi-user.target" ];
Expand Down
12 changes: 12 additions & 0 deletions nixos/modules/services/web-servers/nginx/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -988,5 +988,17 @@ in
nginx.gid = config.ids.gids.nginx;
};

services.logrotate.paths.nginx = mapAttrs (_: mkDefault) {
path = "/var/log/nginx/*.log";
frequency = "weekly";
keep = 26;
extraConfig = ''
compress
delaycompress
postrotate
[ ! -f /var/run/nginx/nginx.pid ] || kill -USR1 `cat /var/run/nginx/nginx.pid`
endscript
'';
};
};
}
17 changes: 17 additions & 0 deletions nixos/modules/system/boot/systemd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,23 @@ in
boot.kernel.sysctl."kernel.pid_max" = mkIf pkgs.stdenv.is64bit (lib.mkDefault 4194304);

boot.kernelParams = optional (!cfg.enableUnifiedCgroupHierarchy) "systemd.unified_cgroup_hierarchy=0";

services.logrotate.paths = {
"/var/log/btmp" = mapAttrs (_: mkDefault) {
frequency = "monthly";
keep = 1;
extraConfig = ''
create 0660 root ${config.users.groups.utmp.name}
'';
};
"/var/log/wtmp" = mapAttrs (_: mkDefault) {
frequency = "monthly";
keep = 1;
extraConfig = ''
create 0664 root ${config.users.groups.utmp.name}
'';
};
};
};

# FIXME: Remove these eventually.
Expand Down
1 change: 1 addition & 0 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ in
litestream = handleTest ./litestream.nix {};
locate = handleTest ./locate.nix {};
login = handleTest ./login.nix {};
logrotate = handleTest ./logrotate.nix {};
loki = handleTest ./loki.nix {};
lxd = handleTest ./lxd.nix {};
lxd-image = handleTest ./lxd-image.nix {};
Expand Down
35 changes: 35 additions & 0 deletions nixos/tests/logrotate.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Test logrotate service works and is enabled by default

import ./make-test-python.nix ({ pkgs, ...} : rec {
name = "logrotate";
meta = with pkgs.lib.maintainers; {
maintainers = [ martinetd ];
};

# default machine
machine = { ... }: {
};

testScript =
''
with subtest("whether logrotate works"):
machine.succeed(
# we must rotate once first to create logrotate stamp
"systemctl start --wait logrotate.service",
# wtmp is present in default config.
"rm -f /var/log/wtmp*",
"echo test > /var/log/wtmp",
# move into the future and rotate
"date -s 'now + 1 month + 1 day'",
# systemd will run logrotate from logrotate.timer automatically
# on date change, but if we want to wait for it to terminate
# it's easier to run again...
"systemctl start --wait logrotate.service",
# check rotate worked
"[ -e /var/log/wtmp.1 ]",
)
'';
})
5 changes: 5 additions & 0 deletions pkgs/tools/system/logrotate/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ lib, stdenv, fetchFromGitHub, gzip, popt, autoreconfHook
, mailutils ? null
, aclSupport ? true, acl
, nixosTests
}:

stdenv.mkDerivation rec {
Expand All @@ -25,6 +26,10 @@ stdenv.mkDerivation rec {
nativeBuildInputs = [ autoreconfHook ];
buildInputs = [ popt ] ++ lib.optionals aclSupport [ acl ];

passthru.tests = {
nixos-logrotate = nixosTests.logrotate;
};

meta = with lib; {
homepage = "https://github.com/logrotate/logrotate";
description = "Rotates and compresses system logs";
Expand Down

0 comments on commit e5823f7

Please sign in to comment.