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.rateLimitBurst = mkOption {
32 default = 10000;
33 type = types.int;
34 description = lib.mdDoc ''
35 Configures the rate limiting burst limit (number of messages per
36 interval) that is applied to all messages generated on the system.
37 This rate limiting is applied per-service, so that two services
38 which log do not interfere with each other's limit.
39
40 Note that the effective rate limit is multiplied by a factor derived
41 from the available free disk space for the journal as described on
42 [
43 journald.conf(5)](https://www.freedesktop.org/software/systemd/man/journald.conf.html).
44
45 Note that the total amount of logs stored is limited by journald settings
46 such as `SystemMaxUse`, which defaults to a 4 GB cap.
47
48 It is thus recommended to compute what period of time that you will be
49 able to store logs for when an application logs at full burst rate.
50 With default settings for log lines that are 100 Bytes long, this can
51 amount to just a few hours.
52 '';
53 };
54
55 services.journald.extraConfig = mkOption {
56 default = "";
57 type = types.lines;
58 example = "Storage=volatile";
59 description = lib.mdDoc ''
60 Extra config options for systemd-journald. See man journald.conf
61 for available options.
62 '';
63 };
64
65 services.journald.enableHttpGateway = mkOption {
66 default = false;
67 type = types.bool;
68 description = lib.mdDoc ''
69 Whether to enable the HTTP gateway to the journal.
70 '';
71 };
72
73 services.journald.forwardToSyslog = mkOption {
74 default = config.services.rsyslogd.enable || config.services.syslog-ng.enable;
75 defaultText = literalExpression "services.rsyslogd.enable || services.syslog-ng.enable";
76 type = types.bool;
77 description = lib.mdDoc ''
78 Whether to forward log messages to syslog.
79 '';
80 };
81 };
82
83 config = {
84 systemd.additionalUpstreamSystemUnits = [
85 "systemd-journald.socket"
86 "systemd-journald@.socket"
87 "systemd-journald-varlink@.socket"
88 "systemd-journald.service"
89 "systemd-journald@.service"
90 "systemd-journal-flush.service"
91 "systemd-journal-catalog-update.service"
92 ] ++ (optional (!config.boot.isContainer) "systemd-journald-audit.socket") ++ [
93 "systemd-journald-dev-log.socket"
94 "syslog.socket"
95 ] ++ optionals cfg.enableHttpGateway [
96 "systemd-journal-gatewayd.socket"
97 "systemd-journal-gatewayd.service"
98 ];
99
100 environment.etc = {
101 "systemd/journald.conf".text = ''
102 [Journal]
103 Storage=persistent
104 RateLimitInterval=${cfg.rateLimitInterval}
105 RateLimitBurst=${toString cfg.rateLimitBurst}
106 ${optionalString (cfg.console != "") ''
107 ForwardToConsole=yes
108 TTYPath=${cfg.console}
109 ''}
110 ${optionalString (cfg.forwardToSyslog) ''
111 ForwardToSyslog=yes
112 ''}
113 ${cfg.extraConfig}
114 '';
115 };
116
117 users.groups.systemd-journal.gid = config.ids.gids.systemd-journal;
118 users.users.systemd-journal-gateway.uid = config.ids.uids.systemd-journal-gateway;
119 users.users.systemd-journal-gateway.group = "systemd-journal-gateway";
120 users.groups.systemd-journal-gateway.gid = config.ids.gids.systemd-journal-gateway;
121
122 systemd.sockets.systemd-journal-gatewayd.wantedBy =
123 optional cfg.enableHttpGateway "sockets.target";
124
125 systemd.services.systemd-journal-flush.restartIfChanged = false;
126 systemd.services.systemd-journald.restartTriggers = [ config.environment.etc."systemd/journald.conf".source ];
127 systemd.services.systemd-journald.stopIfChanged = false;
128 systemd.services."systemd-journald@".restartTriggers = [ config.environment.etc."systemd/journald.conf".source ];
129 systemd.services."systemd-journald@".stopIfChanged = false;
130 };
131}