324 lines
11 KiB
Nix
324 lines
11 KiB
Nix
{
|
|
lib,
|
|
stdenv,
|
|
stdenvNoCC,
|
|
fetchzip,
|
|
runCommand,
|
|
config,
|
|
makeWrapper,
|
|
nodejs,
|
|
git,
|
|
cacert,
|
|
yarn-berry_4,
|
|
pkg-config,
|
|
openssl,
|
|
inputs,
|
|
system,
|
|
frontendPatches ? [ ./gitea-document-typography.patch ./byok-model-picker.patch ./self-hosted-ai-usage.patch ],
|
|
}:
|
|
|
|
let
|
|
version = "0.27.4";
|
|
revision = "b4c8548c09da21b2898443559a5b846f0ccf5dd8";
|
|
|
|
src = fetchzip {
|
|
url = "https://github.com/toeverything/AFFiNE/archive/${revision}.tar.gz";
|
|
hash = "sha256-MuzQkiiMUrmFVzHCA5l5hzXm7wA7ChPaKzcrJtc1R3Y=";
|
|
};
|
|
|
|
affinePkgs = import inputs.nixpkgs {
|
|
inherit system config;
|
|
overlays = [ (import inputs.affine-rust-overlay) ];
|
|
};
|
|
rustToolchain = affinePkgs.rust-bin.stable."1.97.1".default;
|
|
rustPlatform = affinePkgs.makeRustPlatform {
|
|
cargo = rustToolchain;
|
|
rustc = rustToolchain;
|
|
};
|
|
|
|
prismaEngines = affinePkgs.callPackage ./prisma-engines.nix {};
|
|
|
|
prismaSchemaEngine = "${prismaEngines}/bin/schema-engine";
|
|
prismaQueryEngine = "${prismaEngines}/lib/libquery_engine.node";
|
|
|
|
native = rustPlatform.buildRustPackage {
|
|
pname = "affine-server-native";
|
|
inherit version src;
|
|
|
|
cargoHash = "sha256-fQY4DmkbZQljXQzWRNLzaYxdPoenWTbOtjBvqT/HFYE=";
|
|
cargoBuildFlags = [
|
|
"-p"
|
|
"affine_server_native"
|
|
];
|
|
nativeBuildInputs = [ pkg-config ];
|
|
buildInputs = [ openssl ];
|
|
|
|
env = {
|
|
CARGO_BUILD_JOBS = "8";
|
|
OPENSSL_NO_VENDOR = "1";
|
|
};
|
|
|
|
doCheck = false;
|
|
meta = {
|
|
license = lib.licenses.unfree;
|
|
platforms = [ "x86_64-linux" ];
|
|
};
|
|
|
|
installPhase = ''
|
|
runHook preInstall
|
|
install -Dm755 target/${stdenv.hostPlatform.rust.cargoShortTarget}/release/libaffine_server_native.so \
|
|
"$out/lib/server-native.node"
|
|
runHook postInstall
|
|
'';
|
|
};
|
|
|
|
yarnOfflineCache = yarn-berry_4.fetchYarnBerryDeps {
|
|
inherit src;
|
|
missingHashes = ./missing-hashes.json;
|
|
hash = "sha256-3dM2b5wR2mYPn8JMBXXXJAHzlHjbLWkRdrp9uLjjV30=";
|
|
};
|
|
|
|
yarnCmd = "${nodejs}/bin/node .yarn/releases/yarn-4.18.0.cjs";
|
|
# Populate Git package archives without a second node_modules tree.
|
|
yarnOfflineCache18 = stdenvNoCC.mkDerivation {
|
|
pname = "affine-yarn-cache";
|
|
inherit version src;
|
|
|
|
nativeBuildInputs = [ nodejs git cacert ];
|
|
|
|
dontConfigure = true;
|
|
dontInstall = true;
|
|
|
|
buildPhase = ''
|
|
runHook preBuild
|
|
|
|
export HOME=$(mktemp -d)
|
|
export YARN_ENABLE_GLOBAL_CACHE=false
|
|
export YARN_ENABLE_SCRIPTS=false
|
|
export YARN_NODE_LINKER=pnp
|
|
export YARN_CACHE_FOLDER=$PWD/.yarn/cache
|
|
rm -rf .yarn/cache
|
|
mkdir -p .yarn/cache
|
|
cp -R ${yarnOfflineCache}/cache/. .yarn/cache/
|
|
chmod -R u+w .yarn/cache
|
|
${yarnCmd} install --immutable --mode=skip-build
|
|
cmp yarn.lock ${src}/yarn.lock
|
|
rm -rf node_modules .yarn/install-state.gz
|
|
mkdir -p "$out"
|
|
cp -R .yarn/cache "$out/cache"
|
|
|
|
runHook postBuild
|
|
'';
|
|
|
|
outputHashMode = "recursive";
|
|
outputHash = "sha256-6rE9uAZalXQY//FUYBD0f2474UnRlWm9n9KzfQj+D6s=";
|
|
};
|
|
|
|
yarnConfigurePhase = ''
|
|
runHook preConfigure
|
|
|
|
export HOME=$(mktemp -d)
|
|
export YARN_ENABLE_GLOBAL_CACHE=false
|
|
export YARN_ENABLE_NETWORK=false
|
|
export YARN_ENABLE_TELEMETRY=false
|
|
export YARN_CACHE_FOLDER=$PWD/.yarn/cache
|
|
rm -rf .yarn/cache .yarn/checkouts
|
|
mkdir -p .yarn/cache
|
|
cp -R ${yarnOfflineCache18}/cache/. .yarn/cache/
|
|
chmod -R u+w .yarn
|
|
${yarnCmd} install --immutable --mode=skip-build --inline-builds
|
|
patchShebangs node_modules
|
|
patchShebangs tools/cli/bin
|
|
mkdir -p .nix-bin
|
|
makeWrapper ${nodejs}/bin/node "$PWD/.nix-bin/yarn" \
|
|
--add-flags "$PWD/.yarn/releases/yarn-4.18.0.cjs"
|
|
export PATH="$PWD/.nix-bin:$PWD/node_modules/.bin:$PATH"
|
|
# Match the version stamping performed by upstream release CI.
|
|
${yarnCmd} workspaces list --json > .nix-workspaces.json
|
|
${nodejs}/bin/node - .nix-workspaces.json ${version} <<'JS'
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
for (const line of fs.readFileSync(process.argv[2], 'utf8').trim().split('\n')) {
|
|
const { location } = JSON.parse(line);
|
|
const file = path.join(location, 'package.json');
|
|
const manifest = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
manifest.version = process.argv[3];
|
|
fs.writeFileSync(file, JSON.stringify(manifest, null, 2) + '\n');
|
|
}
|
|
JS
|
|
${yarnCmd} affine init
|
|
|
|
runHook postConfigure
|
|
'';
|
|
|
|
frontend = stdenv.mkDerivation ({
|
|
pname = "affine-frontend";
|
|
inherit version src yarnOfflineCache;
|
|
missingHashes = ./missing-hashes.json;
|
|
patches = frontendPatches;
|
|
|
|
nativeBuildInputs = [
|
|
nodejs
|
|
makeWrapper
|
|
];
|
|
|
|
configurePhase = yarnConfigurePhase;
|
|
|
|
env = {
|
|
BUILD_TYPE = "stable";
|
|
GITHUB_SHA = revision;
|
|
NODE_OPTIONS = "--max-old-space-size=8192";
|
|
YARN_ENABLE_SCRIPTS = "0";
|
|
};
|
|
|
|
buildPhase = ''
|
|
runHook preBuild
|
|
${yarnCmd} workspace @affine/templates build
|
|
${yarnCmd} affine @affine/web build
|
|
${yarnCmd} affine @affine/admin build
|
|
${yarnCmd} affine @affine/mobile build
|
|
runHook postBuild
|
|
'';
|
|
|
|
installPhase = ''
|
|
runHook preInstall
|
|
mkdir -p "$out"
|
|
cp -R packages/frontend/apps/web/dist "$out/web"
|
|
cp -R packages/frontend/admin/dist "$out/admin"
|
|
cp -R packages/frontend/apps/mobile/dist "$out/mobile"
|
|
runHook postInstall
|
|
'';
|
|
} // lib.optionalAttrs (builtins.elem ./byok-model-picker.patch frontendPatches || builtins.elem ./self-hosted-ai-usage.patch frontendPatches) {
|
|
doCheck = true;
|
|
checkPhase = ''
|
|
runHook preCheck
|
|
${lib.optionalString (builtins.elem ./byok-model-picker.patch frontendPatches) ''AFFINE_SOURCE_DIR="$PWD" ${nodejs}/bin/node ${../../test/package/affine/test/model-picker.cjs}''}
|
|
${lib.optionalString (builtins.elem ./self-hosted-ai-usage.patch frontendPatches) ''AFFINE_SOURCE_DIR="$PWD" ${nodejs}/bin/node ${../../test/package/affine/test/ai-usage.cjs}''}
|
|
runHook postCheck
|
|
'';
|
|
});
|
|
|
|
frontendStatic = runCommand "affine-frontend-static-${version}" {
|
|
inherit version;
|
|
passthru = { inherit revision; };
|
|
meta = {
|
|
description = "Source-built AFFiNE web assets with typography and BYOK model selection";
|
|
license = [ lib.licenses.mit lib.licenses.mpl20 ];
|
|
platforms = [ "x86_64-linux" ];
|
|
};
|
|
} ''
|
|
mkdir -p "$out/admin" "$out/mobile" "$out/licenses"
|
|
cp -RL ${frontend}/web/. "$out/"
|
|
cp -RL ${frontend}/admin/. "$out/admin/"
|
|
cp -RL ${frontend}/mobile/. "$out/mobile/"
|
|
cp ${src}/LICENSE ${src}/LICENSE-MIT "$out/licenses/"
|
|
cp ${src}/packages/backend/server/LICENSE "$out/licenses/AFFINE-EE-and-MPL"
|
|
'';
|
|
in
|
|
stdenv.mkDerivation (finalAttrs: {
|
|
pname = "affine";
|
|
inherit version src yarnOfflineCache;
|
|
missingHashes = ./missing-hashes.json;
|
|
patches = [ ./native-loader.patch ./read-only-schema.patch ];
|
|
|
|
nativeBuildInputs = [
|
|
makeWrapper
|
|
nodejs
|
|
];
|
|
|
|
configurePhase = yarnConfigurePhase;
|
|
|
|
env = {
|
|
NODE_OPTIONS = "--max-old-space-size=8192";
|
|
YARN_ENABLE_SCRIPTS = "0";
|
|
PRISMA_SCHEMA_ENGINE_BINARY = prismaSchemaEngine;
|
|
PRISMA_QUERY_ENGINE_LIBRARY = prismaQueryEngine;
|
|
};
|
|
|
|
postPatch = ''
|
|
substituteInPlace packages/backend/server/schema.prisma \
|
|
--replace-fail '["native", "debian-openssl-3.0.x", "linux-arm64-openssl-3.0.x"]' '["native"]'
|
|
'';
|
|
|
|
buildPhase = ''
|
|
runHook preBuild
|
|
|
|
cp ${native}/lib/server-native.node packages/backend/native/server-native.node
|
|
${yarnCmd} affine @affine/server build
|
|
${yarnCmd} workspaces focus @affine/server --production
|
|
${yarnCmd} workspace @affine/server prisma generate
|
|
|
|
runHook postBuild
|
|
'';
|
|
|
|
installPhase = ''
|
|
runHook preInstall
|
|
|
|
app="$out/share/affine"
|
|
mkdir -p "$app" "$out/bin"
|
|
cp -R packages/backend/server/dist "$app/dist"
|
|
cp packages/backend/server/package.json packages/backend/server/schema.prisma "$app/"
|
|
cp -R packages/backend/server/migrations "$app/migrations"
|
|
mkdir -p "$app/scripts"
|
|
cp packages/backend/server/scripts/self-host-predeploy.js "$app/scripts/"
|
|
substituteInPlace "$app/scripts/self-host-predeploy.js" \
|
|
--replace-fail 'yarn prisma' '${nodejs}/bin/node '"$app/node_modules/prisma/build/index.js" \
|
|
--replace-fail 'yarn cli run' 'SERVER_FLAVOR=script ${nodejs}/bin/node '"$app/dist/main.js run" \
|
|
--replace-fail 'fs.mkdirSync(SELF_HOST_CONFIG_DIR, { recursive: true });' \
|
|
'fs.mkdirSync(SELF_HOST_CONFIG_DIR, { recursive: true, mode: 0o700 });' \
|
|
--replace-fail "fs.writeFileSync(targetFilePath, generator(), 'utf-8');" \
|
|
"fs.writeFileSync(targetFilePath, generator(), { encoding: 'utf-8', mode: 0o600 });"
|
|
cp -R node_modules "$app/node_modules"
|
|
mkdir -p "$app/packages/common" "$app/packages/backend/server"
|
|
cp -R packages/common/s3-compat "$app/packages/common/"
|
|
cp -R packages/backend/native "$app/packages/backend/"
|
|
cp packages/backend/server/package.json "$app/packages/backend/server/"
|
|
ln -s ../../../dist "$app/packages/backend/server/dist"
|
|
ln -s ../../../schema.prisma "$app/packages/backend/server/schema.prisma"
|
|
ln -s ../../../migrations "$app/packages/backend/server/migrations"
|
|
mkdir -p "$out/share/doc/affine"
|
|
cp LICENSE LICENSE-MIT "$out/share/doc/affine/"
|
|
cp packages/backend/server/LICENSE "$out/share/doc/affine/LICENSE-backend"
|
|
mkdir -p "$app/static/admin" "$app/static/mobile"
|
|
cp -R ${frontend}/web/. "$app/static/"
|
|
cp -R ${frontend}/admin/. "$app/static/admin/"
|
|
cp -R ${frontend}/mobile/. "$app/static/mobile/"
|
|
|
|
makeWrapper ${nodejs}/bin/node "$out/bin/affine-server" \
|
|
--run 'umask 077' \
|
|
--add-flags "$app/dist/main.js" \
|
|
--set-default NODE_ENV production \
|
|
--set-default AFFINE_ENV production \
|
|
--set-default DEPLOYMENT_TYPE selfhosted \
|
|
--set-default SERVER_FLAVOR allinone \
|
|
--set-default PRISMA_QUERY_ENGINE_LIBRARY ${prismaQueryEngine} \
|
|
--set-default HOME /var/lib/affine-native
|
|
|
|
makeWrapper ${nodejs}/bin/node "$out/bin/affine-migrate" \
|
|
--run 'umask 077' \
|
|
--add-flags "$app/scripts/self-host-predeploy.js" \
|
|
--chdir "$app" \
|
|
--prefix PATH : "${lib.makeBinPath [ nodejs ]}" \
|
|
--set-default NODE_ENV production \
|
|
--set-default AFFINE_ENV production \
|
|
--set-default DEPLOYMENT_TYPE selfhosted \
|
|
--set-default PRISMA_SCHEMA_ENGINE_BINARY ${prismaSchemaEngine} \
|
|
--set-default PRISMA_QUERY_ENGINE_LIBRARY ${prismaQueryEngine} \
|
|
--set-default HOME /var/lib/affine-native
|
|
|
|
runHook postInstall
|
|
'';
|
|
|
|
passthru = {
|
|
inherit frontend frontendStatic native prismaEngines prismaQueryEngine prismaSchemaEngine yarnOfflineCache18;
|
|
};
|
|
|
|
meta = {
|
|
description = "Local-first collaborative knowledge workspace";
|
|
homepage = "https://affine.pro/";
|
|
license = lib.licenses.unfree;
|
|
platforms = [ "x86_64-linux" ];
|
|
mainProgram = "affine-server";
|
|
};
|
|
})
|