5.6 KiB
Native AFFiNE NixOS module
hectic.services.affine runs the source-built AFFiNE backend without Docker.
It is opt-in and uses distinct affine-native and affine-native-migrate
units, so enabling or importing the module does not alter an existing
affine.service or Compose deployment.
Building and checking
The package targets x86_64-linux and pins AFFiNE 0.27.4. It builds the server,
Rust server-native binding, all three web/admin/mobile web bundles, and matching
Prisma 6.8.2 engines from source. Toolchains and dependency caches are fixed
inputs. No AFFiNE application binary or OCI image is used.
The backend license is marked unfree. For local development/testing, explicitly allow evaluation without changing machine-wide Nixpkgs configuration:
NIXPKGS_ALLOW_UNFREE=1 nix build --impure .#affine --option post-build-hook ''
NIXPKGS_ALLOW_UNFREE=1 nix build --impure .#checks.x86_64-linux.affine-runtime --option post-build-hook ''
nix build .#checks.x86_64-linux.affine-module-basic --option post-build-hook ''
Do not publish server/native artifacts to a shared binary cache without checking their redistribution terms. Allowing unfree evaluation does not grant production or redistribution rights. The runtime check uses a fresh PostgreSQL 16 database, pgvector, a non-superuser application role, and a private Redis instance. It tests both migration passes, server startup, assets, administrator setup, authentication, workspace creation, and disabled public signup.
Runtime contract
The selected package must provide:
bin/affine-server, the long-running backend;bin/affine-migrate, the full upstream predeploy sequence, including private key setup, Prisma schema migrations, and AFFiNE data migrations.
Both units run as the static affine-native system user. systemd manages
/var/lib/affine-native with mode 0700. HOME points there, so upstream uses
/var/lib/affine-native/.affine/config and
/var/lib/affine-native/.affine/storage.
The module writes an initial config.json only when none exists. It seeds the
external URL, server name, loopback listener, port, disabled public/OAuth
signup, and disabled copilot. Later admin-panel or manual edits are retained.
Use settings only to extend or override this initial seed.
Isolated local example
This example deliberately creates a new database, Redis instance, state path, and secret file. Do not point an initial native deployment at a production AFFiNE database.
{ config, lib, pkgs, ... }:
{
nixpkgs.config.allowUnfreePredicate = package:
builtins.elem (lib.getName package) [ "affine" "affine-server-native" ];
services.postgresql = {
enable = true;
package = pkgs.postgresql_16;
ensureDatabases = [ "affine-native" ];
ensureUsers = [{
name = "affine-native";
ensureDBOwnership = true;
}];
extensions = ps: [ ps.pgvector ];
};
services.redis.servers.affine-native = {
enable = true;
port = 6388;
bind = "127.0.0.1";
};
# DATABASE_URL: postgresql://affine-native@localhost/affine-native?host=/run/postgresql
sops.secrets.affine-native-database-url = {};
sops.templates."affine-native.env" = {
owner = "affine-native";
group = "affine-native";
mode = "0400";
content = ''
DATABASE_URL=${config.sops.placeholder.affine-native-database-url}
'';
};
hectic.services.affine = {
enable = true;
environmentFile = config.sops.templates."affine-native.env".path;
externalUrl = "http://localhost:3011";
redisHost = "127.0.0.1";
redisPort = 6388;
};
# This example runs both dependencies locally, so order migration after them.
systemd.services.affine-native-migrate = {
after = [ "postgresql.service" "redis-affine-native.service" ];
requires = [ "postgresql.service" "redis-affine-native.service" ];
};
# pgvector is not a trusted extension; provision it as postgres, not the app role.
systemd.services.affine-native-database = {
requires = [ "postgresql.service" ];
after = [ "postgresql.service" ];
before = [ "affine-native-migrate.service" ];
requiredBy = [ "affine-native-migrate.service" ];
serviceConfig = {
Type = "oneshot";
User = "postgres";
RemainAfterExit = true;
};
script = ''
${pkgs.postgresql_16}/bin/psql --dbname=affine-native --set=ON_ERROR_STOP=1 \
--command='CREATE EXTENSION IF NOT EXISTS vector;'
'';
};
}
The module never creates PostgreSQL, pgvector, Redis, reverse-proxy, or
firewall configuration. environmentFile is required and is passed to systemd
at runtime; put DATABASE_URL, Redis credentials, and other secrets there,
never in Nix module values or the Nix store. Non-secret Redis host and port
options map to upstream REDIS_SERVER_HOST and REDIS_SERVER_PORT.
Default binding is 127.0.0.1:3011. Perform initial administration through an
SSH tunnel, for example ssh -L 3011:127.0.0.1:3011 host, then browse to
http://localhost:3011. Add a reverse proxy separately only when remote access
is intended.
Upgrades and rollback
affine-native-migrate.service is required by and ordered before
affine-native.service. Package or initial-seed changes restart the migration
unit before the app, and the package wrapper owns the complete upstream
predeploy procedure.
Database schema and AFFiNE data migrations are not reversed by switching to an older NixOS generation. Back up the dedicated database and state directory before upgrades. Production migration remains a separate, explicit operation.
AFFiNE backend source has its own license; review it before production use. This module does not grant production authorization or alter upstream license terms.