diff --git a/nixos/modules/services/logging/logrotate.nix b/nixos/modules/services/logging/logrotate.nix index 082cf92ff4efe..8dfece8109d3a 100644 --- a/nixos/modules/services/logging/logrotate.nix +++ b/nixos/modules/services/logging/logrotate.nix @@ -97,12 +97,18 @@ let ''; paths = sortProperties (attrValues (filterAttrs (_: pathOpts: pathOpts.enable) cfg.paths)); - configFile = pkgs.writeText "logrotate.conf" ( - concatStringsSep "\n" ( + configText = concatStringsSep "\n" ( [ "missingok" "notifempty" cfg.extraConfig ] ++ (map mkConf paths) - ) - ); - + ); + configFile = pkgs.writeText "logrotate.conf" configText; + + mailOption = + # add mail option to service if a mail is requested in config + # this ugly match will be replaced by cleaner attribute check in + # the near future + if builtins.match "(.*[[:space:]])?mail[[:space:]].*" configText != null + then "--mail=${pkgs.mailutils}/bin/mail" + else ""; in { imports = [ @@ -172,7 +178,7 @@ in serviceConfig = { Restart = "no"; User = "root"; - ExecStart = "${pkgs.logrotate}/sbin/logrotate ${configFile}"; + ExecStart = "${pkgs.logrotate}/sbin/logrotate ${mailOption} ${configFile}"; }; }; }; diff --git a/nixos/tests/logrotate.nix b/nixos/tests/logrotate.nix index 38da8d535275c..85923465593a9 100644 --- a/nixos/tests/logrotate.nix +++ b/nixos/tests/logrotate.nix @@ -1,26 +1,33 @@ # Test logrotate service works and is enabled by default -import ./make-test-python.nix ({ pkgs, ...} : rec { +import ./make-test-python.nix ({ pkgs, ... }: rec { name = "logrotate"; meta = with pkgs.lib.maintainers; { maintainers = [ martinetd ]; }; - # default machine - machine = { ... }: { + nodes = { + defaultMachine = { ... }: { }; + machine = { config, ... }: { + services.logrotate.paths = { + # using mail somewhere should add --mail to logrotate invokation + sendmail = { + extraConfig = "mail user@domain.tld"; + }; + }; + }; }; testScript = '' with subtest("whether logrotate works"): - machine.succeed( - # we must rotate once first to create logrotate stamp - "systemctl start logrotate.service") + # we must rotate once first to create logrotate stamp + defaultMachine.succeed("systemctl start logrotate.service") # we need to wait for console text once here to # clear console buffer up to this point for next wait - machine.wait_for_console_text('logrotate.service: Deactivated successfully') + defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') - machine.succeed( + defaultMachine.succeed( # wtmp is present in default config. "rm -f /var/log/wtmp*", # we need to give it at least 1MB @@ -28,10 +35,14 @@ import ./make-test-python.nix ({ pkgs, ...} : rec { # move into the future and check rotation. "date -s 'now + 1 month + 1 day'") - machine.wait_for_console_text('logrotate.service: Deactivated successfully') - machine.succeed( + defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') + defaultMachine.succeed( # check rotate worked "[ -e /var/log/wtmp.1 ]", ) + with subtest("default config does not have mail"): + defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail") + with subtest("using mails adds mail option"): + machine.succeed("systemctl cat logrotate.service | grep -- --mail") ''; }) diff --git a/pkgs/tools/system/logrotate/default.nix b/pkgs/tools/system/logrotate/default.nix index f0ce08383359c..4c9536cd7cf40 100644 --- a/pkgs/tools/system/logrotate/default.nix +++ b/pkgs/tools/system/logrotate/default.nix @@ -1,5 +1,4 @@ { lib, stdenv, fetchFromGitHub, gzip, popt, autoreconfHook -, mailutils ? null , aclSupport ? true, acl , nixosTests }: @@ -19,8 +18,6 @@ stdenv.mkDerivation rec { configureFlags = [ "--with-compress-command=${gzip}/bin/gzip" "--with-uncompress-command=${gzip}/bin/gunzip" - ] ++ lib.optionals (mailutils != null) [ - "--with-default-mail-command=${mailutils}/bin/mail" ]; nativeBuildInputs = [ autoreconfHook ];