blob: 4508fdc1f339a83c8e81e0bb9d5afee00380ce43 (
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
|
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.nats;
format = pkgs.formats.json { };
validateConfig =
file:
pkgs.callPackage (
{ runCommand, nats-server }:
runCommand "validate-nats-conf"
{
nativeBuildInputs = [ nats-server ];
}
''
nats-server --config "${file}" -t
ln -s "${file}" "$out"
''
) { };
unvalidatedConfigFile = format.generate "nats.conf" cfg.settings;
configFile =
if cfg.validateConfig then validateConfig unvalidatedConfigFile else unvalidatedConfigFile;
in
{
### Interface
options = {
services.nats = {
enable = mkEnableOption "NATS messaging system";
user = mkOption {
type = types.str;
default = "nats";
description = "User account under which NATS runs.";
};
group = mkOption {
type = types.str;
default = "nats";
description = "Group under which NATS runs.";
};
serverName = mkOption {
default = "nats";
example = "n1-c3";
type = types.str;
description = ''
Name of the NATS server, must be unique if clustered.
'';
};
jetstream = mkEnableOption "JetStream";
port = mkOption {
default = 4222;
type = types.port;
description = ''
Port on which to listen.
'';
};
dataDir = mkOption {
default = "/var/lib/nats";
type = types.path;
description = ''
The NATS data directory. Only used if JetStream is enabled, for
storing stream metadata and messages.
If left as the default value this directory will automatically be
created before the NATS server starts, otherwise the sysadmin is
responsible for ensuring the directory exists with appropriate
ownership and permissions.
'';
};
settings = mkOption {
default = { };
type = format.type;
example = literalExpression ''
{
jetstream = {
max_mem = "1G";
max_file = "10G";
};
};
'';
description = ''
Declarative NATS configuration. See the
[
NATS documentation](https://docs.nats.io/nats-server/configuration) for a list of options.
'';
};
validateConfig = mkOption {
type = types.bool;
default = true;
description = ''
If true, validate nats config at build time. When the config can't
be checked during build time, for example when it includes other
files, disable this option.
'';
};
};
};
### Implementation
config = mkIf cfg.enable {
services.nats.settings = {
server_name = cfg.serverName;
port = cfg.port;
jetstream = optionalAttrs cfg.jetstream { store_dir = cfg.dataDir; };
};
systemd.services.nats = {
description = "NATS messaging system";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = mkMerge [
(mkIf (cfg.dataDir == "/var/lib/nats") {
StateDirectory = "nats";
StateDirectoryMode = "0750";
})
{
Type = "simple";
ExecStart = "${pkgs.nats-server}/bin/nats-server -c ${configFile}";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
KillMode = "mixed";
KillSignal = "SIGUSR2";
SuccessExitStatus = [
0
"SIGUSR2"
];
TimeoutStopSec = "150"; # must exceed lame_duck_duration, which defaults to 2min
Restart = "on-failure";
User = cfg.user;
Group = cfg.group;
# Hardening
CapabilityBoundingSet = "";
LimitNOFILE = 800000; # JetStream requires 2 FDs open per stream.
LockPersonality = true;
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
PrivateDevices = true;
PrivateTmp = true;
PrivateUsers = true;
ProcSubset = "pid";
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectProc = "invisible";
ProtectSystem = "strict";
ReadOnlyPaths = [ ];
ReadWritePaths = [ cfg.dataDir ];
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallFilter = [
"@system-service"
"~@privileged"
];
UMask = "0077";
}
];
};
users.users = mkIf (cfg.user == "nats") {
nats = {
description = "NATS daemon user";
isSystemUser = true;
group = cfg.group;
home = cfg.dataDir;
};
};
users.groups = mkIf (cfg.group == "nats") { nats = { }; };
};
}
|