2019-10-27 17:28:53 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.zigbee2mqtt;
|
|
|
|
|
2021-04-30 00:41:58 +01:00
|
|
|
format = pkgs.formats.yaml { };
|
|
|
|
configFile = format.generate "zigbee2mqtt.yaml" cfg.settings;
|
2021-04-30 19:05:57 +01:00
|
|
|
|
2019-10-27 17:28:53 +00:00
|
|
|
in
|
|
|
|
{
|
2021-04-30 19:40:04 +01:00
|
|
|
meta.maintainers = with maintainers; [ sweber hexa ];
|
2019-10-27 17:28:53 +00:00
|
|
|
|
2021-04-30 19:05:57 +01:00
|
|
|
imports = [
|
|
|
|
# Remove warning before the 21.11 release
|
|
|
|
(mkRenamedOptionModule [ "services" "zigbee2mqtt" "config" ] [ "services" "zigbee2mqtt" "settings" ])
|
|
|
|
];
|
|
|
|
|
2019-10-27 17:28:53 +00:00
|
|
|
options.services.zigbee2mqtt = {
|
|
|
|
enable = mkEnableOption "enable zigbee2mqtt service";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
description = "Zigbee2mqtt package to use";
|
|
|
|
default = pkgs.zigbee2mqtt.override {
|
|
|
|
dataDir = cfg.dataDir;
|
|
|
|
};
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression ''
|
2021-04-30 00:41:58 +01:00
|
|
|
pkgs.zigbee2mqtt {
|
|
|
|
dataDir = services.zigbee2mqtt.dataDir
|
|
|
|
}
|
|
|
|
'';
|
2019-10-27 17:28:53 +00:00
|
|
|
type = types.package;
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
description = "Zigbee2mqtt data directory";
|
|
|
|
default = "/var/lib/zigbee2mqtt";
|
|
|
|
type = types.path;
|
|
|
|
};
|
|
|
|
|
2021-04-30 00:41:58 +01:00
|
|
|
settings = mkOption {
|
|
|
|
type = format.type;
|
2019-10-27 17:28:53 +00:00
|
|
|
default = {};
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''
|
2019-10-27 17:28:53 +00:00
|
|
|
{
|
|
|
|
homeassistant = config.services.home-assistant.enable;
|
|
|
|
permit_join = true;
|
|
|
|
serial = {
|
|
|
|
port = "/dev/ttyACM1";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Your <filename>configuration.yaml</filename> as a Nix attribute set.
|
2021-04-30 00:41:58 +01:00
|
|
|
Check the <link xlink:href="https://www.zigbee2mqtt.io/information/configuration.html">documentation</link>
|
|
|
|
for possible options.
|
2019-10-27 17:28:53 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf (cfg.enable) {
|
2021-04-30 00:41:58 +01:00
|
|
|
|
|
|
|
# preset config values
|
|
|
|
services.zigbee2mqtt.settings = {
|
|
|
|
homeassistant = mkDefault config.services.home-assistant.enable;
|
|
|
|
permit_join = mkDefault false;
|
|
|
|
mqtt = {
|
|
|
|
base_topic = mkDefault "zigbee2mqtt";
|
|
|
|
server = mkDefault "mqtt://localhost:1883";
|
|
|
|
};
|
|
|
|
serial.port = mkDefault "/dev/ttyACM0";
|
|
|
|
# reference device configuration, that is kept in a separate file
|
|
|
|
# to prevent it being overwritten in the units ExecStartPre script
|
|
|
|
devices = mkDefault "devices.yaml";
|
|
|
|
};
|
|
|
|
|
2019-10-27 17:28:53 +00:00
|
|
|
systemd.services.zigbee2mqtt = {
|
|
|
|
description = "Zigbee2mqtt Service";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2020-12-05 13:26:30 +00:00
|
|
|
environment.ZIGBEE2MQTT_DATA = cfg.dataDir;
|
2019-10-27 17:28:53 +00:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${cfg.package}/bin/zigbee2mqtt";
|
|
|
|
User = "zigbee2mqtt";
|
|
|
|
WorkingDirectory = cfg.dataDir;
|
|
|
|
Restart = "on-failure";
|
2021-04-24 14:40:12 +01:00
|
|
|
|
|
|
|
# Hardening
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
DeviceAllow = [
|
|
|
|
config.services.zigbee2mqtt.settings.serial.port
|
|
|
|
];
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = false;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = false; # prevents access to /dev/serial, because it is set 0700 root:root
|
|
|
|
PrivateUsers = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProcSubset = "pid";
|
2019-10-27 17:28:53 +00:00
|
|
|
ProtectSystem = "strict";
|
|
|
|
ReadWritePaths = cfg.dataDir;
|
|
|
|
RemoveIPC = true;
|
2021-04-24 14:40:12 +01:00
|
|
|
RestrictAddressFamilies = [
|
|
|
|
"AF_INET"
|
|
|
|
"AF_INET6"
|
|
|
|
];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SupplementaryGroups = [
|
|
|
|
"dialout"
|
|
|
|
];
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = [
|
|
|
|
"@system-service"
|
|
|
|
"~@privileged"
|
|
|
|
"~@resources"
|
|
|
|
];
|
|
|
|
UMask = "0077";
|
2019-10-27 17:28:53 +00:00
|
|
|
};
|
|
|
|
preStart = ''
|
|
|
|
cp --no-preserve=mode ${configFile} "${cfg.dataDir}/configuration.yaml"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.zigbee2mqtt = {
|
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
group = "zigbee2mqtt";
|
|
|
|
uid = config.ids.uids.zigbee2mqtt;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups.zigbee2mqtt.gid = config.ids.gids.zigbee2mqtt;
|
|
|
|
};
|
|
|
|
}
|