65 lines
2.2 KiB
Nix
65 lines
2.2 KiB
Nix
{ ... }:
|
|
{ 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;
|
|
};
|
|
};
|
|
};
|
|
}
|