diff --git a/hosts/jupiter/configuration.nix b/hosts/jupiter/configuration.nix index e907941..b329de0 100644 --- a/hosts/jupiter/configuration.nix +++ b/hosts/jupiter/configuration.nix @@ -65,13 +65,30 @@ # next value: acpi -> bios -> cold -> efi. boot.kernelParams = [ "reboot=pci" ]; + # ---- Closure size (29G eMMC holds two generations during a switch) ---- + # Only the firmware this board loads (i915 DMC + r8169), not all ~850M of linux-firmware. + hardware.enableRedistributableFirmware = false; + hardware.cpu.intel.updateMicrocode = true; + hardware.firmware = [ + (pkgs.runCommand "jupiter-firmware" { } '' + mkdir -p $out/lib/firmware/i915 + cp -r ${pkgs.linux-firmware}/lib/firmware/rtl_nic $out/lib/firmware/ + cp ${pkgs.linux-firmware}/lib/firmware/i915/bxt_* $out/lib/firmware/i915/ + '') + ]; + # Pins a ~200M nixpkgs source; the box is only ever deployed to, never built on. + nixpkgs.flake.setFlakeRegistry = false; + nixpkgs.flake.setNixPath = false; + nix.settings.auto-optimise-store = true; + # ---- GPU (jellyfin hardware transcoding) ---- - # Apollo Lake N3450 / HD Graphics 500 (Gen9). i915 binds on its own, but VAAPI needs - # the iHD userspace driver (Gen9; i965 is Gen8-only) or jellyfin-ffmpeg exits 251 on - # every transcode with no clearer error than "FFmpeg exited with code 251" in the log. - hardware.graphics = { - enable = true; - extraPackages = [ pkgs.intel-media-driver ]; + # Apollo Lake N3450 / HD Graphics 500 (Gen9). VAAPI needs the iHD userspace driver + # (Gen9; i965 is Gen8-only) or jellyfin-ffmpeg exits 251 on every transcode with no + # clearer error than "FFmpeg exited with code 251" in the log. Pointed at directly + # instead of via hardware.graphics, which drags in mesa + llvm (~850M) for nothing. + systemd.services.jellyfin.environment = { + LIBVA_DRIVER_NAME = "iHD"; + LIBVA_DRIVERS_PATH = "${pkgs.intel-media-driver}/lib/dri"; }; # ⚠️ Use VAAPI, not QSV, in jellyfin's UI — QSV needs an MFX runtime not safely # available for this Gen9 chip (only insecure/EOL options) and fails with exit 171. @@ -92,14 +109,14 @@ options = [ "nofail" ]; # don't block boot if the array is degraded/absent }; - # `nofail` alone isn't enough — mounts layered on the array (prowlarr/seerr binds) + # `nofail` alone isn't enough — mounts layered on the array (the prowlarr bind) # are RequiredBy local-fs.target and can still trip Emergency Mode, which is a dead # end here (root locked, no ssh). Boot as far as possible instead; the array-backed # services carry RequiresMountsFor=/mnt/data so they still won't write to the eMMC. systemd.enableEmergencyMode = false; # ---- Heavy state moved off the eMMC ---- - # A deploy holds two full closures (~9G each) on this 29G disk at once, so state + # A deploy holds two full closures (~4G each) on this 29G disk at once, so state # that grows on its own can't live there — moved under /mnt/data/AppData like every # other service's state. Settings below are jupiter-only; services/containers.nix # stays engine/host-agnostic (mercury runs podman with no array). @@ -133,7 +150,7 @@ # Without this, podman would recreate an empty store on the eMMC if the array mounts # late or is absent, and the runner would re-pull every image. - # (podman-clonarr already sets this in services/media/clonarr.nix.) + # (the media containers set this in their own modules.) systemd.services.podman.unitConfig.RequiresMountsFor = [ "/mnt/data" ]; systemd.services.gitea-runner-jupiter.unitConfig.RequiresMountsFor = [ "/mnt/data" ]; diff --git a/hosts/jupiter/secrets.nix b/hosts/jupiter/secrets.nix index b50def3..5c1d19f 100644 --- a/hosts/jupiter/secrets.nix +++ b/hosts/jupiter/secrets.nix @@ -21,8 +21,8 @@ sops.secrets.tailscale_authkey = { }; # Immich's OIDC client secret (separate Authentik app from headscale/headplane, see - # hosts/neptun/secrets.nix). Resolved via systemd LoadCredential as root before - # privilege drop, so sops's default root:root 0400 is correct — do NOT set `owner`. + # hosts/neptun/secrets.nix). Only rendered into the immich-owned + # `immich-config.json` template (services/media/immich.nix), so it stays root:root. sops.secrets.immich_oauth_client_secret = { }; # Gitea Actions runner registration token — gitea generates this itself once Actions diff --git a/services/media/immich.nix b/services/media/immich.nix index ec3fdf5..44d3af9 100644 --- a/services/media/immich.nix +++ b/services/media/immich.nix @@ -1,103 +1,181 @@ -{ config, pkgs, inputs, ... }: +{ config, lib, pkgs, ... }: -# Immich photo/video library. Native nixpkgs module, not the upstream compose -# stack — it owns its own postgres (pgvector + vectorchord) and a unix-socket redis. +# 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 postgres cluster itself stays on the OS disk. +# 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 - # Package pinned to nixpkgs-unstable (3.0.3) while the module stays on the 26.05 - # pin (2.7.5) — safe only because the two module files are byte-identical - # (verified by diff; re-check on any input bump). Needed because immich's - # migrations are forward-only and jupiter's imported DB was last written by - # 3.0.0, which 2.7.5 refuses to start against; drop once the pin ships >= 3.0.0. - # diff <(nixpkgs)/nixos/modules/services/web-apps/immich.nix \ - # <(unstable)/nixos/modules/services/web-apps/immich.nix - unstable = import inputs.nixpkgs-unstable { - inherit (pkgs.stdenv.hostPlatform) system; + # 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 { - services.immich = { + # 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; - # Both the server and immich-machine-learning follow this: the module takes - # the ML service from cfg.package.machine-learning (passthru). - package = unstable.immich; - # Listens on all interfaces: :2283 stays closed on the LAN (no - # openFirewall), reachable over tailscale0 and via localhost (caddy). - host = "0.0.0.0"; - port = 2283; - mediaLocation = "/mnt/data/AppData/immich"; - machine-learning.enable = true; - - # ⚠️ Setting `settings` at all switches immich to IMMICH_CONFIG_FILE mode, - # which 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 block is deleted), and the admin settings UI goes read-only. An - # unknown/misspelled key is a hard startup failure here (just a warning - # without a config file), so keys are copied verbatim from `defaults` in - # immich's dist/config.js. + ensureDatabases = [ "immich" ]; + ensureUsers = [{ + name = "immich"; + ensureDBOwnership = true; + ensureClauses.login = true; + }]; + extensions = ps: [ ps.pgvector ps.vectorchord ]; settings = { - server.externalDomain = "https://immich.mgaction.town"; - newVersionCheck.enabled = false; # nixpkgs 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) — only the - # client secret is managed here (hosts/neptun/secrets.nix). - 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/"; - # Authentik generates this; not a secret (it is sent in the browser - # redirect), so it lives here rather than in sops. - clientId = "FxrkWFe8keBrN83tu03UbAshSck52he2GphJDYSQ"; - # Read at runtime via systemd LoadCredential — never lands in the - # world-readable /nix/store copy of the rendered config. - clientSecret._secret = config.sops.secrets.immich_oauth_client_secret.path; - 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, immich falls back to the IdP's discovered - # end_session_endpoint and logout dumps you on Authentik's own page - # instead of back here — must be an absolute url, mirroring immich's - # internal LOGIN_URL. This ends the immich session only; the Authentik - # SSO session survives, so the next login skips the credential prompt — - # drop this line to end both. - 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"; - }; + shared_preload_libraries = [ "vchord.so" ]; + search_path = "\"$user\", public, vectors"; }; - # Hardware transcoding needs accelerationDevices set explicitly (e.g. - # "/dev/dri/renderD128"); default CPU-only transcode is slow on the - # ZimaBlade's Celeron but only runs on upload. + }; + # 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 }; - # /mnt/data/AppData is drwx--x--- darman:users; immich only needs group "users" - # to traverse into it — the dir itself stays 0700 immich:immich (tmpfiles + - # UMask=0077 reassert that), so this grants nothing else. - users.users.immich.extraGroups = [ "users" ]; + 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 /mnt/data/AppData/immich 0700 immich immich -" + "d ${mediaLocation} 0700 immich immich -" + "d ${mlCache} 0700 immich immich -" ]; - - # The unit's automatic RequiresMountsFor doesn't cover mediaLocation — without - # this, immich starts before /mnt/data mounts and writes uploads onto the 29G - # eMMC, invisibly, under the future mountpoint. - systemd.services.immich-server.unitConfig.RequiresMountsFor = [ "/mnt/data" ]; } diff --git a/services/media/jellyfin.nix b/services/media/jellyfin.nix index 2223924..66c5cd4 100644 --- a/services/media/jellyfin.nix +++ b/services/media/jellyfin.nix @@ -10,7 +10,7 @@ # "video"/"render" cover the DRI nodes for hardware transcoding — card1 is # 0660 root:video (not guaranteed 0666 like renderD128), so don't rely on # device perms alone. Harmless on a GPU-less host: the driver itself is - # enabled per-host (e.g. jupiter's hardware.graphics + intel-media-driver). + # enabled per-host (e.g. jupiter's LIBVA_DRIVERS_PATH -> intel-media-driver). users.users.jellyfin.extraGroups = [ "users" "video" "render" ]; # The upstream module hardcodes UMask=0077, which made jellyfin write diff --git a/services/media/seerr.nix b/services/media/seerr.nix index 889f2d0..9ebf884 100644 --- a/services/media/seerr.nix +++ b/services/media/seerr.nix @@ -1,26 +1,27 @@ { ... }: # Seerr (formerly Jellyseerr) — request manager for Jellyfin, talking to -# Sonarr/Radarr; fresh install, no migrated data. configDir stays at the -# module default, with AppData bind-mounted onto it instead (same -# DynamicUser/StateDirectory issue as prowlarr.nix — see that file for why, -# and why the mount targets /var/lib/private/seerr rather than the public path). +# Sonarr/Radarr. Runs the official image rather than the nixpkgs package, whose +# ~1.5G node_modules would otherwise sit in the system closure on the eMMC. { - services.seerr.enable = true; - - # `nofail` for the same reason as prowlarr.nix — see the comment there: an - # array-backed bind without it fails local-fs.target and boots to an - # unusable emergency shell. - fileSystems."/var/lib/private/seerr" = { - device = "/mnt/data/AppData/seerr"; - fsType = "none"; - options = [ "bind" "nofail" ]; + virtualisation.oci-containers.containers.seerr = { + image = "ghcr.io/seerr-team/seerr:v3.2.0"; + # Host networking so "localhost" reaches jellyfin/sonarr/radarr (native + # services) — same reason as clonarr.nix. Listens on :5055. + extraOptions = [ "--network=host" "--user=1000:100" "--init" ]; + volumes = [ "/mnt/data/AppData/seerr:/app/config" ]; + environment = { + TZ = "Europe/Berlin"; + PORT = "5055"; + }; }; - # Only /var/lib/seerr (eMMC) is picked up automatically; pin to the array. - systemd.services.seerr.unitConfig.RequiresMountsFor = [ "/mnt/data" ]; - + # Z: the config was written by the old module's DynamicUser (a random uid); + # hand it to darman, which the container runs as. systemd.tmpfiles.rules = [ "d /mnt/data/AppData/seerr 0755 darman users -" + "Z /mnt/data/AppData/seerr - darman users -" ]; + + systemd.services.podman-seerr.unitConfig.RequiresMountsFor = [ "/mnt/data" ]; }