Files
homelab/services/media/immich.nix
T
darmanandClaude Opus 5.5 eec1f33e96 feat(jupiter): shrink system closure for the 29G eMMC
Run immich and seerr as official images (their node/python stacks live in
podman's graphroot on the array), ship only the firmware the board loads,
and point jellyfin at intel-media-driver directly instead of
hardware.graphics (mesa + llvm).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 23:15:30 +02:00

182 lines
7.7 KiB
Nix

{ config, lib, pkgs, ... }:
# Immich photo/video library. Postgres (pgvector + vectorchord) and a unix-socket
# redis are native; the server and machine-learning run as the official images so
# their ~2.5G node/python/onnx stacks live in podman's graphroot on the array
# instead of the system closure on the eMMC.
#
# Storage lives under /mnt/data/AppData/immich, migrated from the old ZimaOS/CasaOS
# UPLOAD_LOCATION (same subfolder layout); see scripts/immich-import-legacy-db for
# the matching DB import.
#
# ⚠️ The immich DB is the only copy of albums/faces/dates — the files alone
# can't rebuild it. It joins the other unbacked databases on this network.
let
# Migrations are forward-only: never move this tag backwards.
version = "v3.2.4";
mediaLocation = "/mnt/data/AppData/immich";
mlCache = "/mnt/data/AppData/immich-ml-cache";
# Containers run as the host's immich user so postgres peer auth and the
# 0700 media dir keep working; ids are pinned below to keep this stable.
uid = 988;
gid = 985;
redisGid = 984;
usersGid = 100; # traverse /mnt/data/AppData (drwx--x--- darman:users)
# IMMICH_CONFIG_FILE is all-or-nothing: undeclared keys fall back to immich's
# defaults, not the admin UI's saved values (which stay in system_metadata and
# return if this file is dropped), and the admin settings UI goes read-only. An
# unknown/misspelled key is a hard startup failure, so keys are copied verbatim
# from `defaults` in immich's dist/config.js.
settings = {
server.externalDomain = "https://immich.mgaction.town";
newVersionCheck.enabled = false; # the image tag pins the version, not immich
# OIDC via Authentik on neptun; the application/provider is created by hand
# in its UI (like headscale's and headplane's, separate apps).
oauth = {
enabled = true;
# Authentik's per-application issuer. Trailing slash matters: immich
# appends /.well-known/openid-configuration to it.
issuerUrl = "https://auth.mgaction.town/application/o/immich/";
# Not a secret (it is sent in the browser redirect).
clientId = "FxrkWFe8keBrN83tu03UbAshSck52he2GphJDYSQ";
clientSecret = config.sops.placeholder.immich_oauth_client_secret;
scope = "openid email profile";
buttonText = "Login with Authentik";
# Matches by email, so the 2 imported users adopt their Authentik account
# instead of getting a duplicate.
autoRegister = true;
# Leave the password form reachable — autoLaunch would bounce straight
# to Authentik, locking everyone out if the OIDC app is misconfigured.
autoLaunch = false;
# Without this, logout dumps you on Authentik's own page instead of back
# here. Ends the immich session only; the Authentik SSO session survives.
endSessionEndpoint = "https://auth.mgaction.town/application/o/immich/end-session?post_logout_redirect_url=https://immich.mgaction.town";
# The mobile app can't follow a browser redirect back to a custom
# scheme through Authentik, so immich bounces it via this endpoint.
mobileOverrideEnabled = true;
mobileRedirectUri = "https://immich.mgaction.town/api/oauth/mobile-redirect";
};
};
runAs = [
"--network=host"
"--user=${toString uid}:${toString gid}"
"--group-add=${toString usersGid}"
];
in
{
# What the nixpkgs immich module set up besides the server itself — not used
# because evaluating it forces its (insecure, too old) default package.
services.postgresql = {
enable = true;
ensureDatabases = [ "immich" ];
ensureUsers = [{
name = "immich";
ensureDBOwnership = true;
ensureClauses.login = true;
}];
extensions = ps: [ ps.pgvector ps.vectorchord ];
settings = {
shared_preload_libraries = [ "vchord.so" ];
search_path = "\"$user\", public, vectors";
};
};
# Reindex when vectorchord changes version:
# https://docs.immich.app/administration/postgres-standalone/#updating-vectorchord
systemd.services.postgresql-setup.serviceConfig.ExecStartPost =
let
extensions = [ "unaccent" "uuid-ossp" "cube" "earthdistance" "pg_trgm" "vector" "vchord" ];
sqlFile = pkgs.writeText "immich-pgvectors-setup.sql" ''
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 immich;
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' config.services.postgresql.package "psql"} -d immich -f ${sqlFile}" ];
services.redis.servers.immich = {
enable = true;
port = 0; # unix socket only
};
users.users.immich = {
isSystemUser = true;
group = "immich";
inherit uid;
# /mnt/data/AppData is drwx--x--- darman:users; group "users" only grants traversal.
extraGroups = [ "users" ];
};
users.groups.immich.gid = gid;
users.groups.redis-immich.gid = redisGid;
sops.templates."immich-config.json" = {
owner = "immich";
content = builtins.toJSON settings;
};
virtualisation.oci-containers.containers = {
immich-server = {
image = "ghcr.io/immich-app/immich-server:${version}";
# :2283 on all interfaces but closed on the LAN (no firewall rule):
# reachable over tailscale0 and via localhost (caddy).
extraOptions = runAs ++ [ "--group-add=${toString redisGid}" ];
volumes = [
# Same path inside as out: the DB stores absolute asset paths.
"${mediaLocation}:${mediaLocation}"
"/run/postgresql:/run/postgresql"
"/run/redis-immich:/run/redis-immich"
"${config.sops.templates."immich-config.json".path}:/run/immich/config.json:ro"
"/etc/localtime:/etc/localtime:ro"
];
environment = {
DB_URL = "postgresql:///immich?host=/run/postgresql";
REDIS_SOCKET = "/run/redis-immich/redis.sock";
IMMICH_HOST = "0.0.0.0";
IMMICH_PORT = "2283";
IMMICH_MEDIA_LOCATION = mediaLocation;
IMMICH_MACHINE_LEARNING_URL = "http://127.0.0.1:3003";
IMMICH_CONFIG_FILE = "/run/immich/config.json";
};
};
immich-machine-learning = {
image = "ghcr.io/immich-app/immich-machine-learning:${version}";
extraOptions = runAs;
# Models (~1G) are re-downloaded on first use.
volumes = [ "${mlCache}:/cache" ];
environment = {
IMMICH_HOST = "127.0.0.1";
IMMICH_PORT = "3003";
MACHINE_LEARNING_CACHE_FOLDER = "/cache";
XDG_CACHE_HOME = "/cache";
MPLCONFIGDIR = "/cache/matplotlib";
};
};
};
systemd.services.podman-immich-server = {
requires = [ "postgresql.target" "redis-immich.service" ];
after = [ "postgresql.target" "redis-immich.service" ];
# Without this, immich starts before /mnt/data mounts and writes uploads onto
# the 29G eMMC, invisibly, under the future mountpoint.
unitConfig.RequiresMountsFor = [ "/mnt/data" ];
};
systemd.services.podman-immich-machine-learning.unitConfig.RequiresMountsFor = [ "/mnt/data" ];
# mediaLocation is outside /var/lib, so the module won't create it — this rule
# only adjusts perms on the dir the legacy import already created.
systemd.tmpfiles.rules = [
"d ${mediaLocation} 0700 immich immich -"
"d ${mlCache} 0700 immich immich -"
];
}