216 lines
7.0 KiB
Nix
216 lines
7.0 KiB
Nix
{
|
|
inputs,
|
|
flake,
|
|
...
|
|
}:
|
|
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}: let
|
|
cfg = config.hectic.services.affine;
|
|
stateDirectory = "/var/lib/affine-native";
|
|
configDirectory = "${stateDirectory}/.affine/config";
|
|
storageDirectory = "${stateDirectory}/.affine/storage";
|
|
initialConfig = pkgs.writeText "affine-native-config.json" (builtins.toJSON (
|
|
lib.recursiveUpdate {
|
|
server = {
|
|
externalUrl = cfg.externalUrl;
|
|
host = "localhost";
|
|
listenAddr = cfg.address;
|
|
name = cfg.serverName;
|
|
port = cfg.port;
|
|
};
|
|
auth = {
|
|
allowSignup = false;
|
|
allowSignupForOauth = false;
|
|
};
|
|
copilot.enabled = false;
|
|
} cfg.settings
|
|
));
|
|
prepareState = ''
|
|
install -d -m 0700 ${configDirectory} ${storageDirectory}
|
|
if [ ! -e ${configDirectory}/config.json ]; then
|
|
install -m 0600 ${initialConfig} ${configDirectory}/config.json
|
|
fi
|
|
'';
|
|
commonEnvironment = {
|
|
HOME = stateDirectory;
|
|
PORT = toString cfg.port;
|
|
AFFINE_SERVER_PORT = toString cfg.port;
|
|
LISTEN_ADDR = cfg.address;
|
|
REDIS_SERVER_HOST = cfg.redisHost;
|
|
REDIS_SERVER_PORT = toString cfg.redisPort;
|
|
};
|
|
commonServiceConfig = {
|
|
User = "affine-native";
|
|
Group = "affine-native";
|
|
StateDirectory = "affine-native";
|
|
StateDirectoryMode = "0700";
|
|
EnvironmentFile = cfg.environmentFile;
|
|
UMask = "0077";
|
|
NoNewPrivileges = true;
|
|
PrivateTmp = true;
|
|
ProtectHome = true;
|
|
ProtectSystem = "strict";
|
|
ReadWritePaths = [ stateDirectory ];
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
|
|
};
|
|
in {
|
|
options.hectic.services.affine = {
|
|
enable = lib.mkEnableOption "native AFFiNE server";
|
|
|
|
package = lib.mkOption {
|
|
type = lib.types.package;
|
|
default = pkgs.callPackage (flake + "/package/affine") {
|
|
inherit inputs;
|
|
system = pkgs.stdenv.hostPlatform.system;
|
|
nodejs = pkgs.nodejs_22;
|
|
# Baseline bundle intentionally excludes independently deployed picker patches.
|
|
frontendPatches = [ (flake + "/package/affine/gitea-document-typography.patch") ];
|
|
};
|
|
defaultText = lib.literalExpression "pkgs.callPackage ./package/affine { ...; }";
|
|
description = ''
|
|
AFFiNE package providing affine-server and affine-migrate. The default
|
|
source-built package is available only on x86_64-linux and has an unfree
|
|
backend license. Explicitly allow both "affine" and
|
|
"affine-server-native" through nixpkgs.config.allowUnfreePredicate, or
|
|
provide a package whose licensing policy you have configured separately.
|
|
'';
|
|
};
|
|
|
|
frontendPackage = lib.mkOption {
|
|
type = lib.types.nullOr lib.types.package;
|
|
default = null;
|
|
description = ''
|
|
Optional source-built AFFiNE web, admin, and mobile static assets to
|
|
bind over the native server's bundled frontend.
|
|
'';
|
|
};
|
|
|
|
environmentFile = lib.mkOption {
|
|
type = lib.types.path;
|
|
description = ''
|
|
Runtime environment file containing DATABASE_URL and any secret AFFiNE
|
|
or Redis variables. Keep this file outside the Nix store.
|
|
'';
|
|
};
|
|
|
|
address = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "127.0.0.1";
|
|
description = "Address on which AFFiNE listens.";
|
|
};
|
|
|
|
port = lib.mkOption {
|
|
type = lib.types.port;
|
|
default = 3011;
|
|
description = "Port on which AFFiNE listens.";
|
|
};
|
|
|
|
externalUrl = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "http://localhost:${toString cfg.port}";
|
|
description = "External URL written to the initial AFFiNE configuration.";
|
|
};
|
|
|
|
serverName = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "AFFiNE";
|
|
description = "Server name written to the initial AFFiNE configuration.";
|
|
};
|
|
|
|
redisHost = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "127.0.0.1";
|
|
description = "External Redis host exported as REDIS_SERVER_HOST.";
|
|
};
|
|
|
|
redisPort = lib.mkOption {
|
|
type = lib.types.port;
|
|
default = 6388;
|
|
description = "External Redis port exported as REDIS_SERVER_PORT.";
|
|
};
|
|
|
|
settings = lib.mkOption {
|
|
type = (pkgs.formats.json {}).type;
|
|
default = {};
|
|
description = ''
|
|
Extra values recursively merged into the initial AFFiNE config.json.
|
|
This seed is written only when config.json does not already exist.
|
|
'';
|
|
};
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
assertions = [
|
|
{
|
|
assertion = pkgs.stdenv.hostPlatform.system == "x86_64-linux";
|
|
message = "hectic.services.affine currently supports only x86_64-linux.";
|
|
}
|
|
{
|
|
assertion = cfg.frontendPackage == null || (
|
|
cfg.package ? version &&
|
|
cfg.frontendPackage ? version &&
|
|
cfg.frontendPackage.version == cfg.package.version
|
|
);
|
|
message = "hectic.services.affine.frontendPackage version must match hectic.services.affine.package version.";
|
|
}
|
|
{
|
|
assertion = cfg.frontendPackage == null || (
|
|
cfg.package ? frontendStatic &&
|
|
cfg.package.frontendStatic ? revision &&
|
|
builtins.isString cfg.package.frontendStatic.revision &&
|
|
cfg.package.frontendStatic.revision != "" &&
|
|
cfg.frontendPackage ? revision &&
|
|
builtins.isString cfg.frontendPackage.revision &&
|
|
cfg.frontendPackage.revision != "" &&
|
|
cfg.frontendPackage.revision == cfg.package.frontendStatic.revision
|
|
);
|
|
message = "hectic.services.affine.frontendPackage revision must be a non-empty string matching package.frontendStatic.revision.";
|
|
}
|
|
];
|
|
|
|
users.groups.affine-native = {};
|
|
users.users.affine-native = {
|
|
isSystemUser = true;
|
|
group = "affine-native";
|
|
home = stateDirectory;
|
|
};
|
|
|
|
systemd.services.affine-native-migrate = {
|
|
description = "AFFiNE database and data migrations";
|
|
after = [ "network-online.target" ];
|
|
wants = [ "network-online.target" ];
|
|
environment = commonEnvironment;
|
|
preStart = prepareState;
|
|
restartTriggers = [ cfg.package initialConfig ];
|
|
serviceConfig = commonServiceConfig // {
|
|
Type = "oneshot";
|
|
ExecStart = "${cfg.package}/bin/affine-migrate";
|
|
RemainAfterExit = true;
|
|
};
|
|
};
|
|
|
|
systemd.services.affine-native = {
|
|
description = "Native AFFiNE server";
|
|
after = [ "network-online.target" "affine-native-migrate.service" ];
|
|
wants = [ "network-online.target" ];
|
|
requires = [ "affine-native-migrate.service" ];
|
|
wantedBy = [ "multi-user.target" ];
|
|
environment = commonEnvironment;
|
|
preStart = prepareState;
|
|
restartTriggers = [ cfg.package initialConfig ] ++ lib.optional (cfg.frontendPackage != null) cfg.frontendPackage;
|
|
serviceConfig = commonServiceConfig // {
|
|
Type = "simple";
|
|
ExecStart = "${cfg.package}/bin/affine-server";
|
|
Restart = "on-failure";
|
|
RestartSec = "5s";
|
|
BindReadOnlyPaths = lib.optional (cfg.frontendPackage != null)
|
|
"${cfg.frontendPackage}:${cfg.package}/share/affine/static";
|
|
};
|
|
};
|
|
};
|
|
}
|