blob: e4b0bc2d09d92726b77e8b0778f3b13bf6d2b8ff (
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
|
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.grafana-to-ntfy;
in
{
meta.maintainers = with lib.maintainers; [ kittyandrew ];
options = {
services.grafana-to-ntfy = {
enable = lib.mkEnableOption "grafana-to-ntfy, a Grafana/Alertmanager to ntfy.sh bridge";
package = lib.mkPackageOption pkgs "grafana-to-ntfy" { };
settings = {
ntfyUrl = lib.mkOption {
type = lib.types.str;
description = "The URL to the ntfy-sh topic.";
example = "https://push.example.com/grafana";
};
ntfyBAuthUser = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = ''
The ntfy-sh user to use for authenticating with the ntfy-sh instance.
Setting this option is required when using a ntfy-sh instance with access control enabled.
'';
default = null;
example = "grafana";
};
ntfyBAuthPass = lib.mkOption {
type = lib.types.nullOr lib.types.path;
description = ''
The path to the password for the specified ntfy-sh user.
Setting this option is required when using a ntfy-sh instance with access control enabled.
'';
default = null;
example = "/run/secrets/grafana-to-ntfy-ntfy-pass";
};
bauthUser = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = ''
The user for Basic Auth on incoming webhook requests from Grafana or Alertmanager.
When set together with {option}`bauthPass`, incoming requests require Basic Auth.
When both are null, the endpoint is open (unauthenticated).
'';
default = null;
example = "admin";
};
bauthPass = lib.mkOption {
type = lib.types.nullOr lib.types.path;
description = ''
Path to the password file for Basic Auth on incoming webhook requests.
When set together with {option}`bauthUser`, incoming requests require Basic Auth.
When both are null, the endpoint is open (unauthenticated).
'';
default = null;
example = "/run/secrets/grafana-to-ntfy-bauth-pass";
};
markdown = lib.mkOption {
type = lib.types.bool;
description = "Enable Markdown formatting in ntfy notifications. Sets the X-Markdown header.";
default = false;
};
port = lib.mkOption {
type = lib.types.port;
description = "Port to listen on.";
default = 8080;
};
address = lib.mkOption {
type = lib.types.str;
description = "Address to listen on.";
default = "127.0.0.1";
example = "0.0.0.0";
};
};
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = (cfg.settings.bauthUser == null) == (cfg.settings.bauthPass == null);
message = "services.grafana-to-ntfy: bauthUser and bauthPass must both be set or both be null";
}
{
assertion = (cfg.settings.ntfyBAuthUser == null) == (cfg.settings.ntfyBAuthPass == null);
message = "services.grafana-to-ntfy: ntfyBAuthUser and ntfyBAuthPass must both be set or both be null";
}
];
systemd.services.grafana-to-ntfy = {
description = "Grafana/Alertmanager to ntfy.sh bridge";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
script =
let
optionalCred = name: envVar: ''
export ${envVar}="$(${lib.getExe' config.systemd.package "systemd-creds"} cat ${name})"
'';
in
''
${lib.optionalString (cfg.settings.bauthPass != null) (optionalCred "BAUTH_PASS_FILE" "BAUTH_PASS")}
${lib.optionalString (cfg.settings.ntfyBAuthPass != null) (
optionalCred "NTFY_BAUTH_PASS_FILE" "NTFY_BAUTH_PASS"
)}
exec ${lib.getExe cfg.package}
'';
environment = {
NTFY_URL = cfg.settings.ntfyUrl;
ROCKET_PORT = toString cfg.settings.port;
ROCKET_ADDRESS = cfg.settings.address;
}
// lib.optionalAttrs (cfg.settings.bauthUser != null) {
BAUTH_USER = cfg.settings.bauthUser;
}
// lib.optionalAttrs (cfg.settings.ntfyBAuthUser != null) {
NTFY_BAUTH_USER = cfg.settings.ntfyBAuthUser;
}
// lib.optionalAttrs cfg.settings.markdown {
MARKDOWN = "true";
};
serviceConfig = {
LoadCredential =
lib.optional (cfg.settings.bauthPass != null) "BAUTH_PASS_FILE:${cfg.settings.bauthPass}"
++ lib.optional (
cfg.settings.ntfyBAuthPass != null
) "NTFY_BAUTH_PASS_FILE:${cfg.settings.ntfyBAuthPass}";
DynamicUser = true;
Restart = "always";
RestartSec = 5;
# Hardening
AmbientCapabilities = [ "" ];
CapabilityBoundingSet = [ "" ];
DevicePolicy = "closed";
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";
RemoveIPC = true;
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_UNIX"
];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
SystemCallFilter = [
"@system-service"
"~@privileged"
"~@resources"
];
UMask = "0077";
};
};
};
}
|