immich-container/immich-container.nix

138 lines
3.9 KiB
Nix
Raw Normal View History

2024-02-07 14:09:35 -08:00
{ config, lib, pkgs, ... }:
with lib;
2024-02-07 15:11:07 -08:00
let
cfg = config.services.immichContainer;
hostname = config.instance.hostname;
mkEnvFile = attrs:
2024-02-07 15:18:42 -08:00
pkgs.writeText "env-file"
2024-02-07 15:16:33 -08:00
(concatStringsSep "\n" (mapAttrsToList (k: v: "${k}=${v}") attrs));
2024-02-07 15:11:07 -08:00
databasePassword = pkgs.lib.passwd.stablerandom-passwd-file "immich-db-passwd"
config.instance.build-seed;
2024-02-07 15:25:52 -08:00
hostSecrets = config.fudo.secrets.host-secrets."${hostname}";
2024-02-07 14:09:35 -08:00
in {
options.services.immichContainer = with types; {
enable =
mkEnableOption "Enable Immich photo server running in a container.";
state-directory = mkOption {
type = str;
description = "Path at which to store server state.";
};
store-directory = mkOption {
type = str;
description = "Path at which to store bulk server data.";
};
2024-02-07 14:32:51 -08:00
port = mkOption {
type = port;
description = "Port on which to listen for requests.";
default = 3254;
};
2024-02-07 14:09:35 -08:00
images = {
immich = mkOption {
type = str;
description = "Immich server docker image to use.";
};
immich-ml = mkOption {
type = str;
description = "Immich Machine Learning docker image to use.";
};
redis = mkOption {
type = str;
description = "Redis server docker image to use.";
};
postgresql = mkOption {
type = str;
description = "Postgresql server docker image to use.";
};
};
};
config = {
2024-02-07 15:11:07 -08:00
fudo.secrets.host-secrets."${hostname}".immichEnv = {
source-file = mkEnvFile {
2024-02-07 15:36:09 -08:00
DB_HOSTNAME = "database";
2024-02-07 15:11:07 -08:00
DB_USERNAME = "immich";
DB_DATABASE_NAME = "immich";
DB_PASSWORD = readFile databasePassword;
2024-02-07 15:36:09 -08:00
REDIS_HOSTNAME = "redis";
2024-02-07 15:11:07 -08:00
};
target-file = "/run/immich/env";
};
2024-02-07 14:15:43 -08:00
systemd.tmpfiles.rules = [
"d ${cfg.state-directory} 0750 root root - -"
"d ${cfg.store-directory} 0750 root root - -"
];
2024-02-07 14:50:14 -08:00
virtualisation.arion.projects.immich.settings = let
2024-02-07 14:09:35 -08:00
image = { ... }: {
project.name = "immich";
services = {
immich = {
service = {
image = cfg.images.immich;
restart = "always";
2024-02-07 14:35:08 -08:00
ports = [ "${toString cfg.port}:3001" ];
2024-02-07 14:09:35 -08:00
command = [ "start.sh" "immich" ];
2024-02-07 14:56:46 -08:00
depends_on =
[ "redis" "database" "immich-ml" "immich-microservices" ];
2024-02-07 14:09:35 -08:00
volumes = [
"${cfg.store-directory}:/usr/src/app/upload"
"/etc/localtime:/etc/localtime:ro"
];
2024-02-07 15:31:03 -08:00
env_file = [ hostSecrets.immichEnv.target-file ];
2024-02-07 14:09:35 -08:00
};
};
immich-microservices = {
service = {
image = cfg.images.immich;
restart = "always";
command = [ "start.sh" "microservices" ];
2024-02-07 14:56:46 -08:00
depends_on = [ "redis" "database" "immich-ml" ];
2024-02-07 14:09:35 -08:00
volumes = [
"${cfg.store-directory}:/usr/src/app/upload"
"/etc/localtime:/etc/localtime:ro"
];
2024-02-07 15:28:17 -08:00
env_file = [ hostSecrets.immichEnv.target-file ];
2024-02-07 14:09:35 -08:00
};
};
immich-ml = {
service = {
image = cfg.images.immich-ml;
restart = "always";
volumes = [ "${cfg.state-directory}/model-cache:/cache" ];
2024-02-07 15:28:17 -08:00
env_file = [ hostSecrets.immichEnv.target-file ];
2024-02-07 14:09:35 -08:00
};
};
redis.service = {
image = cfg.images.redis;
restart = "always";
volumes = [ "${cfg.state-directory}/redis:/var/lib/redis" ];
};
database = {
service = {
image = cfg.images.postgresql;
restart = "always";
volumes =
[ "${cfg.state-directory}/database:/var/lib/postgresql/data" ];
2024-02-07 15:28:17 -08:00
env_file = [ hostSecrets.immichEnv.target-file ];
2024-02-07 14:09:35 -08:00
};
};
};
};
2024-02-07 14:30:26 -08:00
in { imports = [ image ]; };
2024-02-07 14:09:35 -08:00
};
}