This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
{ ... }: {
|
||||
hectic.services.affine-chatgpt.enable = true;
|
||||
|
||||
systemd.services.affine-native = {
|
||||
wants = [ "affine-chatgpt.service" ];
|
||||
after = [ "affine-chatgpt.service" ];
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
{
|
||||
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;
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
+174
-274
@@ -1,289 +1,189 @@
|
||||
{
|
||||
domain,
|
||||
...
|
||||
}: {
|
||||
pkgs,
|
||||
}: args@{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}: let
|
||||
cfg = config.hectic.affineDeployment;
|
||||
affineDomain = "affine.${domain}";
|
||||
stateDirectory = "/var/lib/affine";
|
||||
environmentFile = "${stateDirectory}/.env";
|
||||
bootstrapDirectory = "/var/lib/affine-bootstrap";
|
||||
bootstrapPasswordFile = "${bootstrapDirectory}/password";
|
||||
authFile = "${bootstrapDirectory}/htpasswd";
|
||||
databaseName = "affine-native";
|
||||
environmentFile = "/var/lib/affine-native/environment";
|
||||
postgresqlDataDirectory = "/var/lib/affine-postgresql";
|
||||
postgresqlSocketDirectory = "/run/affine-postgresql";
|
||||
|
||||
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;
|
||||
affinePgvector = pkgs.postgresql_16.pkgs.pgvector.overrideAttrs (_: {
|
||||
version = "0.8.7";
|
||||
# v0.8.7 resolves to release commit f37c13f68b57d2c3472b2214fbcff699d6d34876.
|
||||
src = pkgs.fetchFromGitHub {
|
||||
owner = "pgvector";
|
||||
repo = "pgvector";
|
||||
rev = "v0.8.7";
|
||||
hash = "sha256-YYXz8zqiuqYWpJwImAhMry/QTLHKOP5MzuY0Of+aflc=";
|
||||
};
|
||||
});
|
||||
|
||||
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}";
|
||||
affinePostgresql = pkgs.postgresql_16.withPackages (_: [ affinePgvector ]);
|
||||
dockerConfig = (import ./affine-docker.nix { inherit domain; }) args;
|
||||
in {
|
||||
assertions = [
|
||||
options.hectic.affineDeployment.nativeActive = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Route production traffic to native AFFiNE and disable legacy Docker units.";
|
||||
};
|
||||
|
||||
config = lib.mkMerge [
|
||||
{
|
||||
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.";
|
||||
hectic.services.affine = {
|
||||
enable = true;
|
||||
frontendPackage = pkgs.hectic.affine-frontend-static;
|
||||
externalUrl = "https://${affineDomain}";
|
||||
serverName = "Hectic AFFiNE";
|
||||
address = "127.0.0.1";
|
||||
port = 3011;
|
||||
redisHost = "127.0.0.1";
|
||||
redisPort = 6388;
|
||||
inherit environmentFile;
|
||||
settings = {
|
||||
copilot.enabled = false;
|
||||
auth = {
|
||||
allowSignup = false;
|
||||
allowSignupForOauth = false;
|
||||
newAccountShareActionDelay = 0;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
users.groups.affine-postgres = {};
|
||||
users.users.affine-postgres = {
|
||||
isSystemUser = true;
|
||||
group = "affine-postgres";
|
||||
home = postgresqlDataDirectory;
|
||||
};
|
||||
|
||||
systemd.services.affine-postgresql = {
|
||||
description = "Isolated PostgreSQL cluster for AFFiNE";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "local-fs.target" ];
|
||||
unitConfig.RequiresMountsFor = postgresqlDataDirectory;
|
||||
path = [ affinePostgresql ];
|
||||
environment.POSTGRESQL_VERSION = affinePostgresql.version;
|
||||
restartTriggers = [ affinePgvector ];
|
||||
preStart = ''
|
||||
if [ ! -s ${postgresqlDataDirectory}/PG_VERSION ]; then
|
||||
initdb \
|
||||
--pgdata=${postgresqlDataDirectory} \
|
||||
--username=affine-postgres \
|
||||
--encoding=UTF8 \
|
||||
--locale=en_US.utf8 \
|
||||
--auth-local=peer \
|
||||
--auth-host=reject
|
||||
fi
|
||||
|
||||
cat > ${postgresqlDataDirectory}/pg_hba.conf <<'EOF'
|
||||
local all all peer
|
||||
host all all 0.0.0.0/0 reject
|
||||
host all all ::0/0 reject
|
||||
EOF
|
||||
'';
|
||||
postStart = ''
|
||||
ready=0
|
||||
for attempt in $(seq 1 60); do
|
||||
if pg_isready --host=${postgresqlSocketDirectory} --port=5433 --username=affine-postgres; then
|
||||
ready=1
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if [ "$ready" -ne 1 ]; then
|
||||
echo "AFFiNE PostgreSQL did not become ready" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
psql --host=${postgresqlSocketDirectory} --port=5433 --dbname=postgres \
|
||||
--set=ON_ERROR_STOP=1 <<'SQL'
|
||||
DO $role$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'affine-native') THEN
|
||||
CREATE ROLE "affine-native" LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE;
|
||||
END IF;
|
||||
END
|
||||
$role$;
|
||||
ALTER ROLE "affine-native" LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE;
|
||||
SELECT 'CREATE DATABASE "affine-native" OWNER "affine-native"'
|
||||
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'affine-native')\gexec
|
||||
ALTER DATABASE "affine-native" OWNER TO "affine-native";
|
||||
SQL
|
||||
|
||||
psql --host=${postgresqlSocketDirectory} --port=5433 --dbname=${databaseName} \
|
||||
--set=ON_ERROR_STOP=1 <<'SQL'
|
||||
CREATE EXTENSION IF NOT EXISTS vector;
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
SQL
|
||||
'';
|
||||
serviceConfig = {
|
||||
Type = "notify";
|
||||
User = "affine-postgres";
|
||||
Group = "affine-postgres";
|
||||
StateDirectory = "affine-postgresql";
|
||||
StateDirectoryMode = "0700";
|
||||
RuntimeDirectory = "affine-postgresql";
|
||||
RuntimeDirectoryMode = "0755";
|
||||
ExecStart = "${affinePostgresql}/bin/postgres -D ${postgresqlDataDirectory} -k ${postgresqlSocketDirectory} -p 5433 -c \"listen_addresses=\"";
|
||||
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
||||
KillSignal = "SIGINT";
|
||||
TimeoutStopSec = "120s";
|
||||
MemoryMax = "512M";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
};
|
||||
};
|
||||
|
||||
services.redis.servers.affine = {
|
||||
enable = true;
|
||||
bind = "127.0.0.1";
|
||||
port = 6388;
|
||||
settings = {
|
||||
maxmemory = "96mb";
|
||||
maxmemory-policy = "noeviction";
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services.redis-affine.serviceConfig.MemoryMax = "128M";
|
||||
|
||||
# Live data restore is intentionally external. Main stages config, private.key,
|
||||
# storage, and the database before creating this runtime environment file.
|
||||
systemd.services.affine-native-migrate = {
|
||||
after = [ "affine-postgresql.service" "redis-affine.service" ];
|
||||
requires = [ "affine-postgresql.service" "redis-affine.service" ];
|
||||
unitConfig.ConditionPathExists = environmentFile;
|
||||
};
|
||||
|
||||
systemd.services.affine-native = {
|
||||
unitConfig.ConditionPathExists = environmentFile;
|
||||
serviceConfig.MemoryMax = "2G";
|
||||
};
|
||||
}
|
||||
|
||||
(lib.mkIf (!cfg.nativeActive) dockerConfig)
|
||||
|
||||
(lib.mkIf cfg.nativeActive {
|
||||
services.nginx.virtualHosts.${affineDomain} = {
|
||||
enableACME = true;
|
||||
forceSSL = true;
|
||||
extraConfig = ''
|
||||
client_max_body_size 100m;
|
||||
'';
|
||||
locations."/" = {
|
||||
proxyPass = "http://127.0.0.1:3011";
|
||||
proxyWebsockets = true;
|
||||
extraConfig = ''
|
||||
proxy_read_timeout 3600s;
|
||||
proxy_send_timeout 3600s;
|
||||
'';
|
||||
};
|
||||
};
|
||||
})
|
||||
];
|
||||
|
||||
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;
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -27,7 +27,14 @@ in self.lib.nixpkgs-lib.nixosSystem {
|
||||
})
|
||||
];
|
||||
config.allowUnfreePredicate = pkg:
|
||||
builtins.elem (inputs.nixpkgs.lib.getName pkg) [ "steamcmd" "steam-unwrapped" "p4d" "p4" ];
|
||||
builtins.elem (inputs.nixpkgs.lib.getName pkg) [
|
||||
"affine"
|
||||
"affine-server-native"
|
||||
"steamcmd"
|
||||
"steam-unwrapped"
|
||||
"p4d"
|
||||
"p4"
|
||||
];
|
||||
};
|
||||
modules = [
|
||||
{ networking.hostName = name; }
|
||||
|
||||
@@ -63,6 +63,7 @@ in {
|
||||
inputs.iana-angl.nixosModules.iana-angl
|
||||
|
||||
(import ./affine.nix { inherit domain; })
|
||||
./affine-chatgpt.nix
|
||||
(import ./attic.nix { inherit flake self inputs domain; })
|
||||
(import ./containers.nix { inherit flake self inputs; })
|
||||
./experimental-sshd.nix
|
||||
@@ -93,6 +94,7 @@ in {
|
||||
security.acme.defaults.email = lib.mkForce "security@${domain}";
|
||||
|
||||
hectic = {
|
||||
affineDeployment.nativeActive = true;
|
||||
archetype.dev.enable = true;
|
||||
hardware.hetzner-cloud = {
|
||||
enable = true;
|
||||
|
||||
Reference in New Issue
Block a user