blob: b4ed76b031e7cc00f61deb087a1676df395107f8 (
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
|
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.nezha;
# nezha uses yaml as the configuration file format.
# Since we need to use jq to update the content, so here we generate json
settingsFormat = pkgs.formats.json { };
configFile = settingsFormat.generate "config.json" cfg.settings;
in
{
meta.maintainers = with lib.maintainers; [ moraxyc ];
options = {
services.nezha = {
enable = lib.mkEnableOption "Nezha Monitoring";
package = lib.mkPackageOption pkgs "nezha" { };
debug = lib.mkEnableOption "verbose log";
settings = lib.mkOption {
description = ''
Generate to {file}`config.yaml` as a Nix attribute set.
Check the [guide](https://nezha.wiki/en_US/guide/dashboard.html)
for possible options.
'';
type = lib.types.submodule {
freeformType = settingsFormat.type;
options = {
listenhost = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
description = ''
Host on which the nezha web interface and grpc should listen.
'';
};
listenport = lib.mkOption {
type = lib.types.port;
default = 8008;
description = ''
Port on which the nezha web interface and grpc should listen.
'';
};
};
};
};
mutableConfig = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Whether the config.yaml is writable by Nezha.
If this option is disabled, changes on the web interface won't
be possible. If an config.yaml is present, it will be overwritten.
'';
};
jwtSecretFile = lib.mkOption {
type = lib.types.path;
default = null;
description = ''
Path to the file containing the secret to sign web requests using JSON Web Tokens.
'';
};
agentSecretFile = lib.mkOption {
type = lib.types.path;
default = null;
description = ''
Path to the file containing the secret used by agents to connect.
'';
};
extraThemes = lib.mkOption {
type = lib.types.listOf lib.types.package;
default = [ ];
example = lib.literalExpression "[ pkgs.nezha-theme-nazhua ]";
description = ''
A list of additional themes.
'';
};
};
};
config = lib.mkIf cfg.enable {
services.nezha.settings.debug = cfg.debug;
systemd.services.nezha = {
serviceConfig = {
Restart = "on-failure";
StateDirectory = "nezha";
RuntimeDirectory = "nezha";
ConfigurationDirectory = "nezha";
WorkingDirectory = "/var/lib/nezha";
ReadWritePaths = [
"/var/lib/nezha"
"/etc/nezha"
];
LoadCredential = [
"jwt-secret:${cfg.jwtSecretFile}"
"agent-secret:${cfg.agentSecretFile}"
];
# Hardening
ProcSubset = "pid";
DynamicUser = true;
RemoveIPC = true;
LockPersonality = true;
ProtectClock = true;
MemoryDenyWriteExecute = true;
PrivateUsers = cfg.settings.listenport >= 1024; # incompatible with CAP_NET_BIND_SERVICE
ProtectHostname = true;
RestrictSUIDSGID = true;
CapabilityBoundingSet = lib.optionalString (cfg.settings.listenport < 1024) "CAP_NET_BIND_SERVICE";
AmbientCapabilities = lib.optionalString (cfg.settings.listenport < 1024) "CAP_NET_BIND_SERVICE";
NoNewPrivileges = true;
PrivateTmp = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectProc = "invisible";
ProtectSystem = "strict";
RestrictNamespaces = true;
RestrictRealtime = true;
SystemCallArchitectures = "native";
UMask = "0066";
SystemCallFilter = [
"@system-service"
"~@privileged"
]
++ lib.optional (cfg.settings ? tsdb) "mincore";
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
];
PrivateDevices = "yes";
ExecStart =
let
package = cfg.package.override { withThemes = cfg.extraThemes; };
in
''${lib.getExe package} -c "''${CONFIGURATION_DIRECTORY}"/config.yaml -db "''${STATE_DIRECTORY}"/sqlite.db'';
};
enableStrictShellChecks = true;
startLimitIntervalSec = 10;
startLimitBurst = 3;
preStart = ''
cp "${configFile}" "''${RUNTIME_DIRECTORY}"/new
${lib.getExe pkgs.jq} \
--arg jwt_secret "$(<"''${CREDENTIALS_DIRECTORY}"/jwt-secret)" \
--arg agent_secret "$(<"''${CREDENTIALS_DIRECTORY}"/agent-secret)" \
'. + { jwtsecretkey: $jwt_secret, agentsecretkey: $agent_secret }' \
< "''${RUNTIME_DIRECTORY}"/new > "''${RUNTIME_DIRECTORY}"/tmp
mv "''${RUNTIME_DIRECTORY}"/tmp "''${RUNTIME_DIRECTORY}"/new
${lib.optionalString cfg.mutableConfig ''
[ -e "''${CONFIGURATION_DIRECTORY}"/config.yaml ] && \
${lib.getExe pkgs.yj} < "''${CONFIGURATION_DIRECTORY}"/config.yaml > "''${RUNTIME_DIRECTORY}"/old && \
${lib.getExe pkgs.jq} -s '.[0] * .[1]' \
"''${RUNTIME_DIRECTORY}"/old "''${RUNTIME_DIRECTORY}"/new > "''${RUNTIME_DIRECTORY}"/tmp
[ -e "''${RUNTIME_DIRECTORY}"/old ] && rm "''${RUNTIME_DIRECTORY}"/old
[ -e "''${RUNTIME_DIRECTORY}"/tmp ] && mv "''${RUNTIME_DIRECTORY}"/tmp "''${RUNTIME_DIRECTORY}"/new
''}
mv "''${RUNTIME_DIRECTORY}"/new "''${CONFIGURATION_DIRECTORY}"/config.yaml
'';
wantedBy = [ "multi-user.target" ];
};
};
}
|