refactor immich

This commit is contained in:
Joakim Repomaa
2024-07-02 10:56:58 +03:00
parent cca6abb196
commit be9d398606
16 changed files with 417 additions and 61 deletions

View File

@@ -0,0 +1,60 @@
{ ... }:
let
fqdn = "graph.freun.dev";
port = 3300;
in
{
services.grafana = {
enable = true;
settings = {
server = {
root_url = "https://${fqdn}";
http_port = port;
};
database = {
host = "/var/run/postgresql";
type = "postgres";
user = "grafana";
};
smtp = {
enabled = true;
host = "horologium.uberspace.de";
from_address = "noreply@freun.dev";
from_name = "Vaultwarden";
user = "noreply@freun.dev";
password = "$__file{/var/secrets/smtp-password}";
};
};
};
services.prometheus = {
enable = true;
exporters.node.enable = true;
scrapeConfigs = [
{
job_name = "node";
static_configs = [
{ targets = [ "localhost:9100" ]; }
];
}
];
};
services.caddy.virtualHosts = {
"${fqdn}".extraConfig = ''
reverse_proxy localhost:${builtins.toString port}
'';
};
services.postgresql = {
ensureDatabases = [ "grafana" ];
ensureUsers = [{
name = "grafana";
ensureDBOwnership = true;
}];
};
}

View File

@@ -0,0 +1,16 @@
{ config, ... }:
let
fqdn = "trackmap.freun.dev";
in
{
services.gtrackmap = {
enable = true;
port = 3200;
};
services.caddy.virtualHosts = {
"${fqdn}".extraConfig = ''
reverse_proxy localhost:${toString config.services.gtrackmap.port}
'';
};
}

View File

@@ -0,0 +1,21 @@
{ ... }:
let
fqdn = "ci.freun.dev";
port = 3400;
in
{
services.hydra = {
enable = true;
hydraURL = "https://${fqdn}";
notificationSender = "Hydra <noreply@freun.dev>";
buildMachinesFiles = [];
useSubstitutes = true;
inherit port;
};
services.caddy.virtualHosts = {
"${fqdn}".extraConfig = ''
reverse_proxy localhost:${builtins.toString port}
'';
};
}

110
modules/services/immich.nix Normal file
View File

@@ -0,0 +1,110 @@
{ lib, config, ... }:
let
cfg = config.services.immich;
in
{
options.services.immich = with lib; {
enable = mkEnableOption "Enable immich";
fqdn = mkOption {
type = types.str;
description = "FQDN to use for the immich server";
};
data_dir = mkOption {
type = types.str;
description = "The directory to store immich data in";
};
secrets = mkOption {
type = types.str;
description = "Path to file with secrets";
};
version = mkOption {
type = types.str;
default = "release";
description = "The version (docker image tag) of immich to use";
};
mounts = mkOption {
type = types.listOf types.str;
description = "Additional mounts to add to the immich container";
default = [ ];
};
port = mkOption {
type = types.int;
default = 2283;
description = "Port to expose the immich server on";
};
};
imports = [
../util/container-services.nix
];
config = lib.mkIf cfg.enable rec {
container-services.immich = {
description = "Immich image server";
services = {
server = {
image = "ghcr.io/immich-app/immich-server:${cfg.version}";
environmentFiles = [
cfg.secrets
];
volumes = [
"${cfg.data_dir}:/usr/src/app/upload:rw"
"/etc/localtime:/etc/localtime:ro"
] ++ cfg.mounts;
ports = [ "${builtins.toString cfg.port}:3001/tcp" ];
dependsOn = [
container-services.immich.services.redis
container-services.immich.services.postgres
];
};
machine_learning = {
image = "ghcr.io/immich-app/immich-machine-learning:${cfg.version}";
environmentFiles = [
cfg.secrets
];
volumes = [
"model_cache:/cache:rw"
];
};
redis = {
image = "registry.hub.docker.com/library/redis:6.2-alpine";
healthCheck.test = "redis-cli ping || exit 1";
environmentFiles = [
cfg.secrets
];
};
postgres = {
image = "registry.hub.docker.com/tensorchord/pgvecto-rs:pg14-v0.2.0";
environmentFiles = [
cfg.secrets
];
environment = {
POSTGRES_INITDB_ARGS = "--data-checksums";
};
volumes = [
"db_data:/var/lib/postgresql/data:rw"
];
cmd = [ "postgres" "-c" "shared_preload_libraries=vectors.so" "-c" "search_path=\"$user\", public, vectors" "-c" "logging_collector=on" "-c" "max_wal_size=2GB" "-c" "shared_buffers=512MB" "-c" "wal_compression=on" ];
healthCheck = {
test = ''
pg_isready --dbname='$\{DB_DATABASE_NAME}' || exit 1
Chksum="$(psql --dbname='$\{DB_DATABASE_NAME}' --username='$\{DB_USERNAME}' --tuples-only --no-align --command='SELECT COALESCE(SUM(checksum_failures), 0) FROM pg_stat_database')"
echo "checksum failure count is $Chksum"
[ "$Chksum" = '0' ] || exit 1
'';
interval = "5m";
startInterval = "30s";
startPeriod = "5m";
};
};
};
};
services.caddy.virtualHosts = {
"${cfg.fqdn}".extraConfig = ''
reverse_proxy localhost:${builtins.toString cfg.port}
'';
};
};
}

