8 cfg = config.services.mqtt2influxdb;
9 filterNull = lib.filterAttrsRecursive (n: v: v != null);
10 configFile = (pkgs.formats.yaml {}).generate "mqtt2influxdb.config.yaml" (
12 inherit (cfg) mqtt influxdb;
13 points = map filterNull cfg.points;
17 pointType = lib.types.submodule {
19 measurement = lib.mkOption {
21 description = "Name of the measurement";
23 topic = lib.mkOption {
25 description = "MQTT topic to subscribe to.";
27 fields = lib.mkOption {
28 type = lib.types.submodule {
30 value = lib.mkOption {
32 default = "$.payload";
33 description = "Value to be picked up";
36 type = with lib.types; nullOr str;
38 description = "Type to be picked up";
42 description = "Field selector.";
45 type = with lib.types; attrsOf str;
47 description = "Tags applied";
54 measurement = "temperature";
55 topic = "node/+/thermometer/+/temperature";
56 fields.value = "$.payload";
59 channel = "$.topic[3]";
63 measurement = "relative-humidity";
64 topic = "node/+/hygrometer/+/relative-humidity";
65 fields.value = "$.payload";
68 channel = "$.topic[3]";
72 measurement = "illuminance";
73 topic = "node/+/lux-meter/0:0/illuminance";
74 fields.value = "$.payload";
80 measurement = "pressure";
81 topic = "node/+/barometer/0:0/pressure";
82 fields.value = "$.payload";
89 topic = "node/+/co2-meter/-/concentration";
90 fields.value = "$.payload";
96 measurement = "voltage";
97 topic = "node/+/battery/+/voltage";
98 fields.value = "$.payload";
104 measurement = "button";
105 topic = "node/+/push-button/+/event-count";
106 fields.value = "$.payload";
109 channel = "$.topic[3]";
113 measurement = "tvoc";
114 topic = "node/+/voc-lp-sensor/0:0/tvoc";
115 fields.value = "$.payload";
123 services.mqtt2influxdb = {
124 enable = lib.mkEnableOption "BigClown MQTT to InfluxDB bridge";
125 package = lib.mkPackageOption pkgs ["python3Packages" "mqtt2influxdb"] {};
126 environmentFiles = lib.mkOption {
127 type = lib.types.listOf lib.types.path;
129 example = [ "/run/keys/mqtt2influxdb.env" ];
131 File to load as environment file. Environment variables from this file
132 will be interpolated into the config file using envsubst with this
133 syntax: `$ENVIRONMENT` or `''${VARIABLE}`.
134 This is useful to avoid putting secrets into the nix store.
138 host = lib.mkOption {
139 type = lib.types.str;
140 default = "127.0.0.1";
141 description = "Host where MQTT server is running.";
143 port = lib.mkOption {
144 type = lib.types.port;
146 description = "MQTT server port.";
148 username = lib.mkOption {
149 type = with lib.types; nullOr str;
151 description = "Username used to connect to the MQTT server.";
153 password = lib.mkOption {
154 type = with lib.types; nullOr str;
159 It is highly suggested to use here replacement through
160 environmentFiles as otherwise the password is put world readable to
164 cafile = lib.mkOption {
165 type = with lib.types; nullOr path;
167 description = "Certification Authority file for MQTT";
169 certfile = lib.mkOption {
170 type = with lib.types; nullOr path;
172 description = "Certificate file for MQTT";
174 keyfile = lib.mkOption {
175 type = with lib.types; nullOr path;
177 description = "Key file for MQTT";
181 host = lib.mkOption {
182 type = lib.types.str;
183 default = "127.0.0.1";
184 description = "Host where InfluxDB server is running.";
186 port = lib.mkOption {
187 type = lib.types.port;
189 description = "InfluxDB server port";
191 database = lib.mkOption {
192 type = lib.types.str;
193 description = "Name of the InfluxDB database.";
195 username = lib.mkOption {
196 type = with lib.types; nullOr str;
198 description = "Username for InfluxDB login.";
200 password = lib.mkOption {
201 type = with lib.types; nullOr str;
204 Password for InfluxDB login.
206 It is highly suggested to use here replacement through
207 environmentFiles as otherwise the password is put world readable to
212 type = lib.types.bool;
214 description = "Use SSL to connect to the InfluxDB server.";
216 verify_ssl = lib.mkOption {
217 type = lib.types.bool;
219 description = "Verify SSL certificate when connecting to the InfluxDB server.";
222 points = lib.mkOption {
223 type = lib.types.listOf pointType;
224 default = defaultPoints;
225 description = "Points to bridge from MQTT to InfluxDB.";
230 config = lib.mkIf cfg.enable {
231 systemd.services.bigclown-mqtt2influxdb = let
232 envConfig = cfg.environmentFiles != [];
233 finalConfig = if envConfig
234 then "$RUNTIME_DIRECTORY/mqtt2influxdb.config.yaml"
237 description = "BigClown MQTT to InfluxDB bridge";
238 wantedBy = ["multi-user.target"];
239 wants = lib.mkIf config.services.mosquitto.enable ["mosquitto.service"];
242 ${pkgs.envsubst}/bin/envsubst -i "${configFile}" -o "${finalConfig}"
245 EnvironmentFile = cfg.environmentFiles;
246 ExecStart = "${lib.getExe cfg.package} -dc ${finalConfig}";
247 RuntimeDirectory = "mqtt2influxdb";