···
16
+
cfg = config.services.prometheus.exporters.mqtt;
17
+
toConfigBoolean = x: if x then "True" else "False";
18
+
toConfigList = builtins.concatStringsSep ",";
21
+
# https://github.com/kpetremann/mqtt-exporter/tree/master?tab=readme-ov-file#configuration
24
+
keepFullTopic = mkEnableOption "Keep entire topic instead of the first two elements only. Usecase: Shelly 3EM";
25
+
logLevel = mkOption {
35
+
description = "Logging level";
37
+
logMqttMessage = mkEnableOption "Log MQTT original message, only if `LOG_LEVEL` is set to DEBUG.";
38
+
mqttIgnoredTopics = mkOption {
39
+
type = types.listOf types.str;
41
+
description = "Lists of topics to ignore. Accepts wildcards.";
43
+
mqttAddress = mkOption {
45
+
default = "127.0.0.1";
46
+
description = "IP or hostname of MQTT broker.";
48
+
mqttPort = mkOption {
51
+
description = "TCP port of MQTT broker.";
53
+
mqttTopic = mkOption {
56
+
description = "Topic path to subscribe to.";
58
+
mqttKeepAlive = mkOption {
62
+
description = "Keep alive interval to maintain connection with MQTT broker.";
64
+
mqttUsername = mkOption {
65
+
type = types.nullOr types.str;
67
+
example = "mqttexporter";
68
+
description = "Username which should be used to authenticate against the MQTT broker.";
70
+
mqttV5Protocol = mkEnableOption "Force to use MQTT protocol v5 instead of 3.1.1.";
71
+
mqttClientId = mkOption {
72
+
type = types.nullOr types.str;
74
+
description = "Set client ID manually for MQTT connection";
76
+
mqttExposeClientId = mkEnableOption "Expose the client ID as a label in Prometheus metrics.";
77
+
prometheusPrefix = mkOption {
80
+
description = "Prefix added to the metric name.";
82
+
topicLabel = mkOption {
85
+
description = "Define the Prometheus label for the topic.";
87
+
zigbee2MqttAvailability = mkEnableOption "Normalize sensor name for device availability metric added by Zigbee2MQTT.";
88
+
zwaveTopicPrefix = mkOption {
91
+
description = "MQTT topic used for Zwavejs2Mqtt messages.";
93
+
esphomeTopicPrefixes = mkOption {
94
+
type = types.listOf types.str;
96
+
description = "MQTT topic used for ESPHome messages.";
98
+
hubitatTopicPrefixes = mkOption {
99
+
type = types.listOf types.str;
100
+
default = [ "hubitat/" ];
101
+
description = "MQTT topic used for Hubitat messages.";
103
+
environmentFile = mkOption {
104
+
type = types.nullOr types.path;
106
+
example = [ "/run/secrets/mqtt-exporter" ];
108
+
File to load as environment file. Useful for e.g. setting `MQTT_PASSWORD`
109
+
without putting any secrets into the Nix store.
115
+
KEEP_FULL_TOPIC = toConfigBoolean cfg.keepFullTopic;
116
+
LOG_LEVEL = cfg.logLevel;
117
+
LOG_MQTT_MESSAGE = toConfigBoolean cfg.logMqttMessage;
118
+
MQTT_IGNORED_TOPIC = toConfigList cfg.mqttIgnoredTopics;
119
+
MQTT_ADDRESS = cfg.mqttAddress;
120
+
MQTT_PORT = toString cfg.mqttPort;
121
+
MQTT_TOPIC = cfg.mqttTopic;
122
+
MQTT_KEEPALIVE = toString cfg.mqttKeepAlive;
123
+
MQTT_USERNAME = cfg.mqttUsername;
124
+
MQTT_V5_PROTOCOL = toConfigBoolean cfg.mqttV5Protocol;
125
+
MQTT_CLIENT_ID = mkIf (cfg.mqttClientId != null) cfg.mqttClientId;
126
+
PROMETHEUS_ADDRESS = cfg.listenAddress;
127
+
PROMETHEUS_PORT = toString cfg.port;
128
+
PROMETHEUS_PREFIX = cfg.prometheusPrefix;
129
+
TOPIC_LABEL = cfg.topicLabel;
130
+
ZIGBEE2MQTT_AVAILABILITY = toConfigBoolean cfg.zigbee2MqttAvailability;
131
+
ZWAVE_TOPIC_PREFIX = cfg.zwaveTopicPrefix;
132
+
ESPHOME_TOPIC_PREFIXES = toConfigList cfg.esphomeTopicPrefixes;
133
+
HUBITAT_TOPIC_PREFIXES = toConfigList cfg.hubitatTopicPrefixes;
136
+
EnvironmentFile = mkIf (cfg.environmentFile != null) cfg.environmentFile;
137
+
ExecStart = lib.getExe pkgs.mqtt-exporter;