190 lines
6.2 KiB
Nix
190 lines
6.2 KiB
Nix
{
|
|
domain,
|
|
...
|
|
}: args@{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}: let
|
|
cfg = config.hectic.affineDeployment;
|
|
affineDomain = "affine.${domain}";
|
|
databaseName = "affine-native";
|
|
environmentFile = "/var/lib/affine-native/environment";
|
|
postgresqlDataDirectory = "/var/lib/affine-postgresql";
|
|
postgresqlSocketDirectory = "/run/affine-postgresql";
|
|
|
|
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=";
|
|
};
|
|
});
|
|
affinePostgresql = pkgs.postgresql_16.withPackages (_: [ affinePgvector ]);
|
|
dockerConfig = (import ./affine-docker.nix { inherit domain; }) args;
|
|
in {
|
|
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 [
|
|
{
|
|
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;
|
|
'';
|
|
};
|
|
};
|
|
})
|
|
];
|
|
}
|