From e61f18116cc206bc8fa6a27189126c870fb572ec Mon Sep 17 00:00:00 2001 From: niten Date: Fri, 1 Jul 2022 11:40:44 -0700 Subject: [PATCH] Figuring out how to integrate with nixos cfg. --- flake.nix | 8 ++++- module.nix | 80 ++++++++++++++++++++++++++++++++++++++++++++ src/wallfly/core.clj | 41 ++++++++++------------- 3 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 module.nix diff --git a/flake.nix b/flake.nix index 61945aa..1c5e773 100644 --- a/flake.nix +++ b/flake.nix @@ -32,5 +32,11 @@ devShell = pkgs.mkShell { buildInputs = with pkgs; [ clojure update-deps ]; }; - }); + }) // { + overlay = final: prev: { + inherit (self.packages."${prev.system}") wallfly; + }; + + nixosModule = import ./module.nix; + }; } diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..a302953 --- /dev/null +++ b/module.nix @@ -0,0 +1,80 @@ +{ config, lib, pkgs, ... }: + +with lib; +let cfg = config.fudo.wallfly; + +in { + options.fudo.wallfly = with types; { + enable = + mkEnableOption "Enable WallFly presence monitor for users on this host."; + + location = mkOption { + type = str; + description = "Location (in Home Assistant) of this host."; + default = "unknown"; + }; + + mqtt = { + broker-uri = mkOption { + type = str; + description = "URI of the MQTT broker."; + example = "tcp://my-mqtt.host:1883"; + }; + + username = mkOption { + type = str; + description = "Username with which to connect to the MQTT broker."; + default = "wallfly"; + }; + + password-file = mkOption { + type = str; + description = "Path to a file containing the MQTT user password."; + }; + }; + + time-to-idle = mkOption { + type = int; + description = + "Number of seconds before considering the user idle on this host."; + default = "900"; # 15 minutes + }; + + delay-time = mkOption { + type = int; + description = + "Number of seconds to wait before polling for user activity."; + default = 30; + }; + }; + + config = mkIf cfg.enable { + systemd.user.services.wallfly = { + wantedBy = [ "multi-user.target" ]; + after = [ "network-online.target" ]; + serviceConfig = { + ExecStart = pkgs.writeShellScript "launch-wallfly.sh" '' + ${pkgs.wallfly}/bin/wallfly \ + --location=${cfg.location} \ + --mqtt-broker-uri=${cfg.mqtt.broker-uri} \ + --mqtt-username=${cfg.mqtt.username} \ + --mqtt-password-file=${cfg.mqtt.password-file} \ + --time-to-idle=${cfg.time-to-idle} \ + --delay-time=${cfg.delay-time} + ''; + PrivateTmp = true; + PrivateDevices = true; + ProtectSystem = "strict"; + ProtectControlGroups = true; + ProtectKernelTunables = true; + ProtectKernelModules = true; + ProtectHostname = true; + ProtectHome = true; + ProtectClock = true; + ProtectKernelLogs = true; + Restart = "always"; + StandardOutput = "journal"; + }; + }; + }; +} diff --git a/src/wallfly/core.clj b/src/wallfly/core.clj index 0427cea..e969f23 100644 --- a/src/wallfly/core.clj +++ b/src/wallfly/core.clj @@ -1,6 +1,6 @@ (ns wallfly.core (:require [clojure.java.shell :as shell] - [clojure.core.async :refer [chan >!! ! !! password-file (slurp) (str/trim-newline)) + password (-> mqtt-password-file (slurp) (str/trim-newline)) username (get-username) hostname (get-hostname) host-device (format "wallfly-%s" (get-fqdn)) client-id (format "wallfly-%s" (rand-str 10)) client (create-mqtt-client mqtt-broker-uri client-id mqtt-username password) - reporter (create-reporter client time-to-idle location username hostname host-device) - stop-chan (execute! delay-time reporter)] + reporter (create-reporter client (Integer/parseInt time-to-idle) location username hostname host-device) + stop-chan (execute! (Integer/parseInt delay-time) reporter)] (.addShutdownHook (Runtime/getRuntime) (Thread. (fn [] (>!! catch-shutdown true)))) (