Add selfhosted services

This commit is contained in:
aloslider
2026-06-30 04:26:40 +03:00
parent f212c874b5
commit 13e407847f
78 changed files with 2346 additions and 690 deletions
@@ -0,0 +1,38 @@
{ config, lib, ... }:
with lib;
let
common = config.selfhosted.common;
cfg = config.selfhosted.immich;
in
{
imports = [
./immich
./ml
./postgresql
./redis
];
options.selfhosted.immich = {
targetName = mkOption {
type = types.str;
};
version = mkOption {
type = types.str;
};
};
config = {
sops.secrets = {
"immich/db_password" = { };
"immich/client_id" = { };
"immich/client_secret" = { };
};
systemd.targets."${common.backend}-${cfg.targetName}" = {
unitConfig = {
Description = "Root target for ${cfg.targetName}";
};
wantedBy = [ "${common.backend}-${common.rootTarget.name}.target" ];
};
};
}
@@ -0,0 +1,6 @@
{ config, ... }:
{
sops.templates."immich.env".content = ''
DB_PASSWORD=${config.sops.placeholder."immich/db_password"}
'';
}
@@ -0,0 +1,71 @@
{ config, lib, ... }:
with lib;
let
common = config.selfhosted.common;
cfg = config.selfhosted.immich;
cfgPath = "/config/immich.json";
hostDataPath = "${common.dataDir}/immich";
in
{
imports = [ ./config.nix ];
options.selfhosted.immich.immich = {
name = mkOption {
type = types.str;
};
subdomain = mkOption {
type = types.str;
};
hostPort = mkOption {
type = types.port;
};
};
config = {
virtualisation.oci-containers.containers."${cfg.immich.name}" = {
image = "ghcr.io/immich-app/immich-server:${cfg.version}";
ports = [
"0.0.0.0:${toString cfg.immich.hostPort}:2283"
];
environmentFiles = [ config.sops.templates."immich.env".path ];
environment = {
IMMICH_VERSION = cfg.version;
DB_HOSTNAME = cfg.postgresql.name;
DB_DATABASE_NAME = cfg.postgresql.db;
DB_USERNAME = cfg.postgresql.user;
REDIS_HOSTNAME = cfg.redis.name;
};
volumes = [
"${hostDataPath}/data:/data"
"/etc/localtime:/etc/localtime:ro"
];
log-driver = "journald";
extraOptions = [
"--network=${common.network.name}"
];
};
systemd.services."${common.backend}-${cfg.immich.name}" = {
serviceConfig = {
Restart = lib.mkOverride 90 "always";
};
after = [
"${common.backend}-network-${common.network.name}.service"
"${common.backend}-${cfg.postgresql.name}.service"
"${common.backend}-${cfg.redis.name}.service"
"${common.backend}-${config.selfhosted.traefik.name}.service"
];
requires = [
"${common.backend}-network-${common.network.name}.service"
"${common.backend}-${cfg.postgresql.name}.service"
"${common.backend}-${cfg.redis.name}.service"
"${common.backend}-${config.selfhosted.traefik.name}.service"
];
partOf = [ "${common.backend}-${cfg.targetName}.target" ];
wantedBy = [ "${common.backend}-${cfg.targetName}.target" ];
preStart = ''
mkdir -p ${hostDataPath}/data
'';
};
};
}
@@ -0,0 +1,6 @@
{ config, ... }:
{
sops.templates."immich-db.env".content = ''
POSTGRES_PASSWORD=${config.sops.placeholder."immich/db_password"}
'';
}
@@ -0,0 +1,42 @@
{ config, lib, ... }:
with lib;
let
common = config.selfhosted.common;
cfg = config.selfhosted.immich;
hostDataPath = "${common.dataDir}/immich";
in
{
options.selfhosted.immich.ml = {
name = mkOption {
type = types.str;
};
};
config = {
virtualisation.oci-containers.containers."${cfg.ml.name}" = {
image = "ghcr.io/immich-app/immich-machine-learning:${cfg.version}";
environment = {
IMMICH_VERSION = cfg.version;
};
volumes = [
"${hostDataPath}/cache:/cache"
];
extraOptions = [
"--network=${common.network.name}"
];
};
systemd.services."${common.backend}-${cfg.ml.name}" = {
serviceConfig = {
Restart = lib.mkOverride 90 "always";
};
after = [ "${common.backend}-network-${common.network.name}.service" ];
requires = [ "${common.backend}-network-${common.network.name}.service" ];
partOf = [ "${common.backend}-${cfg.targetName}.target" ];
wantedBy = [ "${common.backend}-${cfg.targetName}.target" ];
preStart = ''
mkdir -p ${hostDataPath}/cache
'';
};
};
}
@@ -0,0 +1,6 @@
{ config, ... }:
{
sops.templates."immich-db.env".content = ''
POSTGRES_PASSWORD=${config.sops.placeholder."immich/db_password"}
'';
}
@@ -0,0 +1,55 @@
{ config, lib, ... }:
with lib;
let
common = config.selfhosted.common;
cfg = config.selfhosted.immich;
hostDataPath = "${common.dataDir}/immich";
in
{
imports = [ ./config.nix ];
options.selfhosted.immich.postgresql = {
name = mkOption {
type = types.str;
};
db = mkOption {
type = types.str;
};
user = mkOption {
type = types.str;
};
};
config = {
virtualisation.oci-containers.containers."${cfg.postgresql.name}" = {
image = "ghcr.io/immich-app/postgres:14-vectorchord0.4.3-pgvectors0.2.0@sha256:bcf63357191b76a916ae5eb93464d65c07511da41e3bf7a8416db519b40b1c23";
environmentFiles = [ config.sops.templates."immich-db.env".path ];
environment = {
POSTGRES_DB = cfg.postgresql.db;
POSTGRES_USER = cfg.postgresql.user;
POSTGRES_INITDB_ARGS = "--data-checksums";
};
volumes = [
"${hostDataPath}/db:/var/lib/postgresql/data"
];
log-driver = "journald";
extraOptions = [
"--network=${common.network.name}"
"--shm-size=128m"
];
};
systemd.services."${common.backend}-${cfg.postgresql.name}" = {
serviceConfig = {
Restart = lib.mkOverride 90 "always";
};
after = [ "${common.backend}-network-${common.network.name}.service" ];
requires = [ "${common.backend}-network-${common.network.name}.service" ];
partOf = [ "${common.backend}-${cfg.targetName}.target" ];
wantedBy = [ "${common.backend}-${cfg.targetName}.target" ];
preStart = ''
mkdir -p ${hostDataPath}/db
'';
};
};
}
@@ -0,0 +1,34 @@
{ config, lib, ... }:
with lib;
let
common = config.selfhosted.common;
cfg = config.selfhosted.immich;
in
{
options.selfhosted.immich.redis = {
name = mkOption {
type = types.str;
};
};
config = {
virtualisation.oci-containers.containers."${cfg.redis.name}" = {
image = "docker.io/valkey/valkey:9@sha256:4963247afc4cd33c7d3b2d2816b9f7f8eeebab148d29056c2ca4d7cbc966f2d9";
log-driver = "journald";
extraOptions = [
"--network=${common.network.name}"
"--health-cmd=redis-cli ping || exit 1"
];
};
systemd.services."${common.backend}-${cfg.redis.name}" = {
serviceConfig = {
Restart = lib.mkOverride 90 "always";
};
after = [ "${common.backend}-network-${common.network.name}.service" ];
requires = [ "${common.backend}-network-${common.network.name}.service" ];
partOf = [ "${common.backend}-${cfg.targetName}.target" ];
wantedBy = [ "${common.backend}-${cfg.targetName}.target" ];
};
};
}