added postgres config to home assistant

This commit is contained in:
Leyla Becker 2025-03-08 05:05:32 -06:00
parent 9715517af6
commit 0e5d8e3335
2 changed files with 35 additions and 8 deletions

View file

@ -1,9 +1,11 @@
{ {
lib, lib,
pkgs,
config, config,
... ...
}: let }: let
configDir = "/var/lib/hass"; configDir = "/var/lib/hass";
db_user = "hass";
in { in {
options.host.home-assistant = { options.host.home-assistant = {
enable = lib.mkEnableOption "should home-assistant be enabled on this computer"; 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 [ 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 = { services.home-assistant = {
enable = true; enable = true;
package =
(pkgs.home-assistant.override {
extraPackages = py: with py; [psycopg2];
})
.overrideAttrs (oldAttrs: {
doInstallCheck = false;
});
configDir = configDir; configDir = configDir;
extraComponents = [ extraComponents = [
"met" "met"
@ -35,14 +47,6 @@ in {
"openweathermap" "openweathermap"
]; ];
config = { config = {
homeassistant = {
name = "Home";
latitude = "!secret latitude";
longitude = "!secret longitude";
elevation = "!secret elevation";
unit_system = "metric";
time_zone = "CDT";
};
http = { http = {
server_port = 8082; server_port = 8082;
use_x_forwarded_for = true; use_x_forwarded_for = true;
@ -50,6 +54,9 @@ in {
ip_ban_enabled = true; ip_ban_enabled = true;
login_attempts_threshold = 10; login_attempts_threshold = 10;
}; };
recorder.db_url = "postgresql://@/${db_user}";
"automation manual" = [];
"automation ui" = "!include automations.yaml";
}; };
extraPackages = python3Packages: extraPackages = python3Packages:
with python3Packages; [ with python3Packages; [
@ -57,6 +64,11 @@ in {
gtts gtts
]; ];
}; };
systemd.services.home-assistant = {
requires = [
"postgresql.service"
];
};
host = { host = {
reverse_proxy.subdomains.${config.host.home-assistant.subdomain} = { reverse_proxy.subdomains.${config.host.home-assistant.subdomain} = {
target = "http://localhost:${toString config.services.home-assistant.config.http.server_port}"; target = "http://localhost:${toString config.services.home-assistant.config.http.server_port}";
@ -77,6 +89,20 @@ in {
proxy_read_timeout 90; 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 { (lib.mkIf config.host.impermanence.enable {

View file

@ -65,6 +65,7 @@ in {
++ ( ++ (
builtins.map (user: { builtins.map (user: {
name = user.name; name = user.name;
ensureDBOwnership = true;
}) })
createUsers createUsers
); );