forked from wiltaylor/dotfiles-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
immich.nix
266 lines (222 loc) · 9.29 KB
/
immich.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Immich - Self-hosted photos and videos.
#
# Immich is not available as a NixOS module (yet). So deploy it as an OCI
# container instead. See https://github.com/NixOS/nixpkgs/pull/244803 for
# progress on packaging it natively for NixOS.
#
# This file adapted from Diogo Correia's dotfiles:
# https://github.com/diogotcorreia/dotfiles/blob/7676201683a3785ef17eff9f4ad3295375c670bd/hosts/hera/immich.nix
#
{config, lib, pkgs, ...}:
let
cfg = config.sys.server.immich;
in {
options.sys.server.immich = {
enable = lib.mkEnableOption "Immich";
domain = lib.mkOption {
type = lib.types.str;
description = "The domain to host Immich on";
};
port = lib.mkOption {
type = lib.types.int;
description = "The port to listen on";
};
metricsPortServer = lib.mkOption {
type = lib.types.int;
description = "The port that the server container should listen on for prometheus metrics";
};
metricsPortMicroservices = lib.mkOption {
type = lib.types.int;
description = "The port that the microservices container should listen on for prometheus metrics";
};
storagePath = lib.mkOption {
type = lib.types.path;
description = "The filepath at which persistent Immich files should be stored";
};
logLevel = lib.mkOption {
type = lib.types.enum [ "verbose" "debug" "log" "warn" "error" ];
default = "log";
description = "The log level to run Immich at";
};
};
config = lib.mkIf cfg.enable
(let
images = {
serverAndMicroservices = {
imageName = "ghcr.io/immich-app/immich-server";
imageDigest =
"sha256:d39cb7ecbcc9924f2c51a3e0deb8a469075996c6ba9e1384eb2ddb550984848e"; # v1.106.4
sha256 = "sha256-yt0/neyBIiyr+zpX9jB2UjsBHxLKyZSbnjSa32OchNA=";
};
machineLearning = {
imageName = "ghcr.io/immich-app/immich-machine-learning";
imageDigest =
"sha256:9db20e5c2033bef01fa2be50fa0a2c3d62e43f069aedde4d49a65e65a436d40b"; # v1.106.4
sha256 = "sha256-CB+do2YQ/HTYf40NxyM2WdpD0NAsqvCu+xG95hl5GUI=";
};
};
dbUsername = user;
redisName = "immich";
user = "immich";
group = user;
uid = 15015;
gid = 15015;
immichWebUrl = "http://immich_web:3000";
immichServerUrl = "http://immich_server:3001";
immichMachineLearningUrl = "http://immich_machine_learning:3003";
# Extract the major version of the currently in-use postgres.
postgresPackage = config.services.postgresql.package;
majorPostgresVersion = builtins.head (builtins.match "([0-9]+)\..+" postgresPackage.version);
# Full environment variable docs: https://immich.app/docs/install/environment-variables
environment = {
DB_URL = "socket://${dbUsername}:@/run/postgresql?db=${dbUsername}";
REDIS_SOCKET = config.services.redis.servers.${redisName}.unixSocket;
UPLOAD_LOCATION = cfg.storagePath;
IMMICH_WEB_URL = immichWebUrl;
IMMICH_SERVER_URL = immichServerUrl;
IMMICH_MACHINE_LEARNING_URL = immichMachineLearningUrl;
IMMICH_METRICS = "true";
LOG_LEVEL = cfg.logLevel;
};
# A function to build a container volume mount string that maps to the
# same place in the container as on the host.
mkMount = dir: "${dir}:${dir}";
in {
# Create a system user that Immich can run under, allowing for peer
# authentication to the postgres database.
users.users.${user} = {
inherit group uid;
isSystemUser = true;
};
users.groups.${group} = { inherit gid; };
# Create a postgres database for Immich, and install the pgvecto-rs plugin
# which we build separately as a custom package (see pkgs/default.nix).
services.postgresql = {
ensureUsers = [{
name = dbUsername;
ensureDBOwnership = true;
# Make the "immich" user a superuser such that it can create
# postgres extensions.
ensureClauses.superuser = true;
}];
ensureDatabases = [ dbUsername ];
extraPlugins = [
pkgs."postgresql${majorPostgresVersion}Packages".pgvecto-rs
];
settings = { shared_preload_libraries = "vectors.so"; };
};
# Create a redis server instance specifically for Immich.
services.redis.servers.${redisName} = {
inherit user;
enable = true;
};
# Ensure that the directory where photos will be stored exists.
# TODO: This appear to fail with the following error:
# fchownat() of /mnt/storagebox/media/immich failed: Permission denied
#
# systemd.tmpfiles.rules = [ "d ${cfg.storagePath} 0750 ${user} ${group}" ];
# Start the OCI containers necessary to run an Immich server.
# The containers are connected via a bridge network called "immich-bridge".
virtualisation.oci-containers.containers = {
immich_server = {
imageFile = pkgs.dockerTools.pullImage images.serverAndMicroservices;
image = "ghcr.io/immich-app/immich-server";
extraOptions =
[ "--network=immich-bridge" "--user=${toString uid}:${toString gid}" ];
volumes = [
"${cfg.storagePath}:/usr/src/app/upload"
(mkMount "/run/postgresql")
(mkMount "/run/redis-${redisName}")
];
environment = environment // {
PUID = toString uid;
PGID = toString gid;
};
ports = [ "${toString cfg.port}:3001" "${toString cfg.metricsPortServer}:8081" ];
autoStart = true;
};
immich_machine_learning = {
imageFile = pkgs.dockerTools.pullImage images.machineLearning;
image = "ghcr.io/immich-app/immich-machine-learning";
extraOptions = [ "--network=immich-bridge" ];
environment = environment;
volumes = [ "immich-model-cache:/cache" ];
autoStart = true;
};
};
systemd.services = {
init-immich-network = {
description = "Create the network bridge for immich.";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig.Type = "oneshot";
# We hardcode the docker command here (instead of using ${pkgs.docker}/bin/docker)
# in order to allow for compatibility with podman's docker wrapper.
script = ''
# Put a true at the end to prevent getting non-zero return code, which would
# otherwise cause the service to fail.
check=$(/run/current-system/sw/bin/docker network ls | grep "immich-bridge" || true)
if [ -z "$check" ];
then /run/current-system/sw/bin/docker network create immich-bridge
else echo "immich-bridge docker network already exists"
fi
'';
};
# TODO: Regardless of creating the extensions manually, Immich still
# complains that it can't create the extensions. So... I've just given
# it superuser for now above.
#
# enable-immich-postgresql-extensions = {
# description = "Activate required Postgres extensions for Immich";
# after = [ "network.target" ];
# wantedBy = [ "multi-user.target" ];
# serviceConfig = {
# # Run this as the postgres user so that we have super-user powers.
# User = "postgres";
# Group = "postgres";
# Type = "oneshot";
# };
# script = ''
# # Create extensions if they don't already exist.
# # psql will exit with an error code if the extension already exists, so append "|| true"
# # to have the operation always succeed.
# ${config.services.postgresql.package}/bin/psql ${dbUsername} -c "CREATE EXTENSION cube" || true
# ${config.services.postgresql.package}/bin/psql ${dbUsername} -c "CREATE EXTENSION earthdistance" || true
# '';
# };
};
# Configure the reverse proxy to route to this service.
services.nginx = {
enable = true;
virtualHosts.${cfg.domain} = {
http2 = true;
# Fetch and configure a TLS cert using the ACME protocol.
enableACME = true;
# Redirect all unencrypted traffic to HTTPS.
forceSSL = true;
locations = {
"/metrics/server" = {
# Proxy to the immich server container's metrics port.
# Note: We include a trailing slash in order to drop the path from
# the request.
proxyPass = "http://127.0.0.1:${toString cfg.metricsPortServer}/";
};
"/metrics/microservices" = {
# Proxy to the immich microservices container's metrics port.
# Note: We include a trailing slash in order to drop the path from
# the request.
proxyPass = "http://127.0.0.1:${toString cfg.metricsPortMicroservices}/";
};
"/" = {
# Proxy all other traffic straight through.
proxyPass = "http://127.0.0.1:${toString cfg.port}";
};
};
# Allow uploading media files up to 10 gigabytes in size.
extraConfig = ''
client_max_body_size 10G;
'';
};
};
});
}