nix-config/hosts/broker/default.nix

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

153 lines
3.6 KiB
Nix
Raw Normal View History

2022-12-04 08:53:28 +01:00
{ config, pkgs, ... }:
2022-07-16 02:03:47 +02:00
let
mymqttui = pkgs.writeScriptBin "mqttui" ''
export MQTTUI_USERNAME=consumer
export MQTTUI_PASSWORD=`cat ${(builtins.head config.services.mosquitto.listeners).users.consumer.passwordFile}`
exec ${pkgs.mqttui}/bin/mqttui
'';
2022-07-17 01:48:16 +02:00
fqdn = "broker.serv.zentralwerk.org";
2022-07-19 18:25:28 +02:00
mqttWebsocketPort = 9001;
in
2022-07-16 02:03:47 +02:00
{
c3d2.deployment.server = "server10";
2022-07-16 02:03:47 +02:00
microvm.mem = 1024;
networking = {
hostName = "broker";
2022-07-17 01:48:16 +02:00
firewall.allowedTCPPorts = [
# nginx
80 443
# mosquitto
1883 8883
];
2022-07-16 02:03:47 +02:00
};
services.openssh.enable = true;
2022-07-17 01:48:16 +02:00
# runs mainly to obtain a TLS certificate
services.nginx = {
enable = true;
virtualHosts.${fqdn} = {
default = true;
enableACME = true;
forceSSL = true;
2022-07-19 18:25:28 +02:00
locations."/mqtt" = {
proxyPass = "http://localhost:${toString mqttWebsocketPort}/";
proxyWebsockets = true;
};
2022-07-17 01:48:16 +02:00
};
};
2022-07-16 02:03:47 +02:00
services.mosquitto = {
enable = true;
2022-07-17 01:48:16 +02:00
listeners =
let
users = {
"zentralwerk-network" = {
passwordFile = config.sops.secrets."mosquitto/users/zentralwerk-network".path;
acl = [
"write #"
];
};
"services" = {
passwordFile = config.sops.secrets."mosquitto/users/services".path;
acl = [
"write #"
];
};
"consumer" = {
passwordFile = config.sops.secrets."mosquitto/users/consumer".path;
acl = [
"read #"
];
};
2022-09-05 23:26:08 +02:00
"sensors" = {
passwordFile = config.sops.secrets."mosquitto/users/sensors".path;
acl = [
"write esp-sdk/#"
"write esp-proc/#"
];
};
2022-07-16 02:03:47 +02:00
};
2022-07-19 18:25:28 +02:00
in [ {
address = "0.0.0.0";
port = 1883;
inherit users;
} {
2022-07-17 01:48:16 +02:00
address = "::";
port = 1883;
inherit users;
} {
address = "0.0.0.0";
port = 8883;
settings = {
certfile = "/run/credentials/mosquitto.service/cert.pem";
keyfile = "/run/credentials/mosquitto.service/key.pem";
};
inherit users;
2022-07-17 01:48:16 +02:00
} {
address = "::";
port = 8883;
settings = {
certfile = "/run/credentials/mosquitto.service/cert.pem";
keyfile = "/run/credentials/mosquitto.service/key.pem";
2022-07-16 02:03:47 +02:00
};
2022-07-17 01:48:16 +02:00
inherit users;
2022-07-19 18:25:28 +02:00
} {
settings.protocol = "websockets";
address = "::";
port = mqttWebsocketPort;
inherit users;
2022-07-17 01:48:16 +02:00
} ];
2022-07-16 02:03:47 +02:00
};
2022-07-17 01:48:16 +02:00
systemd.services.mosquitto = {
requires = [ "acme-finished-${fqdn}.target" ];
serviceConfig.LoadCredential =
let
certDir = config.security.acme.certs.${fqdn}.directory;
in [
"cert.pem:${certDir}/fullchain.pem"
"key.pem:${certDir}/key.pem"
];
};
security.acme.certs.${fqdn}.postRun = ''
systemctl restart mosquitto
'';
2022-07-16 02:03:47 +02:00
sops = {
age.sshKeyPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];
2022-07-31 18:13:03 +02:00
defaultSopsFile = ./secrets.yaml;
2022-07-16 02:03:47 +02:00
secrets = let
perms = {
owner = config.systemd.services.mosquitto.serviceConfig.User;
group = config.systemd.services.mosquitto.serviceConfig.Group;
mode = "0440";
};
in
{
"mosquitto/users/zentralwerk-network" = perms;
"mosquitto/users/services" = perms;
"mosquitto/users/consumer" = perms;
2022-09-05 23:26:08 +02:00
"mosquitto/users/sensors" = perms;
2022-07-16 02:03:47 +02:00
};
};
environment.systemPackages = with pkgs; [
mymqttui
2022-07-16 02:03:47 +02:00
];
users.motd = ''
C3D2 MQTT Broker
================
Use `mqttui` to inspect the data in mosquitto.
'';
2022-07-16 02:03:47 +02:00
system.stateVersion = "22.05";
}