Personal Nix setup

Add zigbee2mqtt

Changed files
+99 -7
modules
+1
modules/automation/default.nix
···
} // helpers.linuxAttrs {
imports = [
./mqtt.nix
+
./zigbee.nix
];
}
+32 -7
modules/automation/mqtt.nix
···
enable = mkOption {
default = cfg.enable;
example = true;
-
description = "Whether to enable the mqtt mosquitto broker.";
+
description = "Whether to enable the MQTT Mosquitto broker.";
type = types.bool;
};
+
+
port = mkOption {
+
default = 1883;
+
example = 1883;
+
description = "The port to start Moquitto on.";
+
type = types.port;
+
};
+
+
cafile = mkOption {
+
default = ../base/certs/ca.crt;
+
type = types.path;
+
};
+
+
certfile = mkOption {
+
default = config.age.secrets."mqtt.crt".path;
+
type = types.path;
+
};
+
+
keyfile = mkOption {
+
default = config.age.secrets."mqtt.key".path;
+
type = types.path;
+
};
};
config = mkIf cfg.mqtt.enable {
age.secrets = let
owner = config.users.users.mosquitto.name;
group = config.users.users.mosquitto.group;
+
mode = "0440";
in {
"mqtt.crt" = {
-
inherit owner group;
+
inherit owner group mode;
file = ./certs/mqtt.crt.age;
};
"mqtt.key" = {
-
inherit owner group;
+
inherit owner group mode;
file = ./certs/mqtt.key.age;
};
};
···
enable = true;
listeners = [
{
-
port = 1883;
+
port = cfg.mqtt.port;
+
omitPasswordAuth = true;
settings = {
-
cafile = ../base/certs/ca.crt;
-
certfile = config.age.secrets."mqtt.crt".path;
-
keyfile = config.age.secrets."mqtt.key".path;
+
cafile = cfg.mqtt.cafile;
+
certfile = cfg.mqtt.certfile;
+
keyfile = cfg.mqtt.keyfile;
require_certificate = true;
+
allow_anonymous = true;
};
}
];
+66
modules/automation/zigbee.nix
···
+
{ lib, config, ... }:
+
+
with lib;
+
let
+
cfg = config.modules.automation;
+
+
frontendType = types.submodule {
+
options = {
+
enable = mkOption {
+
default = false;
+
example = true;
+
description = "Whether to enable Zigbee2MQTT's frontend.";
+
type = types.bool;
+
};
+
port = mkOption {
+
default = 8124;
+
example = 8124;
+
description = "The port to use for Zigbee2MQTT's frontend.";
+
type = types.port;
+
};
+
};
+
};
+
in {
+
options.modules.automation.zigbee = {
+
enable = mkOption {
+
default = false;
+
example = true;
+
description = "Whether to enable the Zigbee2MQTT service.";
+
type = types.bool;
+
};
+
+
serialPort = mkOption {
+
default = "/dev/ttyUSB0";
+
example = "/dev/ttyUSB0";
+
description = "The serial port for the USB Zigbee adapter.";
+
type = types.str;
+
};
+
+
frontend = mkOption {
+
default = {};
+
description = "Zigbee2MQTT's frontend options.";
+
type = frontendType;
+
};
+
};
+
+
config = mkIf (cfg.enable && cfg.zigbee.enable) {
+
users.users.zigbee2mqtt.extraGroups = mkIf cfg.mqtt.enable [
+
config.users.users.mosquitto.name
+
];
+
+
services.zigbee2mqtt = {
+
enable = true;
+
settings = {
+
serial.port = cfg.zigbee.serialPort;
+
frontend = cfg.zigbee.frontend;
+
mqtt = mkIf cfg.mqtt.enable {
+
server = "mqtts://localhost:${toString cfg.mqtt.port}";
+
reject_unauthorized = false;
+
ca = cfg.mqtt.cafile;
+
key = cfg.mqtt.keyfile;
+
cert = cfg.mqtt.certfile;
+
};
+
};
+
};
+
};
+
}