This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
{ system, inputs, self, pkgs, ... }:
|
||||
let
|
||||
lib = inputs.nixpkgs.lib;
|
||||
fakePkg = pkgs.runCommand "affine-test-package" {} ''
|
||||
mkdir -p "$out/bin"
|
||||
touch "$out/bin/affine-server" "$out/bin/affine-migrate"
|
||||
chmod +x "$out/bin/affine-server" "$out/bin/affine-migrate"
|
||||
'';
|
||||
environmentFile = "/run/secrets/affine-native.env";
|
||||
mkSystemFor = targetSystem: module:
|
||||
lib.nixosSystem {
|
||||
system = targetSystem;
|
||||
modules = [
|
||||
inputs.sops-nix.nixosModules.sops
|
||||
self.nixosModules.hectic
|
||||
{ system.stateVersion = "25.05"; }
|
||||
module
|
||||
];
|
||||
};
|
||||
mkSystem = mkSystemFor system;
|
||||
disabledCfg = (mkSystem {}).config;
|
||||
enabledCfg = (mkSystem {
|
||||
hectic.services.affine = {
|
||||
enable = true;
|
||||
package = fakePkg;
|
||||
inherit environmentFile;
|
||||
};
|
||||
}).config;
|
||||
app = enabledCfg.systemd.services.affine-native;
|
||||
migrate = enabledCfg.systemd.services.affine-native-migrate;
|
||||
defaultPackageCfg = (mkSystem {
|
||||
nixpkgs.config.allowUnfreePredicate = package:
|
||||
builtins.elem (lib.getName package) [ "affine" "affine-server-native" ];
|
||||
hectic.services.affine = {
|
||||
enable = true;
|
||||
inherit environmentFile;
|
||||
};
|
||||
}).config;
|
||||
unlicensedCfg = (mkSystem {
|
||||
nixpkgs.config = {
|
||||
allowUnfree = false;
|
||||
allowUnfreePredicate = _: false;
|
||||
};
|
||||
hectic.services.affine = {
|
||||
enable = true;
|
||||
inherit environmentFile;
|
||||
};
|
||||
}).config;
|
||||
unsupportedCfg = (mkSystemFor "aarch64-linux" {
|
||||
hectic.services.affine = {
|
||||
enable = true;
|
||||
package = fakePkg;
|
||||
inherit environmentFile;
|
||||
};
|
||||
}).config;
|
||||
in {
|
||||
affine-module-basic =
|
||||
assert !(disabledCfg.systemd.services ? affine-native);
|
||||
assert !(disabledCfg.systemd.services ? affine-native-migrate);
|
||||
assert !disabledCfg.virtualisation.docker.enable;
|
||||
assert !disabledCfg.services.postgresql.enable;
|
||||
assert disabledCfg.services.redis.servers == {};
|
||||
assert enabledCfg.users.users.affine-native.isSystemUser;
|
||||
assert enabledCfg.users.users.affine-native.group == "affine-native";
|
||||
assert app.serviceConfig.User == "affine-native";
|
||||
assert app.serviceConfig.ExecStart == "${fakePkg}/bin/affine-server";
|
||||
assert migrate.serviceConfig.ExecStart == "${fakePkg}/bin/affine-migrate";
|
||||
assert migrate.serviceConfig.Type == "oneshot";
|
||||
assert migrate.serviceConfig.RemainAfterExit;
|
||||
assert app.requires == [ "affine-native-migrate.service" ];
|
||||
assert lib.elem "affine-native-migrate.service" app.after;
|
||||
assert app.environment.HOME == "/var/lib/affine-native";
|
||||
assert app.environment.LISTEN_ADDR == "127.0.0.1";
|
||||
assert app.environment.AFFINE_SERVER_PORT == "3011";
|
||||
assert app.environment.REDIS_SERVER_HOST == "127.0.0.1";
|
||||
assert app.environment.REDIS_SERVER_PORT == "6388";
|
||||
assert enabledCfg.hectic.services.affine.externalUrl == "http://localhost:3011";
|
||||
assert app.serviceConfig.EnvironmentFile == environmentFile;
|
||||
assert app.serviceConfig.StateDirectory == "affine-native";
|
||||
assert app.serviceConfig.StateDirectoryMode == "0700";
|
||||
assert lib.hasInfix "/var/lib/affine-native/.affine/config" app.preStart;
|
||||
assert lib.hasInfix "/var/lib/affine-native/.affine/storage" app.preStart;
|
||||
assert !(enabledCfg.systemd.services ? affine);
|
||||
assert !enabledCfg.virtualisation.docker.enable;
|
||||
assert !enabledCfg.services.postgresql.enable;
|
||||
assert enabledCfg.services.redis.servers == {};
|
||||
assert defaultPackageCfg.hectic.services.affine.package.pname == "affine";
|
||||
assert builtins.isString defaultPackageCfg.hectic.services.affine.package.drvPath;
|
||||
# Nixpkgs explicitly lets NIXPKGS_ALLOW_UNFREE override its configuration.
|
||||
assert (builtins.tryEval unlicensedCfg.hectic.services.affine.package.drvPath).success ==
|
||||
(builtins.getEnv "NIXPKGS_ALLOW_UNFREE" == "1");
|
||||
assert lib.any (check:
|
||||
check.message == "hectic.services.affine currently supports only x86_64-linux." &&
|
||||
!check.assertion
|
||||
) unsupportedCfg.assertions;
|
||||
pkgs.runCommand "affine-module-basic" {} ''
|
||||
touch "$out"
|
||||
'';
|
||||
}
|
||||
@@ -33,7 +33,7 @@ let
|
||||
caseSensitive = false;
|
||||
};
|
||||
}).config;
|
||||
in {
|
||||
in (import ./affine.nix { inherit system inputs self pkgs; }) // {
|
||||
p4d-module-basic =
|
||||
assert enabledCfg.users.users.p4d.isSystemUser;
|
||||
assert enabledCfg.users.users.p4d.group == "p4d";
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
{ system, self, pkgs, ... }:
|
||||
pkgs.lib.optionalAttrs (system == "x86_64-linux") {
|
||||
affine-runtime = pkgs.runCommand "affine-runtime" {
|
||||
nativeBuildInputs = [
|
||||
pkgs.dash
|
||||
pkgs.curl
|
||||
pkgs.jq
|
||||
pkgs.nodejs_22
|
||||
(pkgs.postgresql_16.withPackages (ps: [ ps.pgvector ]))
|
||||
pkgs.redis
|
||||
];
|
||||
AFFINE_PACKAGE = self.packages.${system}.affine;
|
||||
AFFINE_CLIENT_TEST = ./test/client.cjs;
|
||||
} ''
|
||||
dash ${./test/runtime.sh}
|
||||
touch "$out"
|
||||
'';
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const { randomBytes } = require('node:crypto');
|
||||
|
||||
const base = 'http://127.0.0.1:13011';
|
||||
const password = randomBytes(18).toString('base64url');
|
||||
let cookies = '';
|
||||
|
||||
async function json(path, data, authenticated = true) {
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
if (authenticated && cookies) {
|
||||
headers.Cookie = cookies;
|
||||
const csrf = cookies.split('; ').find(cookie => cookie.startsWith('affine_csrf_token='));
|
||||
if (csrf) headers['x-affine-csrf-token'] = decodeURIComponent(csrf.slice(csrf.indexOf('=') + 1));
|
||||
}
|
||||
return fetch(`${base}${path}`, { method: 'POST', headers, body: JSON.stringify(data) });
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const setup = await json('/api/setup/create-admin-user', {
|
||||
name: 'Native test', email: 'native-test@example.invalid', password,
|
||||
}, false);
|
||||
assert.equal(setup.status, 201, 'First administrator setup');
|
||||
cookies = setup.headers.getSetCookie().map(cookie => cookie.split(';', 1)[0]).join('; ');
|
||||
assert(cookies.includes('affine_session='), 'Setup must issue a session');
|
||||
|
||||
const create = await json('/graphql', { query: 'mutation { createWorkspace { id } }' });
|
||||
assert.equal(create.status, 200, 'Workspace creation HTTP status');
|
||||
const created = await create.json();
|
||||
assert(!created.errors, 'Workspace creation must succeed');
|
||||
const id = created.data.createWorkspace.id;
|
||||
|
||||
const list = await json('/graphql', { query: 'query { workspaces { id } }' });
|
||||
const listed = await list.json();
|
||||
assert(!listed.errors, 'Workspace listing must succeed');
|
||||
assert(listed.data.workspaces.some(workspace => workspace.id === id), 'Created workspace must persist');
|
||||
|
||||
const anonymous = await json('/graphql', { query: 'query { workspaces { id } }' }, false);
|
||||
const anonymousData = await anonymous.json();
|
||||
assert(anonymous.status >= 400 || anonymousData.errors, 'Private workspaces must not be public');
|
||||
|
||||
const signup = await json('/api/auth/sign-in', { email: 'unknown@example.invalid' }, false);
|
||||
assert.equal(signup.status, 403, 'Public signup must remain disabled');
|
||||
const takeover = await json('/api/setup/create-admin-user', {}, false);
|
||||
assert.equal(takeover.status, 403, 'First administrator setup must close');
|
||||
|
||||
const login = await json('/api/auth/sign-in', { email: 'native-test@example.invalid', password }, false);
|
||||
assert.equal(login.status, 200, 'Existing administrator login');
|
||||
console.log('Administrator setup, authentication, private workspace CRUD and signup protection verified');
|
||||
}
|
||||
|
||||
main().catch(error => { console.error(error.message); process.exitCode = 1; });
|
||||
@@ -0,0 +1,106 @@
|
||||
#!/bin/dash
|
||||
set -eu
|
||||
|
||||
export HOME="$TMPDIR/affine-home"
|
||||
export DATABASE_URL="postgresql://affine@127.0.0.1:15432/affine"
|
||||
export REDIS_SERVER_HOST=127.0.0.1
|
||||
export REDIS_SERVER_PORT=16388
|
||||
export AFFINE_SERVER_PORT=13011
|
||||
export PORT=13011
|
||||
export LISTEN_ADDR=127.0.0.1
|
||||
export AFFINE_INDEXER_ENABLED=false
|
||||
export NODE_OPTIONS=--max-old-space-size=2048
|
||||
|
||||
mkdir -p "$HOME/.affine/config" "$HOME/.affine/storage" "$TMPDIR/pgsocket"
|
||||
cat > "$HOME/.affine/config/config.json" <<'JSON'
|
||||
{
|
||||
"server": {
|
||||
"name": "AFFiNE native test",
|
||||
"externalUrl": "http://127.0.0.1:13011",
|
||||
"listenAddr": "127.0.0.1",
|
||||
"port": 13011
|
||||
},
|
||||
"auth": { "allowSignup": false, "allowSignupForOauth": false },
|
||||
"copilot": { "enabled": false }
|
||||
}
|
||||
JSON
|
||||
|
||||
app_pid=
|
||||
redis_pid=
|
||||
cleanup() {
|
||||
if [ -n "$app_pid" ]; then kill "$app_pid" 2>/dev/null || true; fi
|
||||
if [ -n "$redis_pid" ]; then kill "$redis_pid" 2>/dev/null || true; fi
|
||||
pg_ctl -D "$TMPDIR/database" -m immediate stop >/dev/null 2>&1 || true
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
initdb -D "$TMPDIR/database" --username=postgres --auth=trust --no-locale --encoding=UTF8 > "$TMPDIR/initdb.log"
|
||||
pg_ctl -D "$TMPDIR/database" -l "$TMPDIR/postgresql.log" \
|
||||
-o "-h 127.0.0.1 -p 15432 -k $TMPDIR/pgsocket" start
|
||||
createuser --host=127.0.0.1 --port=15432 --username=postgres affine
|
||||
createdb --host=127.0.0.1 --port=15432 --username=postgres --owner=affine affine
|
||||
psql --host=127.0.0.1 --port=15432 --username=postgres --dbname=affine \
|
||||
--set=ON_ERROR_STOP=1 --command='CREATE EXTENSION vector;'
|
||||
redis-server --bind 127.0.0.1 --port 16388 --save '' --appendonly no \
|
||||
> "$TMPDIR/redis.log" 2>&1 &
|
||||
redis_pid=$!
|
||||
for _attempt in $(seq 1 30); do
|
||||
if redis-cli -h 127.0.0.1 -p 16388 ping >/dev/null 2>&1; then break; fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if ! "$AFFINE_PACKAGE/bin/affine-migrate" > "$TMPDIR/migrate.log" 2>&1; then
|
||||
cat "$TMPDIR/migrate.log"
|
||||
exit 1
|
||||
fi
|
||||
test -s "$HOME/.affine/config/private.key"
|
||||
test "$(stat -c %a "$HOME/.affine/config/private.key")" = 600
|
||||
cp "$HOME/.affine/config/private.key" "$TMPDIR/original-private.key"
|
||||
|
||||
"$AFFINE_PACKAGE/bin/affine-server" > "$TMPDIR/server.log" 2>&1 &
|
||||
app_pid=$!
|
||||
ready=false
|
||||
for _attempt in $(seq 1 90); do
|
||||
if curl --fail --silent http://127.0.0.1:13011/info > "$TMPDIR/info.json"; then
|
||||
ready=true
|
||||
break
|
||||
fi
|
||||
if ! kill -0 "$app_pid" 2>/dev/null; then break; fi
|
||||
sleep 1
|
||||
done
|
||||
if [ "$ready" != true ]; then
|
||||
cat "$TMPDIR/server.log"
|
||||
exit 1
|
||||
fi
|
||||
if ! jq -e '.type == "selfhosted" and .compatibility == "0.27.4"' "$TMPDIR/info.json"; then
|
||||
cat "$TMPDIR/info.json"
|
||||
exit 1
|
||||
fi
|
||||
curl --fail --silent http://127.0.0.1:13011/admin/setup > "$TMPDIR/setup.html"
|
||||
jq -R -s -e 'contains("<html")' "$TMPDIR/setup.html"
|
||||
|
||||
# Match every script/style reference against the final immutable installation.
|
||||
node - "$AFFINE_PACKAGE/share/affine" <<'JS'
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const app = process.argv[2];
|
||||
for (const subdir of ['', 'admin', 'mobile']) {
|
||||
const dir = path.join(app, 'static', subdir);
|
||||
const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'assets-manifest.json'), 'utf8'));
|
||||
for (const name of [...(manifest.js || []), ...(manifest.css || [])]) {
|
||||
if (!fs.statSync(path.join(dir, name)).isFile()) throw new Error(`Missing asset: ${subdir}/${name}`);
|
||||
}
|
||||
}
|
||||
const native = require(path.join(app, 'dist/server-native.node'));
|
||||
if (Object.keys(native).length < 1) throw new Error('Empty native module');
|
||||
console.log('Source-built native module and all three frontend manifests verified');
|
||||
JS
|
||||
|
||||
node "$AFFINE_CLIENT_TEST"
|
||||
|
||||
# Check idempotent predeploy against an already migrated, running installation.
|
||||
if ! "$AFFINE_PACKAGE/bin/affine-migrate" > "$TMPDIR/migrate-again.log" 2>&1; then
|
||||
cat "$TMPDIR/migrate-again.log"
|
||||
exit 1
|
||||
fi
|
||||
cmp "$TMPDIR/original-private.key" "$HOME/.affine/config/private.key"
|
||||
@@ -11,6 +11,7 @@ in
|
||||
(import ./db-tool { inherit system inputs self pkgs; }) //
|
||||
(import ./with-attic-cache { inherit system inputs self pkgs; }) //
|
||||
(import ./element-web { inherit system inputs self pkgs; }) //
|
||||
(import ./affine { inherit system inputs self pkgs; }) //
|
||||
(import ./linux-devshell { inherit system inputs self pkgs; }) //
|
||||
(import ./windows-devshell { inherit system inputs self pkgs; }) //
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user