Files
hearth/nixos/system/hectic-lab/affine.nix
T
yukkop e935abd0c3
runner nix smoke / nix label and flake smoke (push) Failing after 1m55s
affine fix styles
2026-10-29 21:43:33 +00:00

290 lines
9.4 KiB
Nix

{
domain,
...
}: {
pkgs,
lib,
...
}: let
affineDomain = "affine.${domain}";
stateDirectory = "/var/lib/affine";
environmentFile = "${stateDirectory}/.env";
bootstrapDirectory = "/var/lib/affine-bootstrap";
bootstrapPasswordFile = "${bootstrapDirectory}/password";
authFile = "${bootstrapDirectory}/htpasswd";
affineImage = "ghcr.io/toeverything/affine:0.27.4@sha256:c312116a134184947d98749c8a8e29f4e3bc97f4f2a51c9a3497f9baea2eec2b";
postgresImage = "pgvector/pgvector:pg16@sha256:a97d77306ff47cc1d8add4d000cec2207da2e110746318df3137ce53c337ca30";
redisImage = "redis:7-alpine@sha256:8be847644e195abc4c10c8d9936cfa45332eccc6b714fa7b186ff43f1f4d5579";
logging = {
driver = "json-file";
options = {
max-file = "3";
max-size = "10m";
};
};
commonEnvironment = {
AFFINE_INDEXER_ENABLED = "false";
DATABASE_URL = "postgresql://affine:\${DB_PASSWORD}@postgres:5432/affine";
REDIS_SERVER_HOST = "redis";
};
composeFile = (pkgs.formats.yaml {}).generate "affine-compose.yaml" {
services = {
affine = {
image = affineImage;
restart = "unless-stopped";
mem_limit = "2g";
inherit logging;
ports = [ "127.0.0.1:3010:3010" ];
volumes = [
"${stateDirectory}/data/storage:/root/.affine/storage"
"${stateDirectory}/data/config:/root/.affine/config"
];
environment = commonEnvironment;
depends_on = {
migration.condition = "service_completed_successfully";
postgres.condition = "service_healthy";
redis.condition = "service_healthy";
};
};
migration = {
image = affineImage;
command = [
"sh"
"-c"
"node ./scripts/self-host-predeploy.js"
];
mem_limit = "1g";
inherit logging;
volumes = [
"${stateDirectory}/data/storage:/root/.affine/storage"
"${stateDirectory}/data/config:/root/.affine/config"
];
environment = commonEnvironment;
depends_on = {
postgres.condition = "service_healthy";
redis.condition = "service_healthy";
};
};
postgres = {
image = postgresImage;
restart = "unless-stopped";
mem_limit = "512m";
inherit logging;
environment = {
POSTGRES_DB = "affine";
POSTGRES_PASSWORD = "\${POSTGRES_PASSWORD}";
POSTGRES_USER = "affine";
};
volumes = [ "${stateDirectory}/data/postgres:/var/lib/postgresql/data" ];
healthcheck = {
test = [ "CMD-SHELL" "pg_isready -U affine -d affine" ];
interval = "10s";
timeout = "5s";
retries = 5;
start_period = "10s";
};
};
redis = {
image = redisImage;
restart = "unless-stopped";
mem_limit = "128m";
inherit logging;
command = [
"redis-server"
"--save"
"60"
"1"
"--maxmemory"
"96mb"
"--maxmemory-policy"
"noeviction"
];
healthcheck = {
test = [ "CMD" "redis-cli" "ping" ];
interval = "10s";
timeout = "5s";
retries = 5;
};
};
};
};
configFile = pkgs.writeText "affine-config.json" (builtins.toJSON {
server = {
externalUrl = "https://${affineDomain}";
name = "Hectic AFFiNE";
};
copilot.enabled = false;
auth = {
allowSignup = false;
allowSignupForOauth = false;
newAccountShareActionDelay = 0;
};
});
compose = "${pkgs.docker-compose}/bin/docker-compose --project-name affine --env-file ${environmentFile} --file ${composeFile}";
frontendStatic = pkgs.hectic.affine-frontend-static;
frontendOverride = (pkgs.formats.yaml {}).generate "affine-frontend-compose.yaml" {
services.affine.volumes = [ "${frontendStatic}:/app/static:ro" ];
};
frontendCompose = "${compose} --file ${frontendOverride}";
in {
assertions = [
{
assertion = lib.hasPrefix "ghcr.io/toeverything/affine:${frontendStatic.version}@sha256:" affineImage;
message = "AFFiNE frontend assets and the deployed backend image must use the same release.";
}
{
assertion = frontendStatic.revision == "b4c8548c09da21b2898443559a5b846f0ccf5dd8";
message = "AFFiNE frontend source revision must match the pinned 0.27.4 backend.";
}
];
systemd.tmpfiles.rules = [
"d ${stateDirectory} 0750 root root -"
"d ${stateDirectory}/data 0750 root root -"
"d ${stateDirectory}/data/config 0750 root root -"
"d ${stateDirectory}/data/postgres 0750 999 999 -"
"d ${stateDirectory}/data/storage 0750 root root -"
"d ${bootstrapDirectory} 0750 root nginx -"
];
systemd.services.affine = {
description = "AFFiNE Docker Compose stack";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [
"docker.service"
"network-online.target"
"systemd-tmpfiles-setup.service"
];
requires = [
"docker.service"
"network-online.target"
];
restartIfChanged = true;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStartPre = pkgs.writeShellScript "affine-prepare" ''
set -eu
umask 077
if [ ! -s ${environmentFile} ]; then
environment_tmp="$(${pkgs.coreutils}/bin/mktemp ${stateDirectory}/.env.XXXXXX)"
trap '${pkgs.coreutils}/bin/rm -f "$environment_tmp"' EXIT
password="$(${pkgs.openssl}/bin/openssl rand -hex 32)"
${pkgs.coreutils}/bin/printf 'DB_PASSWORD=%s\nPOSTGRES_PASSWORD=%s\n' "$password" "$password" > "$environment_tmp"
${pkgs.coreutils}/bin/chmod 0600 "$environment_tmp"
${pkgs.coreutils}/bin/chown root:root "$environment_tmp"
${pkgs.coreutils}/bin/mv "$environment_tmp" ${environmentFile}
trap - EXIT
fi
if [ ! -e ${stateDirectory}/data/config/config.json ]; then
${pkgs.coreutils}/bin/install -m 0644 ${configFile} ${stateDirectory}/data/config/config.json
fi
${pkgs.coreutils}/bin/chmod 0600 ${environmentFile}
${pkgs.coreutils}/bin/chown root:root ${environmentFile}
'';
ExecStart = "${compose} up --detach --wait";
ExecStop = "${compose} down";
TimeoutStartSec = "15min";
TimeoutStopSec = "5min";
};
};
# Reconcile only the application container; leave the database and migration job alone.
systemd.services.affine-frontend = {
description = "Source-built AFFiNE frontend assets";
wantedBy = [ "multi-user.target" ];
requires = [ "affine.service" ];
after = [ "affine.service" ];
partOf = [ "affine.service" ];
reloadIfChanged = true;
restartIfChanged = false;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${frontendCompose} up --detach --no-deps --force-recreate affine";
ExecReload = "${frontendCompose} up --detach --no-deps --force-recreate affine";
ExecStop = "${compose} up --detach --no-deps --force-recreate affine";
TimeoutStartSec = "5min";
TimeoutStopSec = "5min";
};
};
systemd.services.affine-bootstrap = {
description = "Prepare AFFiNE pilot basic authentication";
requiredBy = [ "nginx.service" ];
before = [ "nginx.service" ];
after = [ "systemd-tmpfiles-setup.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script = ''
set -eu
umask 077
if [ ! -s ${bootstrapPasswordFile} ]; then
password_tmp="$(${pkgs.coreutils}/bin/mktemp ${bootstrapDirectory}/.password.XXXXXX)"
trap '${pkgs.coreutils}/bin/rm -f "$password_tmp"' EXIT
${pkgs.openssl}/bin/openssl rand -hex 32 > "$password_tmp"
${pkgs.coreutils}/bin/chmod 0600 "$password_tmp"
${pkgs.coreutils}/bin/chown root:root "$password_tmp"
${pkgs.coreutils}/bin/mv "$password_tmp" ${bootstrapPasswordFile}
trap - EXIT
fi
if [ ! -s ${authFile} ]; then
auth_tmp="$(${pkgs.coreutils}/bin/mktemp ${bootstrapDirectory}/.htpasswd.XXXXXX)"
trap '${pkgs.coreutils}/bin/rm -f "$auth_tmp"' EXIT
hash="$(${pkgs.openssl}/bin/openssl passwd -6 -stdin < ${bootstrapPasswordFile})"
${pkgs.coreutils}/bin/printf 'affine:%s\n' "$hash" > "$auth_tmp"
${pkgs.coreutils}/bin/chmod 0640 "$auth_tmp"
${pkgs.coreutils}/bin/chown root:nginx "$auth_tmp"
${pkgs.coreutils}/bin/mv "$auth_tmp" ${authFile}
trap - EXIT
fi
${pkgs.coreutils}/bin/chmod 0600 ${bootstrapPasswordFile}
${pkgs.coreutils}/bin/chown root:root ${bootstrapPasswordFile}
${pkgs.coreutils}/bin/chmod 0640 ${authFile}
${pkgs.coreutils}/bin/chown root:nginx ${authFile}
'';
};
services.nginx.virtualHosts.${affineDomain} = {
enableACME = true;
forceSSL = true;
# Create this marker only after configuring the administrator and signup policy.
extraConfig = ''
client_max_body_size 100m;
set $affine_bootstrap_realm "AFFiNE setup";
if (-f ${bootstrapDirectory}/initialized) {
set $affine_bootstrap_realm off;
}
'';
locations."/" = {
proxyPass = "http://127.0.0.1:3010";
proxyWebsockets = true;
extraConfig = ''
auth_basic $affine_bootstrap_realm;
auth_basic_user_file ${authFile};
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
'';
};
};
}