1 { config, lib, pkgs, ... }:
3 cfg = config.services.journalwatch;
6 group = "systemd-journal";
7 dataDir = "/var/lib/${user}";
9 journalwatchConfig = pkgs.writeText "config" (''
10 # (File Generated by NixOS journalwatch module.)
12 mail_binary = ${cfg.mailBinary}
13 priority = ${toString cfg.priority}
14 mail_from = ${cfg.mailFrom}
16 + lib.optionalString (cfg.mailTo != null) ''
17 mail_to = ${cfg.mailTo}
21 journalwatchPatterns = pkgs.writeText "patterns" ''
22 # (File Generated by NixOS journalwatch module.)
24 ${mkPatterns cfg.filterBlocks}
27 # empty line at the end needed to to separate the blocks
28 mkPatterns = filterBlocks: lib.concatStringsSep "\n" (map (block: ''
34 # can't use joinSymlinks directly, because when we point $XDG_CONFIG_HOME
35 # to the /nix/store path, we still need the subdirectory "journalwatch" inside that
36 # to match journalwatch's expectations
37 journalwatchConfigDir = pkgs.runCommand "journalwatch-config"
38 { preferLocalBuild = true; allowSubstitutes = false; }
40 mkdir -p $out/journalwatch
41 ln -sf ${journalwatchConfig} $out/journalwatch/config
42 ln -sf ${journalwatchPatterns} $out/journalwatch/patterns
48 services.journalwatch = {
49 enable = lib.mkOption {
50 type = lib.types.bool;
53 If enabled, periodically check the journal with journalwatch and report the results by mail.
57 package = lib.mkPackageOption pkgs "journalwatch" { };
59 priority = lib.mkOption {
63 Lowest priority of message to be considered.
64 A value between 7 ("debug"), and 0 ("emerg"). Defaults to 6 ("info").
65 If you don't care about anything with "info" priority, you can reduce
66 this to e.g. 5 ("notice") to considerably reduce the amount of
67 messages without needing many {option}`filterBlocks`.
71 # HACK: this is a workaround for journalwatch's usage of socket.getfqdn() which always returns localhost if
72 # there's an alias for the localhost on a separate line in /etc/hosts, or take for ages if it's not present and
73 # then return something right-ish in the direction of /etc/hostname. Just bypass it completely.
74 mailFrom = lib.mkOption {
76 default = "journalwatch@${config.networking.hostName}";
77 defaultText = lib.literalExpression ''"journalwatch@''${config.networking.hostName}"'';
79 Mail address to send journalwatch reports from.
83 mailTo = lib.mkOption {
84 type = lib.types.nullOr lib.types.str;
87 Mail address to send journalwatch reports to.
91 mailBinary = lib.mkOption {
92 type = lib.types.path;
93 default = "/run/wrappers/bin/sendmail";
95 Sendmail-compatible binary to be used to send the messages.
99 extraConfig = lib.mkOption {
100 type = lib.types.str;
103 Extra lines to be added verbatim to the journalwatch/config configuration file.
104 You can add any commandline argument to the config, without the '--'.
105 See `journalwatch --help` for all arguments and their description.
109 filterBlocks = lib.mkOption {
110 type = lib.types.listOf (lib.types.submodule {
112 match = lib.mkOption {
113 type = lib.types.str;
114 example = "SYSLOG_IDENTIFIER = systemd";
116 Syntax: `field = value`
117 Specifies the log entry `field` this block should apply to.
118 If the `field` of a message matches this `value`,
119 this patternBlock's {option}`filters` are applied.
120 If `value` starts and ends with a slash, it is interpreted as
121 an extended python regular expression, if not, it's an exact match.
122 The journal fields are explained in systemd.journal-fields(7).
126 filters = lib.mkOption {
127 type = lib.types.str;
129 (Stopped|Stopping|Starting|Started) .*
130 (Reached target|Stopped target) .*
133 The filters to apply on all messages which satisfy {option}`match`.
134 Any of those messages that match any specified filter will be removed from journalwatch's output.
135 Each filter is an extended Python regular expression.
136 You can specify multiple filters and separate them by newlines.
137 Lines starting with '#' are comments. Inline-comments are not permitted.
144 # examples taken from upstream
146 match = "_SYSTEMD_UNIT = systemd-logind.service";
148 New session [a-z]?\d+ of user \w+\.
149 Removed session [a-z]?\d+\.
154 match = "SYSLOG_IDENTIFIER = /(CROND|crond)/";
156 pam_unix\(crond:session\): session (opened|closed) for user \w+
162 # another example from upstream.
163 # very useful on priority = 6, and required as journalwatch throws an error when no pattern is defined at all.
166 match = "SYSLOG_IDENTIFIER = systemd";
168 (Stopped|Stopping|Starting|Started) .*
169 (Created slice|Removed slice) user-\d*\.slice\.
170 Received SIGRTMIN\+24 from PID .*
171 (Reached target|Stopped target) .*
172 Startup finished in \d*ms\.
179 filterBlocks can be defined to blacklist journal messages which are not errors.
180 Each block matches on a log entry field, and the filters in that block then are matched
181 against all messages with a matching log entry field.
183 All messages whose PRIORITY is at least 6 (INFO) are processed by journalwatch.
184 If you don't specify any filterBlocks, PRIORITY is reduced to 5 (NOTICE) by default.
186 All regular expressions are extended Python regular expressions, for details
187 see: http://doc.pyschools.com/html/regex.html
191 interval = lib.mkOption {
192 type = lib.types.str;
195 How often to run journalwatch.
197 The format is described in systemd.time(7).
200 accuracy = lib.mkOption {
201 type = lib.types.str;
204 The time window around the interval in which the journalwatch run will be scheduled.
206 The format is described in systemd.time(7).
212 config = lib.mkIf cfg.enable {
214 users.users.${user} = {
220 systemd.tmpfiles.rules = [
221 # present since NixOS 19.09: remove old stateful symlink join directory,
222 # which has been replaced with the journalwatchConfigDir store path
223 "R ${dataDir}/config"
226 systemd.services.journalwatch = {
229 # journalwatch stores the last processed timpestamp here
230 # the share subdirectory is historic now that config home lives in /nix/store,
231 # but moving this in a backwards-compatible way is much more work than what's justified
232 # for cleaning that up.
233 XDG_DATA_HOME = "${dataDir}/share";
234 XDG_CONFIG_HOME = journalwatchConfigDir;
240 # requires a relative directory name to create beneath /var/lib
241 StateDirectory = user;
242 StateDirectoryMode = "0750";
243 ExecStart = "${lib.getExe cfg.package} mail";
244 # lowest CPU and IO priority, but both still in best-effort class to prevent starvation
246 IOSchedulingPriority=7;
250 systemd.timers.journalwatch = {
251 description = "Periodic journalwatch run";
252 wantedBy = [ "timers.target" ];
254 OnCalendar = cfg.interval;
255 AccuracySec = cfg.accuracy;
263 maintainers = with lib.maintainers; [ florianjacob ];