253 lines
7.9 KiB
Nix
253 lines
7.9 KiB
Nix
{
|
|
domain,
|
|
...
|
|
}: {
|
|
pkgs,
|
|
...
|
|
}: 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}";
|
|
in {
|
|
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";
|
|
};
|
|
};
|
|
|
|
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;
|
|
'';
|
|
};
|
|
};
|
|
}
|