-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathnats.nix
More file actions
297 lines (263 loc) · 8.27 KB
/
nats.nix
File metadata and controls
297 lines (263 loc) · 8.27 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.nats;
# Port allocation
basePort = cfg.port;
baseMonitoringPort = cfg.monitoring.port;
allocatedPort = config.processes.nats.ports.main.value;
allocatedMonitoringPort = config.processes.nats.ports.monitoring.value;
# Generate NATS config file from settings (only if settings are provided)
configFile = pkgs.writeText "nats.conf" (builtins.toJSON cfg.settings);
# Build command-line arguments
buildArgs = concatStringsSep " " (
[ "-a ${cfg.host}" ]
++ [ "-p ${toString allocatedPort}" ]
++ optional (cfg.serverName != "") "-n ${cfg.serverName}"
++ optional (cfg.clientAdvertise != "") "--client_advertise ${cfg.clientAdvertise}"
++ optional cfg.jetstream.enable "-js"
++ optional cfg.monitoring.enable "-m ${toString allocatedMonitoringPort}"
++ optional (cfg.logFile != "") "-l ${cfg.logFile}"
++ optional cfg.debug "-D"
++ optional cfg.trace "-V"
++ optional (cfg.settings != { }) "-c ${configFile}"
);
in
{
options.services.nats = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable the NATS messaging server.
NATS is a simple, secure and high performance messaging
system for cloud native applications, IoT messaging,
and microservices architectures.
'';
};
package = mkOption {
type = types.package;
default = pkgs.nats-server;
defaultText = literalExpression "pkgs.nats-server";
description = ''
Which NATS server package to use.
'';
};
host = mkOption {
type = types.str;
default = "127.0.0.1";
example = "0.0.0.0";
description = ''
Network host to listen on for client connections.
Set to "0.0.0.0" to listen on all interfaces.
Default is localhost for security.
'';
};
port = mkOption {
type = types.port;
default = 4222;
description = ''
Port to listen on for client connections.
'';
};
serverName = mkOption {
type = types.str;
default = "";
example = "nats-dev-1";
description = ''
Server name for identification in clusters.
If empty, NATS will auto-generate a unique name.
'';
};
clientAdvertise = mkOption {
type = types.str;
default = "";
example = "localhost:4222";
description = ''
Client URL to advertise to other servers in a cluster.
Useful when running behind NAT or in containers.
'';
};
jetstream = {
enable = mkEnableOption "JetStream persistence layer for streaming and queues";
maxMemory = mkOption {
type = types.str;
default = "1G";
example = "512M";
description = ''
Maximum memory for in-memory streams.
Use suffixes: K, M, G, T for sizes.
'';
};
maxFileStore = mkOption {
type = types.str;
default = "10G";
example = "100G";
description = ''
Maximum disk space for file-based streams.
Use suffixes: K, M, G, T for sizes.
'';
};
};
monitoring = {
enable = mkOption {
type = types.bool;
default = true;
description = ''
Enable HTTP monitoring endpoint.
Provides /healthz, /varz, /connz, and other monitoring endpoints.
Highly recommended for production deployments.
'';
};
port = mkOption {
type = types.port;
default = 8222;
description = ''
Port for HTTP monitoring endpoint.
Access monitoring at http://host:port/varz
'';
};
};
logFile = mkOption {
type = types.str;
default = "";
example = "/var/log/nats-server.log";
description = ''
Path to log file. If empty, logs to stdout.
Stdout is recommended for devenv as logs are captured by process manager.
'';
};
debug = mkOption {
type = types.bool;
default = false;
description = ''
Enable debug logging for troubleshooting.
'';
};
trace = mkOption {
type = types.bool;
default = false;
description = ''
Enable protocol tracing for deep debugging.
Warning: Very verbose output.
'';
};
authorization = {
enable = mkEnableOption "authorization for client connections";
user = mkOption {
type = types.str;
default = "";
example = "nats-user";
description = ''
Username required for client connections.
Only used if authorization is enabled.
'';
};
password = mkOption {
type = types.str;
default = "";
example = "nats-pass";
description = ''
Password required for client connections.
Only used if authorization is enabled.
Warning: This will be visible in the Nix store.
'';
};
token = mkOption {
type = types.str;
default = "";
example = "my-secret-token";
description = ''
Token required for client connections.
Alternative to user/password authentication.
Warning: This will be visible in the Nix store.
'';
};
};
settings = mkOption {
type = types.attrs;
default = { };
example = literalExpression ''
{
tls = {
cert_file = "/path/to/cert.pem";
key_file = "/path/to/key.pem";
verify = true;
};
cluster = {
name = "my-cluster";
listen = "0.0.0.0:6222";
routes = [
"nats://node1:6222"
"nats://node2:6222"
];
};
}
'';
description = ''
Additional NATS server configuration as a Nix attribute set.
This will be converted to NATS config file format.
Use this for advanced features like:
- TLS/SSL configuration
- Clustering with routes
- MQTT gateway
- WebSocket support
- Custom authorization
See https://docs.nats.io/running-a-nats-service/configuration
'';
};
};
config = mkIf cfg.enable {
packages = [ cfg.package ];
# Merge basic options into settings for config file generation
# Note: User-provided cfg.settings will be automatically merged by the module system
services.nats.settings = mkMerge [
# JetStream settings (if enabled)
(mkIf cfg.jetstream.enable {
jetstream = {
store_dir = config.env.DEVENV_STATE + "/nats/jetstream";
max_memory_store = cfg.jetstream.maxMemory;
max_file_store = cfg.jetstream.maxFileStore;
};
})
# Authorization settings (if enabled)
(mkIf cfg.authorization.enable (
{
authorization = { } //
(optionalAttrs (cfg.authorization.user != "") { user = cfg.authorization.user; }) //
(optionalAttrs (cfg.authorization.password != "") { password = cfg.authorization.password; }) //
(optionalAttrs (cfg.authorization.token != "") { token = cfg.authorization.token; });
}
))
];
env.NATS_PORT = allocatedPort;
env.NATS_MONITORING_PORT = allocatedMonitoringPort;
env.NATS_DATA_DIR = config.env.DEVENV_STATE + "/nats";
# Create necessary directories
enterShell = ''
# Create NATS data directory
mkdir -p ${config.env.DEVENV_STATE}/nats
# Create JetStream directory if enabled
${optionalString cfg.jetstream.enable ''
mkdir -p ${config.env.DEVENV_STATE}/nats/jetstream
''}
'';
processes.nats = {
ports.main.allocate = basePort;
ports.monitoring.allocate = baseMonitoringPort;
exec = "exec ${cfg.package}/bin/nats-server ${buildArgs}";
ready = {
# Use HTTP healthz endpoint if monitoring is enabled, otherwise TCP check
exec =
if cfg.monitoring.enable then
"${pkgs.curl}/bin/curl -f http://${cfg.host}:${toString allocatedMonitoringPort}/healthz"
else
"${pkgs.netcat}/bin/nc -z ${cfg.host} ${toString allocatedPort}";
initial_delay = 2;
period = 5;
probe_timeout = 3;
failure_threshold = 5;
};
};
};
}