1{
2 config,
3 lib,
4 pkgs,
5 options,
6 ...
7}:
8
9let
10 inherit (lib) mkOption types;
11 cfg = config.services.prometheus.exporters.sabnzbd;
12in
13{
14 port = 9387;
15
16 extraOpts = {
17 servers = mkOption {
18 description = "List of sabnzbd servers to connect to.";
19 type = types.listOf (
20 types.submodule {
21 options = {
22 baseUrl = mkOption {
23 type = types.str;
24 description = "Base URL of the sabnzbd server.";
25 example = "http://localhost:8080/sabnzbd";
26 };
27 apiKeyFile = mkOption {
28 type = types.str;
29 description = ''
30 The path to a file containing the API key.
31 The file is securely passed to the service by leveraging systemd credentials.
32 No special permissions need to be set on this file.
33 '';
34 example = "/run/secrets/sabnzbd_apikey";
35 };
36 };
37 }
38 );
39 };
40 };
41
42 serviceOpts =
43 let
44 servers = lib.zipAttrs cfg.servers;
45 credentials = lib.imap0 (i: v: {
46 name = "apikey-${toString i}";
47 path = v;
48 }) servers.apiKeyFile;
49 in
50 {
51 serviceConfig.LoadCredential = builtins.map ({ name, path }: "${name}:${path}") credentials;
52
53 environment = {
54 METRICS_PORT = toString cfg.port;
55 METRICS_ADDR = cfg.listenAddress;
56 SABNZBD_BASEURLS = lib.concatStringsSep "," servers.baseUrl;
57 };
58
59 script =
60 let
61 apiKeys = lib.concatStringsSep "," (
62 builtins.map (cred: "$(< $CREDENTIALS_DIRECTORY/${cred.name})") credentials
63 );
64 in
65 ''
66 export SABNZBD_APIKEYS="${apiKeys}"
67 exec ${lib.getExe pkgs.prometheus-sabnzbd-exporter}
68 '';
69 };
70}