{ lib, config, ... }: { options.services.home-assistant = { postgres = { enable = lib.mkOption { type = lib.types.bool; default = false; description = "Use PostgreSQL instead of SQLite"; }; user = lib.mkOption { type = lib.types.str; default = "hass"; description = "Database user name"; }; database = lib.mkOption { type = lib.types.str; default = "hass"; description = "Database name"; }; }; }; config = lib.mkIf config.services.home-assistant.enable { assertions = [ { assertion = !config.services.home-assistant.postgres.enable || config.services.postgresql.enable; message = "PostgreSQL must be enabled when using postgres database for Home Assistant"; } ]; services.postgresql.databases.home-assistant = lib.mkIf config.services.home-assistant.postgres.enable { enable = true; user = config.services.home-assistant.postgres.user; database = config.services.home-assistant.postgres.database; }; services.home-assistant = lib.mkIf config.services.home-assistant.postgres.enable { extraPackages = python3Packages: with python3Packages; [ psycopg2 ]; }; systemd.services.home-assistant = lib.mkIf config.services.home-assistant.postgres.enable { requires = [ config.systemd.services.postgresql.name ]; }; }; }