nixos/immich: do not create postgresql-setup if not using local db

The Immich module is unconditionally creating the postgresql-setup unit,
even if it never runs. This commit adds a guard that prevents the unit
from being created if the module is not managing a local db.
This commit is contained in:
Diogo Correia
2026-07-30 11:12:16 +01:00
committed by Sefa Eyeoglu
parent c6e9440dab
commit 56149c0fed

View File

@@ -325,41 +325,43 @@ in
search_path = "\"$user\", public, vectors";
};
};
systemd.services.postgresql-setup.serviceConfig.ExecStartPost =
let
extensions = [
"unaccent"
"uuid-ossp"
"cube"
"earthdistance"
"pg_trgm"
"vector"
"vchord"
systemd.services.postgresql-setup = mkIf cfg.database.enable {
serviceConfig.ExecStartPost =
let
extensions = [
"unaccent"
"uuid-ossp"
"cube"
"earthdistance"
"pg_trgm"
"vector"
"vchord"
];
sqlFile = pkgs.writeText "immich-pgvectors-setup.sql" ''
-- save previous version of vectorchord to trigger reindex on update
SELECT COALESCE(installed_version, ''') AS vchord_version_before FROM pg_available_extensions WHERE name = 'vchord' \gset
${lib.concatMapStringsSep "\n" (ext: "CREATE EXTENSION IF NOT EXISTS \"${ext}\";") extensions}
${lib.concatMapStringsSep "\n" (ext: "ALTER EXTENSION \"${ext}\" UPDATE;") extensions}
ALTER SCHEMA public OWNER TO ${cfg.database.user};
-- trigger reindex if vectorchord updates
-- https://docs.immich.app/administration/postgres-standalone/#updating-vectorchord
SELECT COALESCE(installed_version, ''') AS vchord_version_after FROM pg_available_extensions WHERE name = 'vchord' \gset
SELECT (:'vchord_version_before' != ''' AND :'vchord_version_before' != :'vchord_version_after') AS has_vchord_updated \gset
\if :has_vchord_updated
REINDEX INDEX face_index;
REINDEX INDEX clip_index;
\endif
'';
in
[
''
${lib.getExe' cfg.database.package "psql"} -d "${cfg.database.name}" -f "${sqlFile}"
''
];
sqlFile = pkgs.writeText "immich-pgvectors-setup.sql" ''
-- save previous version of vectorchord to trigger reindex on update
SELECT COALESCE(installed_version, ''') AS vchord_version_before FROM pg_available_extensions WHERE name = 'vchord' \gset
${lib.concatMapStringsSep "\n" (ext: "CREATE EXTENSION IF NOT EXISTS \"${ext}\";") extensions}
${lib.concatMapStringsSep "\n" (ext: "ALTER EXTENSION \"${ext}\" UPDATE;") extensions}
ALTER SCHEMA public OWNER TO ${cfg.database.user};
-- trigger reindex if vectorchord updates
-- https://docs.immich.app/administration/postgres-standalone/#updating-vectorchord
SELECT COALESCE(installed_version, ''') AS vchord_version_after FROM pg_available_extensions WHERE name = 'vchord' \gset
SELECT (:'vchord_version_before' != ''' AND :'vchord_version_before' != :'vchord_version_after') AS has_vchord_updated \gset
\if :has_vchord_updated
REINDEX INDEX face_index;
REINDEX INDEX clip_index;
\endif
'';
in
[
''
${lib.getExe' cfg.database.package "psql"} -d "${cfg.database.name}" -f "${sqlFile}"
''
];
};
services.redis.servers = mkIf cfg.redis.enable {
immich = {