View File

@@ -0,0 +1,25 @@
{ ... }:
let
fqdn = "vid.freun.dev";
in
{
services.invidious = {
enable = true;
domain = fqdn;
settings = {
external_port = 443;
db = {
dbname = "invidious";
user = "invidious";
};
};
};
services.caddy.virtualHosts = {
"${fqdn}".extraConfig = ''
reverse_proxy localhost:3000
'';
};
}

View File

@@ -0,0 +1,16 @@
{ ... }:
let
fqdn = "stream.freun.dev";
in
{
services.owncast = {
enable = true;
openFirewall = true;
};
services.caddy.virtualHosts = {
"${fqdn}".extraConfig = ''
reverse_proxy localhost:8080
'';
};
}

View File

@@ -0,0 +1,33 @@
{ pkgs, config, lib, ... }:
let
storage_dir = "/mnt/storage/syncthing";
in
{
services.syncthing = {
enable = true;
dataDir = "/mnt/storage/syncthing";
openDefaultPorts = true;
};
services.caddy.virtualHosts = {
"sync.freun.dev".extraConfig = ''
reverse_proxy localhost:8384 {
header_up Host {upstream_hostport}
}
'';
};
fileSystems."${storage_dir}" = {
device = "//u407959.your-storagebox.de/backup/syncthing";
fsType = "cifs";
options = let
# this line prevents hanging on network split
automount_opts = "x-systemd.automount,auto,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";
uid = builtins.toString config.users.users.syncthing.uid;
gid = builtins.toString config.users.groups.syncthing.gid;
in ["${automount_opts},credentials=/var/secrets/smb-storage,uid=${uid},gid=${gid}"];
};
environment.systemPackages = [ pkgs.cifs-utils ];
}

View File

@@ -0,0 +1,42 @@
{ ... }:
{
services.vaultwarden = {
enable = true;
dbBackend = "postgresql";
environmentFile = "/var/secrets/vaultwarden.env";
config = {
DOMAIN = "https://pw.freun.dev";
DATABASE_URL = "postgres://%2Fvar%2Frun%2Fpostgresql/vaultwarden";
WEBSOCKET_ENABLED = true;
WEBSOCKET_ADDRESS = "127.0.0.1";
WEBSOCKET_PORT = 3012;
SIGNUPS_VERIFY = true;
PASSWORD_ITERATIONS = 600000;
YUBICO_CLIENT_ID = 86799;
SMTP_HOST = "horologium.uberspace.de";
SMTP_FROM = "noreply@freun.dev";
SMTP_FROM_NAME = "Vaultwarden";
SMTP_USERNAME = "noreply@freun.dev";
SMTP_PORT = 587;
HELO_NAME = "freun.dev";
ROCKET_LIMITS = "{json=10485760}";
};
};
services.caddy.virtualHosts = {
"pw.freun.dev".extraConfig = ''
reverse_proxy /notifications/hub localhost:3012
reverse_proxy localhost:8000 {
header_up X-Real-IP {remote_host}
}
'';
};
services.postgresql = {
ensureDatabases = [ "vaultwarden" ];
ensureUsers = [{
name = "vaultwarden";
ensureDBOwnership = true;
}];
};
}

View File

@@ -0,0 +1,41 @@
{ config, pkgs, lib, ... }:
let
port = 51820;
name = "wg0";
peers = [
{
PublicKey = "XI0/k2j20CVSfevwjkmo4IddVoA2VY2fN6feauXYEXU=";
AllowedIPs = [ "10.100.0.2" ];
} # radish
];
address = [ "10.100.0.1/24" ];
in
{
networking.firewall.allowedUDPPorts = [ port ];
networking.useNetworkd = true;
systemd.network = {
enable = true;
netdevs.${name} = {
netdevConfig = {
Kind = "wireguard";
Name = "${name}";
MTUBytes = "1300";
};
wireguardConfig = {
PrivateKeyFile = "/var/secrets/wireguard-privkey";
ListenPort = port;
};
wireguardPeers = peers;
};
networks.${name} = {
matchConfig.Name = name;
inherit address;
networkConfig = {
IPMasquerade = "ipv4";
IPForward = true;
};
};
};
}

View File

