This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
{ ... }:
|
||||
{ config, lib, pkgs, ... }: let
|
||||
cfg = config.hectic.services.affine-chatgpt;
|
||||
stateDirectory = "/var/lib/affine-chatgpt";
|
||||
in {
|
||||
options.hectic.services.affine-chatgpt = {
|
||||
enable = lib.mkEnableOption "native ChatGPT subscription gateway for AFFiNE";
|
||||
package = lib.mkOption {
|
||||
type = lib.types.package;
|
||||
default = pkgs.hectic.affine-chatgpt;
|
||||
description = "Package providing the affine-chatgpt executable.";
|
||||
};
|
||||
port = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
default = 3012;
|
||||
description = "Loopback-only gateway port.";
|
||||
};
|
||||
gatewayKeyFile = lib.mkOption {
|
||||
type = lib.types.path;
|
||||
default = "${stateDirectory}/gateway-key";
|
||||
description = "Owner-only runtime bearer key readable by affine-chatgpt; never put its contents in Nix.";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
users.groups.affine-chatgpt = {};
|
||||
users.users.affine-chatgpt = {
|
||||
isSystemUser = true;
|
||||
group = "affine-chatgpt";
|
||||
home = stateDirectory;
|
||||
};
|
||||
systemd.services.affine-chatgpt = {
|
||||
description = "Native ChatGPT subscription gateway for AFFiNE";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
unitConfig.ConditionPathExists = "${stateDirectory}/session/credentials.json";
|
||||
preStart = ''
|
||||
install -d -m 0700 ${stateDirectory}/session
|
||||
'';
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
User = "affine-chatgpt";
|
||||
Group = "affine-chatgpt";
|
||||
StateDirectory = "affine-chatgpt";
|
||||
StateDirectoryMode = "0700";
|
||||
ExecStart = "${cfg.package}/bin/affine-chatgpt --state-dir ${stateDirectory}/session serve --host 127.0.0.1 --port ${toString cfg.port} --gateway-key-file ${cfg.gatewayKeyFile}";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
UMask = "0077";
|
||||
NoNewPrivileges = true;
|
||||
PrivateTmp = true;
|
||||
ProtectHome = true;
|
||||
ProtectSystem = "strict";
|
||||
ReadWritePaths = [ stateDirectory ];
|
||||
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
|
||||
CapabilityBoundingSet = "";
|
||||
LimitCORE = 0;
|
||||
MemoryMax = "256M";
|
||||
TasksMax = 64;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -67,6 +67,8 @@ in {
|
||||
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 = ''
|
||||
@@ -78,6 +80,15 @@ in {
|
||||
'';
|
||||
};
|
||||
|
||||
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 = ''
|
||||
@@ -133,10 +144,33 @@ in {
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
assertions = [{
|
||||
assertion = pkgs.stdenv.hostPlatform.system == "x86_64-linux";
|
||||
message = "hectic.services.affine currently supports only x86_64-linux.";
|
||||
}];
|
||||
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 = {
|
||||
@@ -167,12 +201,14 @@ in {
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
environment = commonEnvironment;
|
||||
preStart = prepareState;
|
||||
restartTriggers = [ cfg.package initialConfig ];
|
||||
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";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user