From 088643cae5be8f7d4b98af429f3d9fcc2e0ecba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Thu, 21 Nov 2024 15:51:55 +0100 Subject: [PATCH] Create module --- flake.nix | 5 +++ module.nix | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 module.nix diff --git a/flake.nix b/flake.nix index ad2baaf..8bd1b65 100644 --- a/flake.nix +++ b/flake.nix @@ -17,6 +17,11 @@ "x86_64-linux" ]; in { + nixosModules = rec { + hcpy = import ./module.nix self; + default = hcpy; + }; + packages = eachSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; in rec { diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..f7352ac --- /dev/null +++ b/module.nix @@ -0,0 +1,102 @@ +self: { + config, + lib, + pkgs, + system, + ... +}: +with lib; let + cfg = config.services.hcpy; + bridgeConfig = + cfg.config + // { + devices_file = cfg.devicesFile; + }; + hcpy = pkgs.writeShellApplication { + name = "hcpy-service"; + runtimeInputs = [self.packages.${system}.default]; + text = '' + if [ ! -f "${cfg.devicesFile}" ]; then + echo "Device file does not exist, requesting login to HomeConnect cloud"; + hcpy login "$(cat "${cfg.homeconnect.usernameFile}")" \ + "$(cat "${cfg.homeconnect.passwordFile}")" \ + "${cfg.devicesFile}"; + fi + + hcpy run ${cli.toGNUCommandLineShell {} bridgeConfig} \ + --mqtt_username "$(cat "${cfg.mqtt.usernameFile}")" \ + --mqtt_password "$(cat "${cfg.mqtt.passwordFile}")" + ''; + }; +in { + options.services.hcpy = { + enable = mkEnableOption "HomeConnect bridge service"; + + homeconnect = { + usernameFile = mkOption { + type = types.path; + description = "Path to file containing single line with HomeConnect account username"; + example = "/run/hc.username.key"; + }; + + passwordFile = mkOption { + type = types.path; + description = "Path to file containing single line with HomeConnect account password"; + example = "/run/hc.password.key"; + }; + }; + + mqtt = { + usernameFile = mkOption { + type = types.path; + description = "Path to file containing single line with MQTT client username"; + example = "/run/mqtt.username.key"; + }; + + passwordFile = mkOption { + type = types.path; + description = "Path to file containing single line with MQTT client password"; + example = "/run/mqtt.password.key"; + }; + }; + + devicesFile = mkOption { + type = types.path; + description = "Path to JSON file fetched from HomeConnect API"; + example = "/var/lib/hcpy/devices.json"; + }; + + config = mkOption { + type = types.attrs; + description = "The configuration of hcpy bridge"; + example = { + mqtt_host = "localhost"; + mqtt_prefix = "homeconnect/"; + mqtt_port = 8883; + mqtt_ssl = true; + mqtt_cafile = "/run/ca.pem"; + mqtt_certfile = "/run/cert.crt"; + mqtt_keyfile = "/run/cert.key"; + mqtt_clientname = "hcpy1"; + domain_suffix = ""; + debug = true; + ha-discovery = true; + }; + }; + }; + + config = mkIf cfg.enable { + systemd.services.hcpy = { + enable = true; + description = "HomeConnect bridge service"; + + wantedBy = ["multi-user.target"]; + wants = ["network-online.target"]; + after = ["network-online.target"]; + + serviceConfig = { + ExecStart = "${hcpy}/bin/hcpy-service"; + }; + }; + }; +}