@@ -0,0 +1,224 @@
{ pkgs, lib, config, ... }:
let
cfg = config.container-services;
healthcheck = with pkgs.lib; {
test = mkOption {
type = types.oneOf types.str (types.listOf types.str);
};
interval = mkOption {
type = types.str;
default = "30s";
};
startupInterval = mkOption {
type = types.str;
default = "30s";
};
startPeriod = mkOption {
type = types.str;
default = "0s";
};
};
service = with pkgs.lib; {
name = mkOption {
type = types.nilOr types.str;
default = nil;
};
alias = mkOption {
type = types.nilOr types.str;
default = nil;
};
dependsOn = mkOption {
type = types.listOf service;
default = [ ];
};
image = mkOption {
type = types.str;
};
volumes = mkOption {
type = types.listOf types.str;
default = [ ];
};
ports = mkOption {
type = types.listOf types.str;
default = [ ];
};
environment = mkOption {
type = types.attrsOf types.str;
default = { };
};
environmentFiles = mkOption {
type = types.listOf types.str;
default = [ ];
};
cmd = mkOption {
type = types.nilOr (types.listOf types.str);
default = nil;
};
healthCheck = mkOption {
type = types.nilOr (types.submodule healthcheck);
default = nil;
};
};
pod = with pkgs.lib; {
name = mkOption {
type = types.nilOr str;
default = nil;
};
description = mkOption {
type = types.nilOr str;
default = nil;
};
services = mkOption {
type = types.attrsOf (types.submodule service);
default = { };
};
};
backend = config.virtualisation.oci-containers.backend;
volumeName = pod: name: "${pod.name}-${name}";
volumeServiceName = pod: name: "${backend}-volume-${volumeName pod name}";
volumeServiceRef = pod: name: config.systemd.services."${backend}-volume-${volumeName pod name}".name + ".service";
serviceName = pod: service: "${backend}-${pod.name}-${service.name}";
serviceRef = pod: service: config.systemd.services."${backend}-${pod.name}-${service.name}".name + ".service";
networkName = pod: "${pod.name}-default";
networkServiceName = pod: "${backend}-network-${pod.name}";
networkServiceRef = pod: config.systemd.services."${backend}-network-${pod.name}".name + ".service";
podName = pod: "${backend}-pod-${pod.name}";
podRef = pod: config.systemd.targets."${backend}-pod-${pod.name}".name + ".target";
namedVolumes = service: lib.filter (volume: (! ((lib.hasPrefix "/" volume) || (lib.hasPrefix "./" volume)))) (lib.map (volume: lib.head (lib.splitString ":" volume)) service.volumes);
oneShotService = { pod, description, script }: {
path = [ (if backend == "podman" then pkgs.podman else pkgs.docker) ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
inherit description;
inherit script;
partOf = [ podRef pod ];
wantedBy = [ podRef pod ];
};
in
{
options = with pkgs.lib; {
container-services = mkOption {
type = types.attrsOf (types.submodule pod);
default = { };
};
};
config = {
container-services = lib.mapAttrs
(name: pod: {
name = pod.name or name;
services = lib.mapAttrs
(name: service: {
name = service.name or name;
})
pod.services;
})
cfg;
systemd.targets = lib.mapAttrs' (_: pod:
lib.nameValuePair (podName pod) {
wantedBy = [ "multi-user.target" ];
unitConfig = {
Description = pod.description or pod.name;
};
}
);
systemd.services = lib.foldl
(services: pod: lib.mergeAttrsList [
services
{
"${networkServiceName pod}" = oneShotService {
description = "Network for ${pod.name}";
script = ''
${backend} network inspect ${networkName pod} || ${backend} network create ${networkName pod}
'';
};
}
(lib.mapAttrs'
(_: service:
let
dependencies =
(lib.map (service: serviceRef pod.name service.name) service.dependsOn) ++
(lib.map (name: volumeServiceRef pod.name name) (namedVolumes service)) ++
(networkServiceRef pod);
in
lib.nameValuePair (serviceName pod service) {
serviceConfig = {
Restart = lib.mkOverride 500 "always";
};
after = dependencies;
requires = dependencies;
partOf = [ (podRef pod) ];
wantedBy = [ (podRef pod) ];
}
)
pod.services)
(lib.listToAttrs (lib.flatten (
lib.map
(service: (
lib.map
(volume: {
name = volumeServiceName pod volume;
value = oneShotService {
description = "Volume ${volume} of ${pod.name}";
script = ''
${backend} volume inspect ${volumeName pod volume} || ${backend} volume create ${volumeName pod volume}
'';
};
})
namedVolumes)
)
pod.services)))
])
{ }
cfg;
virtualisation.oci-containers.containers = lib.foldl
(containers: pod: containers // (
lib.mapAttrs' (_: service:
lib.nameValuePair (serviceName pod service) {
image = service.image;
volumes = service.volumes;
log-driver = "journal";
ports = service.ports;
environment = service.environment;
environmentFiles = service.environmentFiles;
cmd = service.cmd;
extraOptions = [
"--network-alias=${service.alias or service.name}"
"--network=${networkName pod}"
] ++ (if (service.healthCheck != null) then [
"--health-cmd=${
if (builtins.isList service.healthCheck.test)
then builtins.toJSON service.healthCheck.test
else service.healthCheck.test
}"
"--health-interval=${service.healthCheck.interval}"
"--health-startup-interval=${service.healthCheck.startupInterval}"
"--health-start-period=${service.healthCheck.startPeriod}"
] else [ ]);
}
)
))
{ }
cfg;
};
}