tangled
alpha
login
or
join now
pyrox.dev
/
nixpkgs
0
fork
atom
lol
0
fork
atom
overview
issues
pulls
pipelines
nixos/mqtt2influxdb: init module
Karel Kočí
2 years ago
fd3f5471
dd4eec64
+254
2 changed files
expand all
collapse all
unified
split
nixos
modules
module-list.nix
services
misc
mqtt2influxdb.nix
+1
nixos/modules/module-list.nix
reviewed
···
666
666
./services/misc/mediatomb.nix
667
667
./services/misc/metabase.nix
668
668
./services/misc/moonraker.nix
669
669
+
./services/misc/mqtt2influxdb.nix
669
670
./services/misc/n8n.nix
670
671
./services/misc/nitter.nix
671
672
./services/misc/nix-gc.nix
+253
nixos/modules/services/misc/mqtt2influxdb.nix
reviewed
···
1
1
+
{
2
2
+
config,
3
3
+
lib,
4
4
+
pkgs,
5
5
+
...
6
6
+
}:
7
7
+
8
8
+
with lib;
9
9
+
10
10
+
let
11
11
+
cfg = config.services.mqtt2influxdb;
12
12
+
filterNull = filterAttrsRecursive (n: v: v != null);
13
13
+
configFile = (pkgs.formats.yaml {}).generate "mqtt2influxdb.config.yaml" (
14
14
+
filterNull {
15
15
+
inherit (cfg) mqtt influxdb;
16
16
+
points = map filterNull cfg.points;
17
17
+
}
18
18
+
);
19
19
+
20
20
+
pointType = types.submodule {
21
21
+
options = {
22
22
+
measurement = mkOption {
23
23
+
type = types.str;
24
24
+
description = mdDoc "Name of the measurement";
25
25
+
};
26
26
+
topic = mkOption {
27
27
+
type = types.str;
28
28
+
description = mdDoc "MQTT topic to subscribe to.";
29
29
+
};
30
30
+
fields = mkOption {
31
31
+
type = types.submodule {
32
32
+
options = {
33
33
+
value = mkOption {
34
34
+
type = types.str;
35
35
+
default = "$.payload";
36
36
+
description = mdDoc "Value to be picked up";
37
37
+
};
38
38
+
type = mkOption {
39
39
+
type = with types; nullOr str;
40
40
+
default = null;
41
41
+
description = mdDoc "Type to be picked up";
42
42
+
};
43
43
+
};
44
44
+
};
45
45
+
description = mdDoc "Field selector.";
46
46
+
};
47
47
+
tags = mkOption {
48
48
+
type = with types; attrsOf str;
49
49
+
default = {};
50
50
+
description = mdDoc "Tags applied";
51
51
+
};
52
52
+
};
53
53
+
};
54
54
+
55
55
+
defaultPoints = [
56
56
+
{
57
57
+
measurement = "temperature";
58
58
+
topic = "node/+/thermometer/+/temperature";
59
59
+
fields.value = "$.payload";
60
60
+
tags = {
61
61
+
id = "$.topic[1]";
62
62
+
channel = "$.topic[3]";
63
63
+
};
64
64
+
}
65
65
+
{
66
66
+
measurement = "relative-humidity";
67
67
+
topic = "node/+/hygrometer/+/relative-humidity";
68
68
+
fields.value = "$.payload";
69
69
+
tags = {
70
70
+
id = "$.topic[1]";
71
71
+
channel = "$.topic[3]";
72
72
+
};
73
73
+
}
74
74
+
{
75
75
+
measurement = "illuminance";
76
76
+
topic = "node/+/lux-meter/0:0/illuminance";
77
77
+
fields.value = "$.payload";
78
78
+
tags = {
79
79
+
id = "$.topic[1]";
80
80
+
};
81
81
+
}
82
82
+
{
83
83
+
measurement = "pressure";
84
84
+
topic = "node/+/barometer/0:0/pressure";
85
85
+
fields.value = "$.payload";
86
86
+
tags = {
87
87
+
id = "$.topic[1]";
88
88
+
};
89
89
+
}
90
90
+
{
91
91
+
measurement = "co2";
92
92
+
topic = "node/+/co2-meter/-/concentration";
93
93
+
fields.value = "$.payload";
94
94
+
tags = {
95
95
+
id = "$.topic[1]";
96
96
+
};
97
97
+
}
98
98
+
{
99
99
+
measurement = "voltage";
100
100
+
topic = "node/+/battery/+/voltage";
101
101
+
fields.value = "$.payload";
102
102
+
tags = {
103
103
+
id = "$.topic[1]";
104
104
+
};
105
105
+
}
106
106
+
{
107
107
+
measurement = "button";
108
108
+
topic = "node/+/push-button/+/event-count";
109
109
+
fields.value = "$.payload";
110
110
+
tags = {
111
111
+
id = "$.topic[1]";
112
112
+
channel = "$.topic[3]";
113
113
+
};
114
114
+
}
115
115
+
{
116
116
+
measurement = "tvoc";
117
117
+
topic = "node/+/voc-lp-sensor/0:0/tvoc";
118
118
+
fields.value = "$.payload";
119
119
+
tags = {
120
120
+
id = "$.topic[1]";
121
121
+
};
122
122
+
}
123
123
+
];
124
124
+
in {
125
125
+
options = {
126
126
+
services.mqtt2influxdb = {
127
127
+
enable = mkEnableOption (mdDoc "BigClown MQTT to InfluxDB bridge.");
128
128
+
environmentFiles = mkOption {
129
129
+
type = types.listOf types.path;
130
130
+
default = [];
131
131
+
example = [ "/run/keys/mqtt2influxdb.env" ];
132
132
+
description = mdDoc ''
133
133
+
File to load as environment file. Environment variables from this file
134
134
+
will be interpolated into the config file using envsubst with this
135
135
+
syntax: `$ENVIRONMENT` or `''${VARIABLE}`.
136
136
+
This is useful to avoid putting secrets into the nix store.
137
137
+
'';
138
138
+
};
139
139
+
mqtt = {
140
140
+
host = mkOption {
141
141
+
type = types.str;
142
142
+
default = "127.0.0.1";
143
143
+
description = mdDoc "Host where MQTT server is running.";
144
144
+
};
145
145
+
port = mkOption {
146
146
+
type = types.port;
147
147
+
default = 1883;
148
148
+
description = mdDoc "MQTT server port.";
149
149
+
};
150
150
+
username = mkOption {
151
151
+
type = with types; nullOr str;
152
152
+
default = null;
153
153
+
description = mdDoc "Username used to connect to the MQTT server.";
154
154
+
};
155
155
+
password = mkOption {
156
156
+
type = with types; nullOr str;
157
157
+
default = null;
158
158
+
description = mdDoc ''
159
159
+
MQTT password.
160
160
+
161
161
+
It is highly suggested to use here replacement through
162
162
+
environmentFiles as otherwise the password is put world readable to
163
163
+
the store.
164
164
+
'';
165
165
+
};
166
166
+
cafile = mkOption {
167
167
+
type = with types; nullOr path;
168
168
+
default = null;
169
169
+
description = mdDoc "Certification Authority file for MQTT";
170
170
+
};
171
171
+
certfile = mkOption {
172
172
+
type = with types; nullOr path;
173
173
+
default = null;
174
174
+
description = mdDoc "Certificate file for MQTT";
175
175
+
};
176
176
+
keyfile = mkOption {
177
177
+
type = with types; nullOr path;
178
178
+
default = null;
179
179
+
description = mdDoc "Key file for MQTT";
180
180
+
};
181
181
+
};
182
182
+
influxdb = {
183
183
+
host = mkOption {
184
184
+
type = types.str;
185
185
+
default = "127.0.0.1";
186
186
+
description = mdDoc "Host where InfluxDB server is running.";
187
187
+
};
188
188
+
port = mkOption {
189
189
+
type = types.port;
190
190
+
default = 8086;
191
191
+
description = mdDoc "InfluxDB server port";
192
192
+
};
193
193
+
database = mkOption {
194
194
+
type = types.str;
195
195
+
description = mdDoc "Name of the InfluxDB database.";
196
196
+
};
197
197
+
username = mkOption {
198
198
+
type = with types; nullOr str;
199
199
+
default = null;
200
200
+
description = mdDoc "Username for InfluxDB login.";
201
201
+
};
202
202
+
password = mkOption {
203
203
+
type = with types; nullOr str;
204
204
+
default = null;
205
205
+
description = mdDoc ''
206
206
+
Password for InfluxDB login.
207
207
+
208
208
+
It is highly suggested to use here replacement through
209
209
+
environmentFiles as otherwise the password is put world readable to
210
210
+
the store.
211
211
+
'';
212
212
+
};
213
213
+
ssl = mkOption {
214
214
+
type = types.bool;
215
215
+
default = false;
216
216
+
description = mdDoc "Use SSL to connect to the InfluxDB server.";
217
217
+
};
218
218
+
verify_ssl = mkOption {
219
219
+
type = types.bool;
220
220
+
default = true;
221
221
+
description = mdDoc "Verify SSL certificate when connecting to the InfluxDB server.";
222
222
+
};
223
223
+
};
224
224
+
points = mkOption {
225
225
+
type = types.listOf pointType;
226
226
+
default = defaultPoints;
227
227
+
description = mdDoc "Points to bridge from MQTT to InfluxDB.";
228
228
+
};
229
229
+
};
230
230
+
};
231
231
+
232
232
+
config = mkIf cfg.enable {
233
233
+
systemd.services.bigclown-mqtt2influxdb = let
234
234
+
envConfig = cfg.environmentFiles != [];
235
235
+
finalConfig = if envConfig
236
236
+
then "$RUNTIME_DIRECTORY/mqtt2influxdb.config.yaml"
237
237
+
else configFile;
238
238
+
in {
239
239
+
description = "BigClown MQTT to InfluxDB bridge";
240
240
+
wantedBy = ["multi-user.target"];
241
241
+
wants = mkIf config.services.mosquitto.enable ["mosquitto.service"];
242
242
+
preStart = ''
243
243
+
umask 077
244
244
+
${pkgs.envsubst}/bin/envsubst -i "${configFile}" -o "${finalConfig}"
245
245
+
'';
246
246
+
serviceConfig = {
247
247
+
EnvironmentFile = cfg.environmentFiles;
248
248
+
ExecStart = "${cfg.package}/bin/mqtt2influxdb -dc ${finalConfig}";
249
249
+
RuntimeDirectory = "mqtt2influxdb";
250
250
+
};
251
251
+
};
252
252
+
};
253
253
+
}