{ pkgs, common, ... }:
let
  dbName = "hass";
  domain = "beta.home.${common.domain}";
  port = 8085;
in
{

  services = {
    home-assistant = {
      enable = true;
      package =
        (pkgs.home-assistant.override {
          extraPackages =
            py: with py; [
              # Postgres
              psycopg2
              # Phillips hue
              aiohue
            ];
        }).overrideAttrs
          (oldAttrs: {
            # Avoid long install checks
            doInstallCheck = false;
          });
      extraComponents = [
        # Components required to complete the onboarding
        "esphome"
        "met"
        "radio_browser"
      ];
      config = {
        # Includes dependencies for a basic setup
        # https://www.home-assistant.io/integrations/default_config/
        default_config = { };
        homeassistant = {
          name = "Hjem";
          unit_system = "metric";
          temperature_unit = "C";
        };
        http = {
          server_host = "::1";
          trusted_proxies = [ "::1" ];
          use_x_forwarded_for = true;
          server_port = port;
        };
        recorder.db_url = "postgresql://@/${dbName}";
      };
    };

    nginx.virtualHosts.${domain} = {
      forceSSL = true;
      enableACME = true;
      extraConfig = ''
        proxy_buffering off;
      '';
      locations."/" = {
        proxyPass = "http://[::1]:${toString port}";
        proxyWebsockets = true;
      };
    };
    postgresql = {
      enable = true;
      ensureDatabases = [ dbName ];
      ensureUsers = [
        {
          name = dbName;
          ensureDBOwnership = true;
        }
      ];
    };
  };
}
