about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/misc/mqtt2influxdb.nix
blob: 621f51a4e7fdab39660f90e9162ca443176198a7 (plain) (blame)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
{
  config,
  lib,
  pkgs,
  ...
}:

with lib;

let
  cfg = config.services.mqtt2influxdb;
  filterNull = filterAttrsRecursive (n: v: v != null);
  configFile = (pkgs.formats.yaml {}).generate "mqtt2influxdb.config.yaml" (
    filterNull {
      inherit (cfg) mqtt influxdb;
      points = map filterNull cfg.points;
    }
  );

  pointType = types.submodule {
    options = {
      measurement = mkOption {
        type = types.str;
        description = mdDoc "Name of the measurement";
      };
      topic = mkOption {
        type = types.str;
        description = mdDoc "MQTT topic to subscribe to.";
      };
      fields = mkOption {
        type = types.submodule {
          options = {
            value = mkOption {
              type = types.str;
              default = "$.payload";
              description = mdDoc "Value to be picked up";
            };
            type = mkOption {
              type = with types; nullOr str;
              default = null;
              description = mdDoc "Type to be picked up";
            };
          };
        };
        description = mdDoc "Field selector.";
      };
      tags = mkOption {
        type = with types; attrsOf str;
        default = {};
        description = mdDoc "Tags applied";
      };
    };
  };

  defaultPoints = [
    {
      measurement = "temperature";
      topic = "node/+/thermometer/+/temperature";
      fields.value = "$.payload";
      tags = {
        id = "$.topic[1]";
        channel = "$.topic[3]";
      };
    }
    {
      measurement = "relative-humidity";
      topic = "node/+/hygrometer/+/relative-humidity";
      fields.value = "$.payload";
      tags = {
        id = "$.topic[1]";
        channel = "$.topic[3]";
      };
    }
    {
      measurement = "illuminance";
      topic = "node/+/lux-meter/0:0/illuminance";
      fields.value = "$.payload";
      tags = {
        id = "$.topic[1]";
      };
    }
    {
      measurement = "pressure";
      topic = "node/+/barometer/0:0/pressure";
      fields.value = "$.payload";
      tags = {
        id = "$.topic[1]";
      };
    }
    {
      measurement = "co2";
      topic = "node/+/co2-meter/-/concentration";
      fields.value = "$.payload";
      tags = {
        id = "$.topic[1]";
      };
    }
    {
      measurement = "voltage";
      topic = "node/+/battery/+/voltage";
      fields.value = "$.payload";
      tags = {
        id = "$.topic[1]";
      };
    }
    {
      measurement = "button";
      topic = "node/+/push-button/+/event-count";
      fields.value = "$.payload";
      tags = {
        id = "$.topic[1]";
        channel = "$.topic[3]";
      };
    }
    {
      measurement = "tvoc";
      topic = "node/+/voc-lp-sensor/0:0/tvoc";
      fields.value = "$.payload";
      tags = {
        id = "$.topic[1]";
      };
    }
  ];
in {
  options = {
    services.mqtt2influxdb = {
      enable = mkEnableOption (mdDoc "BigClown MQTT to InfluxDB bridge.");
      environmentFiles = mkOption {
        type = types.listOf types.path;
        default = [];
        example = [ "/run/keys/mqtt2influxdb.env" ];
        description = mdDoc ''
          File to load as environment file. Environment variables from this file
          will be interpolated into the config file using envsubst with this
          syntax: `$ENVIRONMENT` or `''${VARIABLE}`.
          This is useful to avoid putting secrets into the nix store.
        '';
      };
      mqtt = {
        host = mkOption {
          type = types.str;
          default = "127.0.0.1";
          description = mdDoc "Host where MQTT server is running.";
        };
        port = mkOption {
          type = types.port;
          default = 1883;
          description = mdDoc "MQTT server port.";
        };
        username = mkOption {
          type = with types; nullOr str;
          default = null;
          description = mdDoc "Username used to connect to the MQTT server.";
        };
        password = mkOption {
          type = with types; nullOr str;
          default = null;
          description = mdDoc ''
            MQTT password.

            It is highly suggested to use here replacement through
            environmentFiles as otherwise the password is put world readable to
            the store.
          '';
        };
        cafile = mkOption {
          type = with types; nullOr path;
          default = null;
          description = mdDoc "Certification Authority file for MQTT";
        };
        certfile = mkOption {
          type = with types; nullOr path;
          default = null;
          description = mdDoc "Certificate file for MQTT";
        };
        keyfile = mkOption {
          type = with types; nullOr path;
          default = null;
          description = mdDoc "Key file for MQTT";
        };
      };
      influxdb = {
        host = mkOption {
          type = types.str;
          default = "127.0.0.1";
          description = mdDoc "Host where InfluxDB server is running.";
        };
        port = mkOption {
          type = types.port;
          default = 8086;
          description = mdDoc "InfluxDB server port";
        };
        database = mkOption {
          type = types.str;
          description = mdDoc "Name of the InfluxDB database.";
        };
        username = mkOption {
          type = with types; nullOr str;
          default = null;
          description = mdDoc "Username for InfluxDB login.";
        };
        password = mkOption {
          type = with types; nullOr str;
          default = null;
          description = mdDoc ''
            Password for InfluxDB login.

            It is highly suggested to use here replacement through
            environmentFiles as otherwise the password is put world readable to
            the store.
            '';
        };
        ssl = mkOption {
          type = types.bool;
          default = false;
          description = mdDoc "Use SSL to connect to the InfluxDB server.";
        };
        verify_ssl = mkOption {
          type = types.bool;
          default = true;
          description = mdDoc "Verify SSL certificate when connecting to the InfluxDB server.";
        };
      };
      points = mkOption {
        type = types.listOf pointType;
        default = defaultPoints;
        description = mdDoc "Points to bridge from MQTT to InfluxDB.";
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.bigclown-mqtt2influxdb = let
      envConfig = cfg.environmentFiles != [];
      finalConfig = if envConfig
        then "$RUNTIME_DIRECTORY/mqtt2influxdb.config.yaml"
        else configFile;
    in {
      description = "BigClown MQTT to InfluxDB bridge";
      wantedBy = ["multi-user.target"];
      wants = mkIf config.services.mosquitto.enable ["mosquitto.service"];
      preStart = ''
        umask 077
        ${pkgs.envsubst}/bin/envsubst -i "${configFile}" -o "${finalConfig}"
      '';
      serviceConfig = {
        EnvironmentFile = cfg.environmentFiles;
        ExecStart = "${cfg.package}/bin/mqtt2influxdb -dc ${finalConfig}";
        RuntimeDirectory = "mqtt2influxdb";
      };
    };
  };
}