From 7c69dc60873f10d8c8fff775968b3f788ffd305a Mon Sep 17 00:00:00 2001
From: Ethel Morgan <eth@ethulhu.co.uk>
Date: Wed, 24 Jun 2020 22:52:50 +0100
Subject: add catbus-lgtv daemons

---
 nixos/modules/module-list.nix                   |   2 +
 nixos/modules/services/catbus-actuator-lgtv.nix | 103 ++++++++++++++++++++++++
 nixos/modules/services/catbus-observer-lgtv.nix | 103 ++++++++++++++++++++++++
 pkgs/default.nix                                |   5 ++
 4 files changed, 213 insertions(+)
 create mode 100644 nixos/modules/services/catbus-actuator-lgtv.nix
 create mode 100644 nixos/modules/services/catbus-observer-lgtv.nix

diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
index e8e5f7f..a5e7dcc 100644
--- a/nixos/modules/module-list.nix
+++ b/nixos/modules/module-list.nix
@@ -5,8 +5,10 @@
   ./overlays.nix
   ./programs/dwm.nix
   ./services/ambience.nix
+  ./services/catbus-actuator-lgtv.nix
   ./services/catbus-actuator-wakeonlan.nix
   ./services/catbus-bridge-snapcast.nix
+  ./services/catbus-observer-lgtv.nix
   ./services/catbus-observer-networkpresence.nix
   ./services/dlnatoad.nix
   ./services/helix-directory-jackalope.nix
