2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-04-29 18:54:08 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2010-04-29 18:54:08 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.logrotate;
|
|
|
|
|
2022-02-28 21:54:12 +00:00
|
|
|
generateLine = n: v:
|
|
|
|
if builtins.elem n [ "files" "priority" "enable" "global" ] || v == null then null
|
2022-08-23 05:40:24 +00:00
|
|
|
else if builtins.elem n [ "frequency" ] then "${v}\n"
|
2022-02-28 21:54:12 +00:00
|
|
|
else if builtins.elem n [ "firstaction" "lastaction" "prerotate" "postrotate" "preremove" ]
|
|
|
|
then "${n}\n ${v}\n endscript\n"
|
|
|
|
else if isInt v then "${n} ${toString v}\n"
|
|
|
|
else if v == true then "${n}\n"
|
|
|
|
else if v == false then "no${n}\n"
|
|
|
|
else "${n} ${v}\n";
|
|
|
|
generateSection = indent: settings: concatStringsSep (fixedWidthString indent " " "") (
|
|
|
|
filter (x: x != null) (mapAttrsToList generateLine settings)
|
|
|
|
);
|
|
|
|
|
|
|
|
# generateSection includes a final newline hence weird closing brace
|
|
|
|
mkConf = settings:
|
|
|
|
if settings.global or false then generateSection 0 settings
|
|
|
|
else ''
|
|
|
|
${concatMapStringsSep "\n" (files: ''"${files}"'') (toList settings.files)} {
|
|
|
|
${generateSection 2 settings}}
|
|
|
|
'';
|
|
|
|
|
|
|
|
settings = sortProperties (attrValues (filterAttrs (_: settings: settings.enable) (
|
|
|
|
foldAttrs recursiveUpdate { } [
|
|
|
|
{
|
|
|
|
header = {
|
|
|
|
enable = true;
|
|
|
|
missingok = true;
|
|
|
|
notifempty = true;
|
|
|
|
frequency = "weekly";
|
|
|
|
rotate = 4;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
cfg.settings
|
|
|
|
{ header = { global = true; priority = 100; }; }
|
|
|
|
]
|
|
|
|
)));
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
configFile = pkgs.writeTextFile {
|
|
|
|
name = "logrotate.conf";
|
|
|
|
text = concatStringsSep "\n" (
|
2022-02-28 21:54:12 +00:00
|
|
|
map mkConf settings
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
);
|
|
|
|
checkPhase = optionalString cfg.checkConfig ''
|
|
|
|
# logrotate --debug also checks that users specified in config
|
|
|
|
# file exist, but we only have sandboxed users here so brown these
|
|
|
|
# out. according to man page that means su, create and createolddir.
|
|
|
|
# files required to exist also won't be present, so missingok is forced.
|
2022-04-01 11:50:06 +00:00
|
|
|
user=$(${pkgs.buildPackages.coreutils}/bin/id -un)
|
|
|
|
group=$(${pkgs.buildPackages.coreutils}/bin/id -gn)
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
sed -e "s/\bsu\s.*/su $user $group/" \
|
|
|
|
-e "s/\b\(create\s\+[0-9]*\s*\|createolddir\s\+[0-9]*\s\+\).*/\1$user $group/" \
|
|
|
|
-e "1imissingok" -e "s/\bnomissingok\b//" \
|
2022-07-08 22:12:43 +00:00
|
|
|
$out > logrotate.conf
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
# Since this makes for very verbose builds only show real error.
|
|
|
|
# There is no way to control log level, but logrotate hardcodes
|
|
|
|
# 'error:' at common log level, so we can use grep, taking care
|
|
|
|
# to keep error codes
|
|
|
|
set -o pipefail
|
2022-07-08 22:12:43 +00:00
|
|
|
if ! ${pkgs.buildPackages.logrotate}/sbin/logrotate -s logrotate.status \
|
|
|
|
--debug logrotate.conf 2>&1 \
|
|
|
|
| ( ! grep "error:" ) > logrotate-error; then
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
echo "Logrotate configuration check failed."
|
|
|
|
echo "The failing configuration (after adjustments to pass tests in sandbox) was:"
|
|
|
|
printf "%s\n" "-------"
|
2022-07-08 22:12:43 +00:00
|
|
|
cat logrotate.conf
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
printf "%s\n" "-------"
|
|
|
|
echo "The error reported by logrotate was as follow:"
|
|
|
|
printf "%s\n" "-------"
|
2022-07-08 22:12:43 +00:00
|
|
|
cat logrotate-error
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
printf "%s\n" "-------"
|
|
|
|
echo "You can disable this check with services.logrotate.checkConfig = false,"
|
|
|
|
echo "but if you think it should work please report this failure along with"
|
|
|
|
echo "the config file being tested!"
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
2022-02-27 10:06:44 +00:00
|
|
|
|
|
|
|
mailOption =
|
2023-03-19 20:44:31 +00:00
|
|
|
optionalString (foldr (n: a: a || (n.mail or false) != false) false (attrValues cfg.settings))
|
|
|
|
"--mail=${pkgs.mailutils}/bin/mail";
|
2010-04-29 18:54:08 +00:00
|
|
|
in
|
|
|
|
{
|
2020-05-13 18:59:28 +00:00
|
|
|
imports = [
|
2022-08-23 05:40:24 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "logrotate" "config" ] "Modify services.logrotate.settings.header instead")
|
|
|
|
(mkRemovedOptionModule [ "services" "logrotate" "extraConfig" ] "Modify services.logrotate.settings.header instead")
|
|
|
|
(mkRemovedOptionModule [ "services" "logrotate" "paths" ] "Add attributes to services.logrotate.settings instead")
|
2020-05-13 18:59:28 +00:00
|
|
|
];
|
|
|
|
|
2010-04-29 18:54:08 +00:00
|
|
|
options = {
|
|
|
|
services.logrotate = {
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "the logrotate systemd service") // {
|
2022-02-28 21:54:12 +00:00
|
|
|
default = foldr (n: a: a || n.enable) false (attrValues cfg.settings);
|
|
|
|
defaultText = literalExpression "cfg.settings != {}";
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
default = { };
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-02-28 21:54:12 +00:00
|
|
|
logrotate freeform settings: each attribute here will define its own section,
|
|
|
|
ordered by priority, which can either define files to rotate with their settings
|
|
|
|
or settings common to all further files settings.
|
2022-07-28 21:19:15 +00:00
|
|
|
Refer to <https://linux.die.net/man/8/logrotate> for details.
|
2022-02-28 21:54:12 +00:00
|
|
|
'';
|
2022-06-12 22:36:08 +00:00
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
# global options
|
|
|
|
header = {
|
|
|
|
dateext = true;
|
|
|
|
};
|
|
|
|
# example custom files
|
|
|
|
"/var/log/mylog.log" = {
|
|
|
|
frequency = "daily";
|
|
|
|
rotate = 3;
|
|
|
|
};
|
|
|
|
"multiple paths" = {
|
|
|
|
files = [
|
|
|
|
"/var/log/first*.log"
|
|
|
|
"/var/log/second.log"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
'';
|
2022-02-28 21:54:12 +00:00
|
|
|
type = types.attrsOf (types.submodule ({ name, ... }: {
|
|
|
|
freeformType = with types; attrsOf (nullOr (oneOf [ int bool str ]));
|
|
|
|
|
|
|
|
options = {
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "setting individual kill switch") // {
|
2022-02-28 21:54:12 +00:00
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
global = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-02-28 21:54:12 +00:00
|
|
|
Whether this setting is a global option or not: set to have these
|
|
|
|
settings apply to all files settings with a higher priority.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
files = mkOption {
|
|
|
|
type = with types; either str (listOf str);
|
|
|
|
default = name;
|
|
|
|
defaultText = ''
|
|
|
|
The attrset name if not specified
|
|
|
|
'';
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-02-28 21:54:12 +00:00
|
|
|
Single or list of files for which rules are defined.
|
|
|
|
The files are quoted with double-quotes in logrotate configuration,
|
|
|
|
so globs and spaces are supported.
|
|
|
|
Note this setting is ignored if globals is true.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
frequency = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-02-28 21:54:12 +00:00
|
|
|
How often to rotate the logs. Defaults to previously set global setting,
|
2022-12-18 00:31:14 +00:00
|
|
|
which itself defaults to weekly.
|
2022-02-28 21:54:12 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
priority = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 1000;
|
2022-08-14 07:03:30 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-02-28 21:54:12 +00:00
|
|
|
Order of this logrotate block in relation to the others. The semantics are
|
|
|
|
the same as with `lib.mkOrder`. Smaller values are inserted first.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}));
|
2022-02-11 08:13:38 +00:00
|
|
|
};
|
2020-05-13 18:59:28 +00:00
|
|
|
|
2022-03-01 13:02:32 +00:00
|
|
|
configFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = configFile;
|
|
|
|
defaultText = ''
|
|
|
|
A configuration file automatically generated by NixOS.
|
|
|
|
'';
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2023-03-23 20:09:47 +00:00
|
|
|
Override the configuration file used by logrotate. By default,
|
2022-08-03 20:46:41 +00:00
|
|
|
NixOS generates one automatically from [](#opt-services.logrotate.settings).
|
2022-03-01 13:02:32 +00:00
|
|
|
'';
|
|
|
|
example = literalExpression ''
|
|
|
|
pkgs.writeText "logrotate.conf" '''
|
|
|
|
missingok
|
|
|
|
"/var/log/*.log" {
|
|
|
|
rotate 4
|
|
|
|
weekly
|
|
|
|
}
|
|
|
|
''';
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
checkConfig = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
Whether the config should be checked at build time.
|
|
|
|
|
|
|
|
Some options are not checkable at build time because of the build sandbox:
|
|
|
|
for example, the test does not know about existing files and system users are
|
|
|
|
not known.
|
|
|
|
These limitations mean we must adjust the file for tests (missingok is forced
|
2022-03-04 22:50:12 +00:00
|
|
|
and users are replaced by dummy users), so tests are complemented by a
|
|
|
|
logrotate-checkconf service that is enabled by default.
|
|
|
|
This extra check can be disabled by disabling it at the systemd level with the
|
2022-07-28 21:19:15 +00:00
|
|
|
{option}`services.systemd.services.logrotate-checkconf.enable` option.
|
logrotate: add configuration check at build time
Now the service no longer starts immediately,
check if the config we generated makes sense as soon as possible.
The check isn't perfect because logrotate --debug wants to check
users required, there are two problems:
- /etc/passwd and /etc/group are sandboxed and we don't have
visibility of system users
- the check phase runs as nixbld which cannot su to other users
and logrotate fails on this
Until these two problems can be addressed, users-related checks
are filtered out, it's still much better than no check.
The check can be disabled with services.logrotate.checkConfig
if required
(bird also has a preCheck param, to prepare the environment
before check, but we can add it if it becomes necessary)
Since this makes for very verbose builds, we only show errors:
There is no way to control log level, but logrotate hardcodes
'error:' at common log level, so we can use grep, taking care
to keep error codes
Some manual tests:
───────┬──────────────────────────────────────────
│ File: valid-config.conf
───────┼──────────────────────────────────────────
1 │ missingok
───────┴──────────────────────────────────────────
logrotate --debug ok
grep ok
───────┬──────────────────────────────────────────
│ File: postrotate-no-end.conf
───────┼──────────────────────────────────────────
1 │ missingok
2 │ /file {
3 │ postrotate
4 │ test
5 │ }
───────┴──────────────────────────────────────────
error: postrotate-no-end.conf:prerotate, postrotate or preremove without endscript
───────┬──────────────────────────────────────────
│ File: missing-file.conf
───────┼──────────────────────────────────────────
1 │ "test" { daily }
───────┴──────────────────────────────────────────
error: stat of test failed: No such file or directory
───────┬──────────────────────────────────────────
│ File: unknown-option.conf
───────┼──────────────────────────────────────────
1 │ some syntax error
───────┴──────────────────────────────────────────
logrotate --debug ok
error: unknown-option.conf:1 unknown option 'some' -- ignoring line
───────┬──────────────────────────────────────────
│ File: unknown-user.conf
───────┼──────────────────────────────────────────
1 │ su notauser notagroup
───────┴──────────────────────────────────────────
error: unknown-user.conf:1 unknown user 'notauser'
In particular note that logrotate would not error on unknown option
(it just ignores the line) but this change makes the check fail.
2022-03-02 13:12:45 +00:00
|
|
|
|
|
|
|
Conversely there are still things that might make this check fail incorrectly
|
|
|
|
(e.g. a file path where we don't have access to intermediate directories):
|
|
|
|
in this case you can disable the failing check with this option.
|
|
|
|
'';
|
|
|
|
};
|
2010-04-29 18:54:08 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2020-08-19 16:36:54 +00:00
|
|
|
systemd.services.logrotate = {
|
|
|
|
description = "Logrotate Service";
|
2021-09-03 21:17:28 +00:00
|
|
|
startAt = "hourly";
|
2020-08-19 16:36:54 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "no";
|
|
|
|
User = "root";
|
2022-03-01 13:02:32 +00:00
|
|
|
ExecStart = "${pkgs.logrotate}/sbin/logrotate ${mailOption} ${cfg.configFile}";
|
2020-08-19 16:36:54 +00:00
|
|
|
};
|
2014-07-10 11:11:35 +00:00
|
|
|
};
|
2022-03-04 22:50:12 +00:00
|
|
|
systemd.services.logrotate-checkconf = {
|
|
|
|
description = "Logrotate configuration check";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
ExecStart = "${pkgs.logrotate}/sbin/logrotate --debug ${cfg.configFile}";
|
|
|
|
};
|
|
|
|
};
|
2010-04-29 18:54:08 +00:00
|
|
|
};
|
|
|
|
}
|