From ea950b8c4cf72034e4f000aee58be6b9b5d489d0 Mon Sep 17 00:00:00 2001
From: Ethel Morgan <eth@ethulhu.co.uk>
Date: Wed, 24 Jun 2020 14:21:31 +0100
Subject: add pkgs.catbus-networkpresence, and corresponding service

---
 nixos/modules/module-list.nix                      |  1 +
 .../services/catbus-observer-networkpresence.nix   | 95 ++++++++++++++++++++++
 2 files changed, 96 insertions(+)
 create mode 100644 nixos/modules/services/catbus-observer-networkpresence.nix

(limited to 'nixos')

diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
index 4f8e072..dddb83d 100644
--- a/nixos/modules/module-list.nix
+++ b/nixos/modules/module-list.nix
@@ -6,6 +6,7 @@
   ./programs/dwm.nix
   ./services/catbus-actuator-wakeonlan.nix
   ./services/catbus-bridge-snapcast.nix
+  ./services/catbus-observer-networkpresence.nix
   ./services/dlnatoad.nix
   ./services/helix-directory-jackalope.nix
   ./services/helix-directory.nix
diff --git a/nixos/modules/services/catbus-observer-networkpresence.nix b/nixos/modules/services/catbus-observer-networkpresence.nix
new file mode 100644
index 0000000..be48e27
--- /dev/null
+++ b/nixos/modules/services/catbus-observer-networkpresence.nix
@@ -0,0 +1,95 @@
+{ config, lib, pkgs, ... }:
+with lib;
+
+let
+
+  cfg = config.eth.services.catbus-observer-networkpresence;
+
+  configJSON = pkgs.writeText "config.json" ''
+      {
+        "mqttBroker": "tcp://${cfg.mqttBroker.host}:${toString cfg.mqttBroker.port}",
+
+        "devices": {
+          "TV": {
+            "mac": "${cfg.devices.tv.mac}",
+            "topic": "${cfg.devices.tv.topic}"
+          }
+        }
+      }
+  '';
+
+in {
+
+  options.eth.services.catbus-observer-networkpresence = {
+
+    enable = mkEnableOption "Whether to enable the Catbus network-presence observer";
+
+    interface = mkOption {
+      type = types.str;
+      description = "interface to scan";
+      default = "";
+      example = "enp2s0";
+    };
+
+    mqttBroker = {
+      host = mkOption {
+        type = types.str;
+        description = "Host of the MQTT broker.";
+        example = "localhost";
+      };
+      port = mkOption {
+        type = types.int;
+        description = "Port of the MQTT broker.";
+        default = 1883;
+      };
+    };
+
+    # TODO: replace this with a proper set of option sets.
+    devices = {
+      tv = {
+        mac = mkOption {
+          type = types.str;
+          description = "The device's MAC address";
+          example = "aa:bb:cc:dd:ee:ff";
+        };
+        topic = mkOption {
+          type = types.str;
+          description = "MQTT topic for controlling the device";
+          example = "home/house/speakers/power";
+        };
+      };
+    };
+  };
+
+
+  config = mkIf cfg.enable {
+    assertions = [
+      {
+        assertion = cfg.interface != "";
+        message = "must set config.eth.services.catbus-observer-networkpresence.interface";
+      }
+    ];
+
+    systemd.services.catbus-observer-networkpresence = {
+      enable = true;
+      description = "Detect devices on the network to publish to Catbus";
+      wants = [ "network.target" ];
+      after = [ "network.target" ];
+      wantedBy = [ "multi-user.target" ];
+      serviceConfig = {
+        DynamicUser = true;
+        AmbientCapabilities = "CAP_NET_RAW CAP_NET_ADMIN";
+
+        ExecStart = "${pkgs.eth.catbus-networkpresence}/bin/catbus-observer-networkpresence --config-path ${configJSON} --interface ${cfg.interface}";
+
+        NoNewPrivileges = true;
+        ProtectKernelTunables = true;
+        ProtectControlGroups = true;
+        ProtectKernelModules = true;
+        RestrictNamespaces = true;
+      };
+    };
+  };
+
+}
+
-- 
cgit v1.2.3