From 08f8d721fa2d51595cdfafff26b6e9a663fb57a0 Mon Sep 17 00:00:00 2001 From: niten Date: Mon, 24 Apr 2023 09:49:57 -0700 Subject: [PATCH] Add module.nix --- flake.nix | 2 +- module.nix | 71 ++++++++++++++++++++++++++++++++++++++++++++ src/snooper/cli.clj | 17 ++++++++--- src/snooper/core.clj | 21 ++++++++----- 4 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 module.nix diff --git a/flake.nix b/flake.nix index 9f9d591..9bc1d90 100644 --- a/flake.nix +++ b/flake.nix @@ -17,7 +17,7 @@ packages = rec { default = snooper-server; snooper-server = helpers.packages."${system}".mkClojureBin { - name = "org.fudo/snooper"; + name = "org.fudo/snooper-server"; primaryNamespace = "snooper.cli"; src = ./.; }; diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..190bcdd --- /dev/null +++ b/module.nix @@ -0,0 +1,71 @@ +packages: + +{ config, lib, pkgs, ... }: + +with lib; +let + snooper-server = packages."${pkgs.system}.snooper-server"; + cfg = config.services.snooper; + +in { + options.services.snooper = with types; { + enable = mkEnableOption "Enable Snooper notifiaction server."; + + 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."; + }; + + mqtt-client = { + 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; + }; + + 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."; + }; + }; + }; + + config = mkIf cfg.enable { + systemd.services.snooper = { + path = [ snooper-server ]; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + DynamicUser = true; + LoadCredential = [ "mqtt.passwd:${cfg.mqtt-client.password-file}" ]; + ExecStart = pkgs.writeShellScript "snooper-server.sh" + (concatStringsSep " " ([ + "snooper-server" + "--mqtt-host=${cfg.mqtt.host}" + "--mqtt-port=${toString cfg.mqtt.port}" + "--mqtt-user=${cfg.mqtt.username}" + "--mqtt-password-file=$CREDENTIALS_DIRECTORY/mqtt.passwd" + "--notification-topic=${cfg.notification-topic}" + ] ++ (map (topic: "--event-topic=${topic}") cfg.event-topics) + ++ (optional cfg.verbose "--verbose"))); + }; + }; + }; +} diff --git a/src/snooper/cli.clj b/src/snooper/cli.clj index c5fb158..1086323 100644 --- a/src/snooper/cli.clj +++ b/src/snooper/cli.clj @@ -5,10 +5,12 @@ [clojure.string :as str] [snooper.core :as snooper] [milquetoast.client :as mqtt] - [fudo-clojure.logging :as log])) + [fudo-clojure.logging :as log]) + (:gen-class)) (def cli-opts [["-v" "--verbose" "Provide verbose output."] + ["-h" "--help" "Print this message."] [nil "--mqtt-host HOSTNAME" "Hostname of MQTT server."] [nil "--mqtt-port PORT" "Port on which to connect to the MQTT server." @@ -44,13 +46,15 @@ (defn -main [& args] (let [required-args #{:mqtt-host :mqtt-port :mqtt-user :mqtt-password-file :event-topic :notification-topic} {:keys [options _ errors summary]} (parse-opts args required-args cli-opts)] + (when (:help options) (msg-quit 0 (usage summary))) (when (seq errors) (msg-quit 1 (usage summary errors))) (let [{:keys [mqtt-host mqtt-port mqtt-user mqtt-password-file notification-topic - event-topic]} options + event-topic + verbose]} options catch-shutdown (async/chan) mqtt-client (mqtt/connect-json! :host mqtt-host :port mqtt-port @@ -59,13 +63,18 @@ (slurp) (str/trim))) logger (log/print-logger)] - (snooper/listen! :mqtt-client mqtt-client + (when verbose + (println (format "launching snooper server to listen on %s and report events on %s" + event-topic notification-topic))) + (snooper/listen! :mqtt-client mqtt-client :notification-topic notification-topic :event-topics event-topic - :logger logger) + :logger logger + :verbose verbose) (.addShutdownHook (Runtime/getRuntime) (Thread. (fn [] (>!! catch-shutdown true)))) (