nixpkgs/nixos/modules/services/misc/mqtt2influxdb.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

254 lines
6.8 KiB
Nix
Raw Normal View History

2023-04-26 09:19:19 +02:00
{
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 = "Name of the measurement";
2023-04-26 09:19:19 +02:00
};
topic = mkOption {
type = types.str;
description = "MQTT topic to subscribe to.";
2023-04-26 09:19:19 +02:00
};
fields = mkOption {
type = types.submodule {
options = {
value = mkOption {
type = types.str;
default = "$.payload";
description = "Value to be picked up";
2023-04-26 09:19:19 +02:00
};
type = mkOption {
type = with types; nullOr str;
default = null;
description = "Type to be picked up";
2023-04-26 09:19:19 +02:00
};
};
};
description = "Field selector.";
2023-04-26 09:19:19 +02:00
};
tags = mkOption {
type = with types; attrsOf str;
default = {};
description = "Tags applied";
2023-04-26 09:19:19 +02:00
};
};
};
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 "BigClown MQTT to InfluxDB bridge.";
2023-04-26 09:19:19 +02:00
environmentFiles = mkOption {
type = types.listOf types.path;
default = [];
example = [ "/run/keys/mqtt2influxdb.env" ];
description = ''
2023-04-26 09:19:19 +02:00
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 = "Host where MQTT server is running.";
2023-04-26 09:19:19 +02:00
};
port = mkOption {
type = types.port;
default = 1883;
description = "MQTT server port.";
2023-04-26 09:19:19 +02:00
};
username = mkOption {
type = with types; nullOr str;
default = null;
description = "Username used to connect to the MQTT server.";
2023-04-26 09:19:19 +02:00
};
password = mkOption {
type = with types; nullOr str;
default = null;
description = ''
2023-04-26 09:19:19 +02:00
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 = "Certification Authority file for MQTT";
2023-04-26 09:19:19 +02:00
};
certfile = mkOption {
type = with types; nullOr path;
default = null;
description = "Certificate file for MQTT";
2023-04-26 09:19:19 +02:00
};
keyfile = mkOption {
type = with types; nullOr path;
default = null;
description = "Key file for MQTT";
2023-04-26 09:19:19 +02:00
};
};
influxdb = {
host = mkOption {
type = types.str;
default = "127.0.0.1";
description = "Host where InfluxDB server is running.";
2023-04-26 09:19:19 +02:00
};
port = mkOption {
type = types.port;
default = 8086;
description = "InfluxDB server port";
2023-04-26 09:19:19 +02:00
};
database = mkOption {
type = types.str;
description = "Name of the InfluxDB database.";
2023-04-26 09:19:19 +02:00
};
username = mkOption {
type = with types; nullOr str;
default = null;
description = "Username for InfluxDB login.";
2023-04-26 09:19:19 +02:00
};
password = mkOption {
type = with types; nullOr str;
default = null;
description = ''
2023-04-26 09:19:19 +02:00
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 = "Use SSL to connect to the InfluxDB server.";
2023-04-26 09:19:19 +02:00
};
verify_ssl = mkOption {
type = types.bool;
default = true;
description = "Verify SSL certificate when connecting to the InfluxDB server.";
2023-04-26 09:19:19 +02:00
};
};
points = mkOption {
type = types.listOf pointType;
default = defaultPoints;
description = "Points to bridge from MQTT to InfluxDB.";
2023-04-26 09:19:19 +02:00
};
};
};
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";
};
};
};
}