1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.journald;
7in {
8 imports = [
9 (mkRenamedOptionModule [ "services" "journald" "enableHttpGateway" ] [ "services" "journald" "gateway" "enable" ])
10 ];
11
12 options = {
13 services.journald.console = mkOption {
14 default = "";
15 type = types.str;
16 description = "If non-empty, write log messages to the specified TTY device.";
17 };
18
19 services.journald.rateLimitInterval = mkOption {
20 default = "30s";
21 type = types.str;
22 description = ''
23 Configures the rate limiting interval that is applied to all
24 messages generated on the system. This rate limiting is applied
25 per-service, so that two services which log do not interfere with
26 each other's limit. The value may be specified in the following
27 units: s, min, h, ms, us. To turn off any kind of rate limiting,
28 set either value to 0.
29
30 See {option}`services.journald.rateLimitBurst` for important
31 considerations when setting this value.
32 '';
33 };
34
35 services.journald.storage = mkOption {
36 default = "persistent";
37 type = types.enum [ "persistent" "volatile" "auto" "none" ];
38 description = ''
39 Controls where to store journal data. See
40 {manpage}`journald.conf(5)` for further information.
41 '';
42 };
43
44 services.journald.rateLimitBurst = mkOption {
45 default = 10000;
46 type = types.int;
47 description = ''
48 Configures the rate limiting burst limit (number of messages per
49 interval) that is applied to all messages generated on the system.
50 This rate limiting is applied per-service, so that two services
51 which log do not interfere with each other's limit.
52
53 Note that the effective rate limit is multiplied by a factor derived
54 from the available free disk space for the journal as described on
55 [
56 journald.conf(5)](https://www.freedesktop.org/software/systemd/man/journald.conf.html).
57
58 Note that the total amount of logs stored is limited by journald settings
59 such as `SystemMaxUse`, which defaults to 10% the file system size
60 (capped at max 4GB), and `SystemKeepFree`, which defaults to 15% of the
61 file system size.
62
63 It is thus recommended to compute what period of time that you will be
64 able to store logs for when an application logs at full burst rate.
65 With default settings for log lines that are 100 Bytes long, this can
66 amount to just a few hours.
67 '';
68 };
69
70 services.journald.extraConfig = mkOption {
71 default = "";
72 type = types.lines;
73 example = "Storage=volatile";
74 description = ''
75 Extra config options for systemd-journald. See man journald.conf
76 for available options.
77 '';
78 };
79
80 services.journald.forwardToSyslog = mkOption {
81 default = config.services.rsyslogd.enable || config.services.syslog-ng.enable;
82 defaultText = literalExpression "services.rsyslogd.enable || services.syslog-ng.enable";
83 type = types.bool;
84 description = ''
85 Whether to forward log messages to syslog.
86 '';
87 };
88 };
89
90 config = {
91 systemd.additionalUpstreamSystemUnits = [
92 "systemd-journald.socket"
93 "systemd-journald@.socket"
94 "systemd-journald-varlink@.socket"
95 "systemd-journald.service"
96 "systemd-journald@.service"
97 "systemd-journal-flush.service"
98 "systemd-journal-catalog-update.service"
99 ] ++ (optional (!config.boot.isContainer) "systemd-journald-audit.socket") ++ [
100 "systemd-journald-dev-log.socket"
101 "syslog.socket"
102 ];
103
104 environment.etc = {
105 "systemd/journald.conf".text = ''
106 [Journal]
107 Storage=${cfg.storage}
108 RateLimitInterval=${cfg.rateLimitInterval}
109 RateLimitBurst=${toString cfg.rateLimitBurst}
110 ${optionalString (cfg.console != "") ''
111 ForwardToConsole=yes
112 TTYPath=${cfg.console}
113 ''}
114 ${optionalString (cfg.forwardToSyslog) ''
115 ForwardToSyslog=yes
116 ''}
117 ${cfg.extraConfig}
118 '';
119 };
120
121 users.groups.systemd-journal.gid = config.ids.gids.systemd-journal;
122
123 systemd.services.systemd-journal-flush.restartIfChanged = false;
124 systemd.services.systemd-journald.restartTriggers = [ config.environment.etc."systemd/journald.conf".source ];
125 systemd.services.systemd-journald.stopIfChanged = false;
126 systemd.services."systemd-journald@".restartTriggers = [ config.environment.etc."systemd/journald.conf".source ];
127 systemd.services."systemd-journald@".stopIfChanged = false;
128 };
129}