1{
2 config,
3 lib,
4 pkgs,
5 options,
6 ...
7}:
8
9let
10 cfg = config.services.prometheus.exporters.nut;
11 inherit (lib)
12 mkOption
13 types
14 optionalString
15 concatStringsSep
16 ;
17in
18{
19 port = 9199;
20 extraOpts = {
21 nutServer = mkOption {
22 type = types.str;
23 default = "127.0.0.1";
24 description = ''
25 Hostname or address of the NUT server
26 '';
27 };
28 nutUser = mkOption {
29 type = types.str;
30 default = "";
31 example = "nut";
32 description = ''
33 The user to log in into NUT server. If set, passwordPath should
34 also be set.
35
36 Default NUT configs usually permit reading variables without
37 authentication.
38 '';
39 };
40 passwordPath = mkOption {
41 type = types.nullOr types.path;
42 default = null;
43 apply = final: if final == null then null else toString final;
44 description = ''
45 A run-time path to the nutUser password file, which should be
46 provisioned outside of Nix store.
47 '';
48 };
49 nutVariables = mkOption {
50 type = types.listOf types.str;
51 default = [ ];
52 description = ''
53 List of NUT variable names to monitor.
54
55 If no variables are set, all numeric variables will be exported automatically.
56 See the [upstream docs](https://github.com/DRuggeri/nut_exporter?tab=readme-ov-file#variables-and-information)
57 for more information.
58 '';
59 };
60 };
61 serviceOpts = {
62 script = ''
63 ${optionalString (
64 cfg.passwordPath != null
65 ) "export NUT_EXPORTER_PASSWORD=$(cat ${toString cfg.passwordPath})"}
66 ${pkgs.prometheus-nut-exporter}/bin/nut_exporter \
67 --nut.server=${cfg.nutServer} \
68 --web.listen-address="${cfg.listenAddress}:${toString cfg.port}" \
69 ${optionalString (cfg.nutUser != "") "--nut.username=${cfg.nutUser}"} \
70 ${
71 optionalString (
72 cfg.nutVariables != [ ]
73 ) "--nut.vars_enable=${concatStringsSep "," cfg.nutVariables}"
74 } \
75 ${concatStringsSep " " cfg.extraFlags}
76 '';
77 };
78}