nixos-config/services/immich.nix

146 lines
4.3 KiB
Nix
Raw Normal View History

2022-08-06 16:07:29 +02:00
{ config, lib, pkgs, ... }:
let
2022-08-06 17:00:45 +02:00
dataDir = "/var/lib/immich";
uploadDir = "${dataDir}/upload";
2022-08-06 16:07:29 +02:00
dbuser = "immich";
dbname = "immich";
2022-08-06 17:00:45 +02:00
dbPasswordFile = config.age.secrets.immich-db-password.path;
2022-08-06 16:07:29 +02:00
ociBackend = config.virtualisation.oci-containers.backend;
2022-08-06 17:00:45 +02:00
containersHost = "localhost";
2022-08-06 16:07:29 +02:00
domain = "photos.felschr.com";
2022-08-06 17:00:45 +02:00
pgSuperUser = config.services.postgresql.superUser;
2022-08-07 12:17:29 +02:00
immichBase = {
2022-08-06 16:07:29 +02:00
environment = {
NODE_ENV = "production";
DB_HOSTNAME = containersHost;
DB_PORT = toString config.services.postgresql.port;
DB_USERNAME = dbuser;
DB_DATABASE_NAME = dbname;
REDIS_HOSTNAME = containersHost;
REDIS_PORT = toString config.services.redis.servers.immich.port;
};
2022-08-06 17:00:45 +02:00
# only secrets need to be included, e.g. DB_PASSWORD, JWT_SECRET, MAPBOX_KEY
2022-08-06 16:07:29 +02:00
environmentFiles = [ config.age.secrets.immich-env.path ];
2022-08-07 12:17:29 +02:00
extraOptions = [
"--network=host"
"--add-host=immich-server:127.0.0.1"
"--add-host=immich-microservices:127.0.0.1"
"--add-host=immich-machine-learning:127.0.0.1"
"--add-host=immich-web:127.0.0.1"
];
2022-08-06 16:07:29 +02:00
};
in {
age.secrets.immich-env.file = ../secrets/immich/.env.age;
2022-08-06 17:00:45 +02:00
age.secrets.immich-db-password.file = ../secrets/immich/db-password.age;
2022-08-06 16:07:29 +02:00
services.postgresql = {
enable = true;
2022-08-06 17:00:45 +02:00
enableTCPIP = true;
2022-08-06 16:07:29 +02:00
ensureDatabases = [ dbname ];
ensureUsers = [{
name = dbuser;
ensurePermissions."DATABASE ${dbname}" = "ALL PRIVILEGES";
}];
};
2022-08-06 17:00:45 +02:00
services.redis.servers.immich = {
enable = true;
port = 31640;
};
2022-08-06 16:07:29 +02:00
2022-08-06 17:00:45 +02:00
systemd.services.immich-init = {
2022-08-06 16:07:29 +02:00
enable = true;
2022-08-06 17:00:45 +02:00
description = "Set up paths & database access";
requires = [ "postgresql.service" ];
after = [ "postgresql.service" ];
2022-08-06 16:07:29 +02:00
before = [
"${ociBackend}-immich-server.service"
"${ociBackend}-immich-microservices.service"
"${ociBackend}-immich-machine-learning.service"
"${ociBackend}-immich-web.service"
"${ociBackend}-immich-proxy.service"
];
2022-08-06 17:00:45 +02:00
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
LoadCredential = [ "db_password:${dbPasswordFile}" ];
};
script = ''
mkdir -p ${dataDir} ${uploadDir}
echo "Set immich postgres user password"
db_password="$(<"$CREDENTIALS_DIRECTORY/db_password")"
${pkgs.sudo}/bin/sudo -u ${pgSuperUser} ${pkgs.postgresql}/bin/psql postgres \
-c "alter user ${dbuser} with password '$db_password'"
'';
2022-08-06 16:07:29 +02:00
};
virtualisation.oci-containers.containers = {
2022-08-07 12:17:29 +02:00
immich-server = immichBase // {
2022-08-06 16:07:29 +02:00
image = "altran1502/immich-server:release";
2022-08-06 17:00:45 +02:00
ports = [ "3001:3001" ];
2022-08-06 16:07:29 +02:00
entrypoint = "/bin/sh";
cmd = [ "./start-server.sh" ];
volumes = [ "${uploadDir}:/usr/src/app/upload" ];
};
2022-08-07 12:17:29 +02:00
immich-microservices = immichBase // {
2022-08-06 16:07:29 +02:00
image = "altran1502/immich-server:release";
entrypoint = "/bin/sh";
cmd = [ "./start-microservices.sh" ];
volumes = [ "${uploadDir}:/usr/src/app/upload" ];
};
2022-08-07 12:17:29 +02:00
immich-machine-learning = immichBase // {
2022-08-06 16:07:29 +02:00
image = "altran1502/immich-machine-learning:release";
entrypoint = "/bin/sh";
cmd = [ "./entrypoint.sh" ];
volumes = [ "${uploadDir}:/usr/src/app/upload" ];
};
2022-08-07 12:17:29 +02:00
immich-web = immichBase // {
2022-08-06 16:07:29 +02:00
image = "altran1502/immich-web:release";
2022-08-06 17:00:45 +02:00
ports = [ "3000:3000" ];
2022-08-06 16:07:29 +02:00
entrypoint = "/bin/sh";
cmd = [ "./entrypoint.sh" ];
};
};
systemd.services = {
"${ociBackend}-immich-server" = {
requires = [ "postgresql.service" "redis-immich.service" ];
after = [ "postgresql.service" "redis-immich.service" ];
};
"${ociBackend}-immich-microservices" = {
requires = [ "postgresql.service" "redis-immich.service" ];
after = [ "postgresql.service" "redis-immich.service" ];
};
"${ociBackend}-immich-machine-learning" = {
requires = [ "postgresql.service" ];
after = [ "postgresql.service" ];
};
};
services.nginx.virtualHosts.${domain} = {
enableACME = true;
forceSSL = true;
locations."/api" = {
proxyPass = "http://localhost:3001";
extraConfig = ''
rewrite /api/(.*) /$1 break;
client_max_body_size 50000M;
'';
};
locations."/" = {
proxyPass = "http://localhost:3000";
extraConfig = ''
client_max_body_size 50000M;
'';
};
};
}