blob: ba4ca5bbcd0da14e698aff19887e797eab0c75ca (
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
{ config, lib, pkgs, ... }:
let
inherit (builtins)
elem isBool isString ;
inherit (lib)
concatMapStringsSep concatStringsSep filter filterAttrs
mapAttrsToList mkIf mkOption optionalString ;
inherit (lib.types)
attrsOf bool either int listOf nullOr path str submodule ;
cfg = config.nixsap.apps.logrotate;
concatNonEmpty = sep: list: concatStringsSep sep (filter (s: s != "") list);
explicit = filterAttrs (n: v: n != "_module" && v != null);
mandatory = t: mkOption { type = t; };
optional = t: mkOption { type = nullOr t; default = null; };
mkConf = name: opts:
let
files = concatMapStringsSep " " (f: ''"${f}"'') opts.files;
show = k: v:
if elem k ["postrotate" "preremove" "prerotate"]
then " ${k}\n ${v}\n endscript"
else if isBool v then optionalString v " ${k}"
else if isString v then " ${k} ${v}"
else " ${k} ${toString v}";
in pkgs.writeText "logrotate-${name}.conf" ''
${files} {
${concatNonEmpty "\n" (mapAttrsToList show (explicit opts.directives))}
}
'';
configFile = pkgs.writeText "logrotate.conf" ''
compress
compresscmd ${pkgs.gzip}/bin/gzip
compressext .gz
compressoptions -6
rotate 4
uncompresscmd ${pkgs.gzip}/bin/gunzip
${concatMapStringsSep "\n" (f: "include ${f}") (mapAttrsToList mkConf (explicit cfg.conf))}
'';
entry = {
options = {
files = mandatory (listOf path);
directives = {
compress = optional bool;
compresscmd = optional path;
compressext = optional str;
compressoptions = optional str;
copy = optional bool;
copytruncate = optional bool;
create = optional (either bool str);
daily = optional bool;
dateext = optional bool;
dateformat = optional str;
dateyesterday = optional bool;
delaycompress = optional bool;
extension = optional str;
firstaction = optional path;
hourly = optional bool;
ifempty = optional bool;
lastaction = optional path;
mail = optional str;
mailfirst = optional bool;
maillast = optional bool;
maxage = optional int;
maxsize = optional int;
minsize = optional int;
missingok = optional bool;
monthly = optional bool;
nocompress = optional bool;
nocopy = optional bool;
nocopytruncate = optional bool;
nocreate = optional bool;
nodateext = optional bool;
nodelaycompress = optional bool;
nomail = optional bool;
nomissingok = optional bool;
nosharedscripts = optional bool;
notifempty = optional bool;
postrotate = optional path;
preremove = optional path;
prerotate = optional path;
rotate = optional int;
sharedscripts = optional bool;
size = optional int;
su = optional str;
uncompresscmd = optional path;
weekly = optional bool;
yearly = optional bool;
};
};
};
exec = pkgs.writeBashScriptBin "logrotate" ''
exec ${pkgs.logrotate}/bin/logrotate \
-s '${cfg.stateDir}/status' \
${optionalString cfg.verbose " -v"} \
${optionalString (cfg.mail != null) " -m '${cfg.mail}'"} \
${configFile}
'';
in {
options.nixsap.apps.logrotate = {
stateDir = mkOption {
description = "Directory for logrotate state file";
type = path;
default = "/logrotate";
};
conf = mkOption {
description = "Logrotate configuration";
type = attrsOf (submodule entry);
default = {};
};
mail = mkOption {
description = ''
Tells logrotate which command to use when mailing logs. This command
should accept two arguments: 1) the subject of the message, and 2)
the recipient.
'';
type = nullOr path;
default = null;
};
verbose = mkOption {
description = "Turns on verbose mode.";
type = bool;
default = true;
};
startAt = mkOption {
description = "Time to start in systemd format";
type = str;
default = "hourly";
};
};
config = mkIf ({} != explicit cfg.conf) {
systemd.services.logrotate = {
description = "rotates, compresses, and mails system logs";
inherit (cfg) startAt;
preStart = ''
mkdir -p '${cfg.stateDir}'
chown -Rc root:root '${cfg.stateDir}'
chmod -Rc u=rwX,g=rX,o= '${cfg.stateDir}'
'';
serviceConfig = {
ExecStart = "${exec}/bin/logrotate";
};
};
};
}
|