snooper/module.nix

112 lines
3.3 KiB
Nix
Raw Normal View History

2023-04-24 09:49:57 -07:00
packages:
{ config, lib, pkgs, ... }:
with lib;
let
2023-04-24 10:28:49 -07:00
snooper-server = packages."${pkgs.system}".snooper-server;
2023-04-24 09:49:57 -07:00
cfg = config.services.snooper;
in {
options.services.snooper = with types; {
2023-05-08 22:46:42 -07:00
enable = mkEnableOption "Enable Snooper notification server.";
2023-04-24 09:49:57 -07:00
verbose = mkEnableOption "Generate verbose logs and output.";
event-topics = mkOption {
type = listOf str;
description = "MQTT topics on which to listen for detection events.";
};
notification-topic = mkOption {
type = str;
description = "MQTT topic on which to send notifications.";
};
2023-04-24 10:31:39 -07:00
mqtt = {
2023-05-08 22:46:42 -07:00
incoming = {
host = mkOption {
type = str;
description = "Hostname of the MQTT server.";
};
2023-04-24 09:49:57 -07:00
2023-05-08 22:46:42 -07:00
port = mkOption {
type = port;
description = "Port on which the MQTT server is listening.";
default = 1883;
};
2023-04-24 09:49:57 -07:00
2023-05-08 22:46:42 -07:00
username = mkOption {
type = str;
description = "User as which to connect to the MQTT server.";
};
password-file = mkOption {
type = str;
description =
"File (on the local host) containing the password for the MQTT server.";
};
2023-04-24 09:49:57 -07:00
};
2023-05-08 22:46:42 -07:00
outgoing = {
host = mkOption {
type = str;
description = "Hostname of the MQTT server.";
};
port = mkOption {
type = port;
description = "Port on which the MQTT server is listening.";
default = 1883;
};
2023-04-24 09:49:57 -07:00
2023-05-08 22:46:42 -07:00
username = mkOption {
type = str;
description = "User as which to connect to the MQTT server.";
};
password-file = mkOption {
type = str;
description =
"File (on the local host) containing the password for the MQTT server.";
};
2023-04-24 09:49:57 -07:00
};
};
};
config = mkIf cfg.enable {
systemd.services.snooper = {
path = [ snooper-server ];
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
2023-04-24 09:49:57 -07:00
serviceConfig = {
DynamicUser = true;
Restart = "on-failure";
RestartSec = "120s";
2023-05-08 22:46:42 -07:00
LoadCredential = [
"mqtt-incoming.passwd:${cfg.mqtt.incoming.password-file}"
"mqtt-outgoing.passwd:${cfg.mqtt.outgoing.password-file}"
];
2023-04-24 09:49:57 -07:00
ExecStart = pkgs.writeShellScript "snooper-server.sh"
(concatStringsSep " " ([
"snooper-server"
2023-05-08 22:46:42 -07:00
"--incoming-mqtt-host=${cfg.mqtt.incoming.host}"
"--incoming-mqtt-port=${toString cfg.mqtt.incoming.port}"
"--incoming-mqtt-user=${cfg.mqtt.incoming.username}"
"--incoming-mqtt-password-file=$CREDENTIALS_DIRECTORY/mqtt-incoming.passwd"
"--outgoing-mqtt-host=${cfg.mqtt.outgoing.host}"
"--outgoing-mqtt-port=${toString cfg.mqtt.outgoing.port}"
"--outgoing-mqtt-user=${cfg.mqtt.outgoing.username}"
"--outgoing-mqtt-password-file=$CREDENTIALS_DIRECTORY/mqtt-outgoing.passwd"
2023-04-24 09:49:57 -07:00
"--notification-topic=${cfg.notification-topic}"
] ++ (map (topic: "--event-topic=${topic}") cfg.event-topics)
++ (optional cfg.verbose "--verbose")));
};
unitConfig.ConditionPathExists =
[ cfg.mqtt.incoming.password-file cfg.mqtt.outgoing.password-file ];
2023-04-24 09:49:57 -07:00
};
};
}