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"; Restart = "on-failure"; RestartSec = 10; }; }; }; }