diff --git a/nixos/modules/services/catbus-actuator-lgtv.nix b/nixos/modules/services/catbus-actuator-lgtv.nix
new file mode 100644
index 0000000..73a2c72
--- /dev/null
+++ b/nixos/modules/services/catbus-actuator-lgtv.nix
@@ -0,0 +1,103 @@
+{ config, lib, pkgs, ... }:
+with lib;
+
+let
+
+  cfg = config.eth.services.catbus-actuator-lgtv;
+
+  configJSON = pkgs.writeText "config.json" (builtins.toJSON {
+    mqttBroker = "tcp://${cfg.mqttBroker.host}:${toString cfg.mqttBroker.port}";
+    apps = cfg.apps;
+    topics = cfg.topics;
+    tv = cfg.tv;
+  });
+
+in {
+
+  options.eth.services.catbus-actuator-lgtv = {
+
+    enable = mkEnableOption "Whether to enable the Catbus WebOS LGTV actuator";
+
+    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;
+      };
+    };
+
+    tv = {
+      host = mkOption {
+        type = types.str;
+        default = "";
+        description = "TV host";
+        example = "192.168.16.69";
+      };
+      key = mkOption {
+        type = types.str;
+        default = "";
+        description = "A key generated from pkgs.eth.catbus-lgtv/bin/generate-key";
+        example = "25561897424495c18042fef5ebe8d7fc";
+      };
+    };
+
+    topics = {
+      app = mkOption {
+        type = types.str;
+        description = "MQTT topic for controlling the TV's app";
+        example = "home/living-room/tv/app_enum";
+      };
+      appValues = mkOption {
+        type = types.str;
+        description = "MQTT topic for exporting the TV's apps";
+        example = "home/living-room/tv/app_enum/values";
+      };
+      power = mkOption {
+        type = types.str;
+        description = "MQTT topic for controlling the TV's power";
+        example = "home/living-room/tv/power";
+      };
+      volume = mkOption {
+        type = types.str;
+        description = "MQTT topic for controlling the TV's volume";
+        example = "home/living-room/tv/volume_percent";
+      };
+    };
+
+    apps = mkOption {
+      type = types.attrsOf types.str;
+      example = { PS4 = "com.webos.input.hdmi1"; };
+      description = "A set of friendly app names and their corresponding IDs";
+    };
+  };
+
+
+  config = mkIf cfg.enable {
+    systemd.services.catbus-actuator-lgtv = {
+      enable = true;
+      description = "Control a WebOS LGTV via Catbus";
+      wants = [ "network.target" ];
+      after = [ "network.target" ];
+      wantedBy = [ "multi-user.target" ];
+      serviceConfig = {
+        DynamicUser = true;
+
+        ExecStart = "${pkgs.eth.catbus-lgtv}/bin/catbus-actuator-lgtv --config-path ${configJSON}";
+
+        NoNewPrivileges = true;
+        ProtectKernelTunables = true;
+        ProtectControlGroups = true;
+        ProtectKernelModules = true;
+        RestrictAddressFamilies = "AF_INET AF_INET6";
+        RestrictNamespaces = true;
+      };
+    };
+  };
+
+}
+
diff --git a/nixos/modules/services/catbus-observer-lgtv.nix b/nixos/modules/services/catbus-observer-lgtv.nix
new file mode 100644
index 0000000..e1a293c
--- /dev/null
+++ b/nixos/modules/services/catbus-observer-lgtv.nix
@@ -0,0 +1,103 @@
+{ config, lib, pkgs, ... }:
+with lib;
+
+let
+
+  cfg = config.eth.services.catbus-observer-lgtv;
+
+  configJSON = pkgs.writeText "config.json" (builtins.toJSON {
+    mqttBroker = "tcp://${cfg.mqttBroker.host}:${toString cfg.mqttBroker.port}";
+    apps = cfg.apps;
+    topics = cfg.topics;
+    tv = cfg.tv;
+  });
+
+in {
+
+  options.eth.services.catbus-observer-lgtv = {
+
+    enable = mkEnableOption "Whether to enable the Catbus WebOS LGTV observer";
+
+    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;
+      };
+    };
+
+    tv = {
+      host = mkOption {
+        type = types.str;
+        default = "";
+        description = "TV host";
+        example = "192.168.16.69";
+      };
+      key = mkOption {
+        type = types.str;
+        default = "";
+        description = "A key generated from pkgs.eth.catbus-lgtv/bin/generate-key";
+        example = "25561897424495c18042fef5ebe8d7fc";
+      };
+    };
+
+    topics = {
+      app = mkOption {
+        type = types.str;
+        description = "MQTT topic for controlling the TV's app";
+        example = "home/living-room/tv/app_enum";
+      };
+      appValues = mkOption {
+        type = types.str;
+        description = "MQTT topic for exporting the TV's apps";
+        example = "home/living-room/tv/app_enum/values";
+      };
+      power = mkOption {
+        type = types.str;
+        description = "MQTT topic for controlling the TV's power";
+        example = "home/living-room/tv/power";
+      };
+      volume = mkOption {
+        type = types.str;
+        description = "MQTT topic for controlling the TV's volume";
+        example = "home/living-room/tv/volume_percent";
+      };
+    };
+
+    apps = mkOption {
+      type = types.attrsOf types.str;
+      example = { PS4 = "com.webos.input.hdmi1"; };
+      description = "A set of friendly app names and their corresponding IDs";
+    };
+  };
+
+
+  config = mkIf cfg.enable {
+    systemd.services.catbus-observer-lgtv = {
+      enable = true;
+      description = "Observe a WebOS LGTV via Catbus";
+      wants = [ "network.target" ];
+      after = [ "network.target" ];
+      wantedBy = [ "multi-user.target" ];
+      serviceConfig = {
+        DynamicUser = true;
+
+        ExecStart = "${pkgs.eth.catbus-lgtv}/bin/catbus-observer-lgtv --config-path ${configJSON}";
+
+        NoNewPrivileges = true;
+        ProtectKernelTunables = true;
+        ProtectControlGroups = true;
+        ProtectKernelModules = true;
+        RestrictAddressFamilies = "AF_INET AF_INET6";
+        RestrictNamespaces = true;
+      };
+    };
+  };
+
+}
+
diff --git a/pkgs/default.nix b/pkgs/default.nix
index 915c6ea..3b66e3a 100644
--- a/pkgs/default.nix
+++ b/pkgs/default.nix
@@ -1,4 +1,8 @@
 let
+  catbus-lgtv = builtins.fetchGit {
+    url = "https://github.com/ethulhu/catbus-lgtv";
+    rev = "6184414321f7f633dbb71cf70ed336e1c42d9d9e";
+  };
   catbus-lifx = builtins.fetchGit {
     url = "https://github.com/ethulhu/catbus-lifx";
     # TODO: rev = "...";
@@ -40,6 +44,7 @@ in
 
       dwm = pkgs.callPackage ./dwm {};
 
+      catbus-lgtv            = pkgs.callPackage catbus-lgtv            {};
       catbus-lifx            = pkgs.callPackage catbus-lifx            {};
       catbus-networkpresence = pkgs.callPackage catbus-networkpresence {};
       catbus-snapcast        = pkgs.callPackage catbus-snapcast        {};
-- 
cgit v1.2.3