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