summaryrefslogtreecommitdiffstats
path: root/modules/system/newsyslog.nix
blob: 022db77c57738c518529046b9d6820afe2fad935 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
{ config, lib, ... }:

let
  inherit (lib) mkOption types;

  cfg = config.system.newsyslog;
in
{
  options = {
    system.newsyslog = {
      enable = lib.mkEnableOption "newsyslog configuration management";

      files = mkOption {
        type = types.attrsOf (
          types.listOf (
            types.submodule {
              options = {
                logfilename = mkOption {
                  type = types.str;
                  description = ''
                    Path to the log file to rotate, or the literal string `<default>`.

                    The `<default>` entry applies when newsyslog is invoked with an explicit
                    log file argument that does not match another entry.
                  '';
                  example = "/var/log/myapp.log";
                };

                owner = mkOption {
                  type = types.nullOr types.str;
                  default = null;
                  description = ''
                    Owner of the log file.

                    If `null`, the owner portion of `owner:group` is left blank.
                    See `newsyslog.conf(5)` for default ownership behavior (`root:admin`).
                  '';
                  example = "myuser";
                };

                group = mkOption {
                  type = types.nullOr types.str;
                  default = null;
                  description = ''
                    Group of the log file.

                    If `null`, the group portion of `owner:group` is left blank.
                    See `newsyslog.conf(5)` for default ownership behavior (`root:admin`).
                  '';
                  example = "wheel";
                };

                mode = mkOption {
                  type = types.str;
                  default = "644";
                  description = ''
                    File mode (permissions) for the rotated log file.
                  '';
                  example = "600";
                };

                count = mkOption {
                  type = types.ints.unsigned;
                  default = 5;
                  description = ''
                    Number of rotated log files to keep.
                  '';
                };

                size = mkOption {
                  type = types.nullOr types.str;
                  default = null;
                  description = ''
                    Maximum size (in KB) before rotation (e.g., "100", "1024", "4096").

                    If `null`, size-based rotation is disabled.
                  '';
                  example = "2048";
                };

                when = mkOption {
                  type = types.nullOr types.str;
                  default = null;
                  description = ''
                    Time-based rotation schedule.

                    The value may be an interval in hours, a specific time, or both.
                    If both are specified, both conditions must be satisfied.

                    Examples:
                    - Interval only: 24
                    - Daily: @T00 (daily at midnight)
                    - Interval and time: 24@T00
                    - Weekly: $W0D00 (weekly on Sunday at midnight)
                    - Monthly: $M1D00 (monthly on the 1st at midnight)

                    If `null`, time-based rotation is disabled.
                  '';
                  example = "@T00";
                };

                flags = mkOption {
                  type = types.listOf (
                    types.enum [
                      "B"
                      "C"
                      "D"
                      "G"
                      "J"
                      "N"
                      "U"
                      "Z"
                      "-"
                    ]
                  );
                  default = [ ];
                  description = ''
                    List of flags for newsyslog.
                    - "B" - Binary file, don't add status message
                    - "C" - Create log file if missing when newsyslog runs with `-C`/`-CC`
                    - "D" - Mark rotated files with the UF_NODUMP flag
                    - "G" - Indicates that logfilename is a shell pattern
                    - "J" - Compress rotated logs with bzip2
                    - "N" - Do not signal a daemon after rotating this log
                    - "U" - pidFile points to a process group ID (negative value)
                    - "Z" - Compress rotated logs with gzip
                    - "-" - Placeholder when specifying pidFile/signal without other flags
                  '';
                  example = [
                    "Z"
                    "C"
                  ];
                };

                pidFile = mkOption {
                  type = types.nullOr types.str;
                  default = null;
                  description = ''
                    Path to PID file.

                    If specified, the process will be signaled after rotation.
                    This path must be absolute (start with `/`).
                  '';
                  example = "/var/run/myapp.pid";
                };

                signal = mkOption {
                  type = types.nullOr types.str;
                  default = null;
                  description = ''
                    Signal to send to the process (default is SIGHUP if pidFile is specified).
                    Can be signal name (HUP, USR1, etc.) or number.
                  '';
                  example = "USR1";
                };
              };
            }
          )
        );
        default = { };
        description = ''
          Attribute set of newsyslog configuration files to create in /etc/newsyslog.d/.

          Each attribute name becomes the filename (with .conf extension automatically added).
          Each attribute value is a list of log rotation entries.
        '';
        example = lib.literalExpression ''
          {
            myapp = [
              {
                logfilename = "/var/log/myapp.log";
                owner = "myuser";
                group = "wheel";
                mode = "644";
                count = 7;
                size = "10M";
                flags = [ "Z" "C" ];
                pidFile = "/var/run/myapp.pid";
                signal = "USR1";
              }
            ];
            system = [
              {
                logfilename = "/var/log/system.log";
                mode = "644";
                count = 5;
                when = "@T00";
                flags = [ "Z" ];
              }
            ];
          }
        '';
      };
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = lib.flatten (
      lib.mapAttrsToList (
        fileName: entries:
        lib.flatten (
          lib.imap0 (index: entry: [
            {
              assertion = entry.signal == null || entry.pidFile != null;
              message = "system.newsyslog.files.${fileName}[${toString index}].signal requires pidFile to be set.";
            }
            {
              assertion = entry.pidFile == null || builtins.match "/.*" entry.pidFile != null;
              message = "system.newsyslog.files.${fileName}[${toString index}].pidFile must be an absolute path starting with '/'.";
            }
          ]) entries
        )
      ) cfg.files
    );

    environment.etc =
      let
        orStar = value: if value == null then "*" else value;

        formatOwnerGroup =
          entry:
          "${lib.optionalString (entry.owner != null) entry.owner}:${
            lib.optionalString (entry.group != null) entry.group
          }";

        formatFlags =
          entry:
          lib.optional (entry.flags != [ ]) (lib.concatStrings entry.flags)
          ++ lib.optional (entry.flags == [ ] && (entry.pidFile != null || entry.signal != null)) "-";

        formatEntry =
          entry:
          lib.concatStringsSep "\t" (
            [
              entry.logfilename
            ]
            ++ lib.optional (entry.owner != null || entry.group != null) (formatOwnerGroup entry)
            ++ [
              entry.mode
              (toString entry.count)
              (orStar entry.size)
              (orStar entry.when)
            ]
            ++ formatFlags entry
            ++ lib.optionals (entry.pidFile != null) [ entry.pidFile ]
            ++ lib.optionals (entry.signal != null) [ entry.signal ]
          );

        generateFileContent = entries: ''
          # Generated by nix-darwin
          # logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]
          ${lib.concatMapStringsSep "\n" formatEntry entries}
        '';
      in
      lib.mapAttrs' (name: entries: {
        name = "newsyslog.d/${name}.conf";
        value.text = generateFileContent entries;
      }) cfg.files;
  };
}