From 0e5d8e3335764f411f4d1d27d2fdff8fd5a8925a Mon Sep 17 00:00:00 2001 From: Leyla Becker Date: Sat, 8 Mar 2025 05:05:32 -0600 Subject: [PATCH] added postgres config to home assistant --- .../nixos-modules/server/home-assistant.nix | 42 +++++++++++++++---- modules/nixos-modules/server/postgres.nix | 1 + 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/modules/nixos-modules/server/home-assistant.nix b/modules/nixos-modules/server/home-assistant.nix index 967846a..5c4b81f 100644 --- a/modules/nixos-modules/server/home-assistant.nix +++ b/modules/nixos-modules/server/home-assistant.nix @@ -1,9 +1,11 @@ { lib, + pkgs, config, ... }: let configDir = "/var/lib/hass"; + db_user = "hass"; in { options.host.home-assistant = { enable = lib.mkEnableOption "should home-assistant be enabled on this computer"; @@ -16,8 +18,18 @@ in { config = lib.mkIf config.host.home-assistant.enable (lib.mkMerge [ { + systemd.tmpfiles.rules = [ + "f ${config.services.home-assistant.configDir}/automations.yaml 0755 hass hass" + ]; services.home-assistant = { enable = true; + package = + (pkgs.home-assistant.override { + extraPackages = py: with py; [psycopg2]; + }) + .overrideAttrs (oldAttrs: { + doInstallCheck = false; + }); configDir = configDir; extraComponents = [ "met" @@ -35,14 +47,6 @@ in { "openweathermap" ]; config = { - homeassistant = { - name = "Home"; - latitude = "!secret latitude"; - longitude = "!secret longitude"; - elevation = "!secret elevation"; - unit_system = "metric"; - time_zone = "CDT"; - }; http = { server_port = 8082; use_x_forwarded_for = true; @@ -50,6 +54,9 @@ in { ip_ban_enabled = true; login_attempts_threshold = 10; }; + recorder.db_url = "postgresql://@/${db_user}"; + "automation manual" = []; + "automation ui" = "!include automations.yaml"; }; extraPackages = python3Packages: with python3Packages; [ @@ -57,6 +64,11 @@ in { gtts ]; }; + systemd.services.home-assistant = { + requires = [ + "postgresql.service" + ]; + }; host = { reverse_proxy.subdomains.${config.host.home-assistant.subdomain} = { target = "http://localhost:${toString config.services.home-assistant.config.http.server_port}"; @@ -77,6 +89,20 @@ in { proxy_read_timeout 90; ''; }; + postgres = { + enable = true; + extraUsers = { + ${db_user} = { + isClient = true; + createUser = true; + }; + }; + extraDatabases = { + ${db_user} = { + name = db_user; + }; + }; + }; }; } (lib.mkIf config.host.impermanence.enable { diff --git a/modules/nixos-modules/server/postgres.nix b/modules/nixos-modules/server/postgres.nix index 8d57d42..71ce44c 100644 --- a/modules/nixos-modules/server/postgres.nix +++ b/modules/nixos-modules/server/postgres.nix @@ -65,6 +65,7 @@ in { ++ ( builtins.map (user: { name = user.name; + ensureDBOwnership = true; }) createUsers );