2025-04-15 11:15:17 +02:00
|
|
|
{
|
|
|
|
|
config,
|
|
|
|
|
lib,
|
|
|
|
|
pkgs,
|
2025-04-15 14:15:36 +02:00
|
|
|
...
|
2025-04-15 11:15:17 +02:00
|
|
|
}: let
|
|
|
|
|
cfg = config.services.versia-server;
|
|
|
|
|
configFormat = pkgs.formats.toml {};
|
|
|
|
|
name = "versia-server";
|
|
|
|
|
|
|
|
|
|
inherit (lib.options) mkOption;
|
|
|
|
|
inherit (lib.modules) mkIf;
|
|
|
|
|
in {
|
|
|
|
|
options = {
|
|
|
|
|
services.versia-server = {
|
|
|
|
|
enable = mkOption {
|
|
|
|
|
type = lib.types.bool;
|
|
|
|
|
default = false;
|
|
|
|
|
description = ''
|
|
|
|
|
Enable the Versia Server services.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
default = name;
|
|
|
|
|
description = ''
|
|
|
|
|
User under which the server will run.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
default = name;
|
|
|
|
|
description = ''
|
|
|
|
|
Group under which the server will run.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
nodes = {
|
|
|
|
|
api = mkOption {
|
2025-04-15 18:11:14 +02:00
|
|
|
type = lib.types.attrsOf (lib.types.submodule {
|
2025-04-15 11:15:17 +02:00
|
|
|
options = {
|
|
|
|
|
configOverrides = mkOption {
|
|
|
|
|
type = lib.types.submodule {
|
|
|
|
|
freeformType = configFormat.type;
|
|
|
|
|
options = {};
|
|
|
|
|
};
|
2025-04-15 20:19:24 +02:00
|
|
|
default = {};
|
2025-04-15 11:15:17 +02:00
|
|
|
description = "Overrides for the node's configuration file.";
|
|
|
|
|
};
|
|
|
|
|
};
|
2025-04-15 18:11:14 +02:00
|
|
|
});
|
2025-04-15 11:15:17 +02:00
|
|
|
};
|
|
|
|
|
worker = mkOption {
|
2025-04-15 18:11:14 +02:00
|
|
|
type = lib.types.attrsOf (lib.types.submodule {
|
2025-04-15 11:15:17 +02:00
|
|
|
options = {
|
|
|
|
|
configOverrides = mkOption {
|
|
|
|
|
type = lib.types.submodule {
|
|
|
|
|
freeformType = configFormat.type;
|
|
|
|
|
options = {};
|
|
|
|
|
};
|
2025-04-15 20:19:24 +02:00
|
|
|
default = {};
|
2025-04-15 11:15:17 +02:00
|
|
|
description = "Overrides for the node's configuration file.";
|
|
|
|
|
};
|
|
|
|
|
};
|
2025-04-15 18:11:14 +02:00
|
|
|
});
|
2025-04-15 11:15:17 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
|
type = lib.types.submodule {
|
|
|
|
|
freeformType = configFormat.type;
|
|
|
|
|
options = {};
|
|
|
|
|
};
|
2025-04-15 13:03:52 +02:00
|
|
|
description = "Contents of the config file, which is serialized to TOML. Check the Versia Server documentation for information on its contents.";
|
2025-04-15 11:15:17 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
assertions = [
|
|
|
|
|
{
|
|
|
|
|
assertion = cfg.nodes.api != [];
|
|
|
|
|
message = "At least one API node must be defined.";
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
assertion = cfg.nodes.worker != [];
|
|
|
|
|
message = "At least one worker node must be defined.";
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
2025-04-15 14:15:36 +02:00
|
|
|
systemd.services =
|
2025-04-15 20:19:24 +02:00
|
|
|
lib.mapAttrs' (nodeName: node: let
|
2025-04-15 14:15:36 +02:00
|
|
|
type = "api";
|
|
|
|
|
exe = lib.getExe pkgs.versia-server;
|
2025-04-15 20:19:24 +02:00
|
|
|
config = lib.recursiveUpdate cfg.config node.configOverrides;
|
|
|
|
|
configFile = configFormat.generate "config-${nodeName}.toml" config;
|
|
|
|
|
in
|
|
|
|
|
lib.nameValuePair "${name}-${type}-${nodeName}" {
|
|
|
|
|
description = "Versia Server ${nodeName} (${type})";
|
|
|
|
|
|
2025-04-15 20:27:50 +02:00
|
|
|
wantedBy = ["versia-server-root.target"];
|
2025-04-15 20:19:24 +02:00
|
|
|
partOf = ["versia-server-root.target"];
|
|
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
ExecStart = "${exe}";
|
|
|
|
|
Type = "simple";
|
|
|
|
|
Restart = "always";
|
|
|
|
|
|
|
|
|
|
User = cfg.user;
|
|
|
|
|
Group = cfg.group;
|
|
|
|
|
|
|
|
|
|
StateDirectory = "${name}";
|
|
|
|
|
StateDirectoryMode = "0700";
|
|
|
|
|
RuntimeDirectory = "${name}";
|
|
|
|
|
RuntimeDirectoryMode = "0700";
|
|
|
|
|
|
|
|
|
|
# Set the working directory to the data directory
|
2025-04-15 21:07:47 +02:00
|
|
|
WorkingDirectory = "${pkgs.versia-server}/versia-server";
|
2025-04-15 20:19:24 +02:00
|
|
|
|
|
|
|
|
StandardOutput = "journal";
|
|
|
|
|
StandardError = "journal";
|
|
|
|
|
SyslogIdentifier = "${name}";
|
|
|
|
|
|
2025-08-09 17:15:05 +02:00
|
|
|
# Hardening
|
|
|
|
|
CapabilityBoundingSet = [""];
|
|
|
|
|
LockPersonality = true;
|
|
|
|
|
PrivateMounts = true;
|
|
|
|
|
PrivateTmp = true;
|
|
|
|
|
ProcSubset = "pid";
|
|
|
|
|
ProtectClock = true;
|
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
|
ProtectHome = true;
|
|
|
|
|
ProtectHostname = true;
|
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
|
RestrictRealtime = true;
|
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
|
RemoveIPC = true;
|
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
|
|
2025-04-15 20:19:24 +02:00
|
|
|
Environment = [
|
2025-04-15 20:31:25 +02:00
|
|
|
"CONFIG_LOCATION=${configFile}"
|
2025-04-15 20:19:24 +02:00
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
}) (cfg.nodes.api)
|
|
|
|
|
// lib.mapAttrs' (nodeName: node: let
|
2025-04-15 14:15:36 +02:00
|
|
|
type = "worker";
|
|
|
|
|
exe = lib.getExe pkgs.versia-server-worker;
|
2025-04-15 20:19:24 +02:00
|
|
|
config = lib.recursiveUpdate cfg.config node.configOverrides;
|
|
|
|
|
configFile = configFormat.generate "config-${nodeName}.toml" config;
|
|
|
|
|
in
|
|
|
|
|
lib.nameValuePair "${name}-${type}-${nodeName}" {
|
|
|
|
|
description = "Versia Server ${nodeName} (${type})";
|
|
|
|
|
|
2025-04-15 20:27:50 +02:00
|
|
|
wantedBy = ["versia-server-root.target"];
|
2025-04-15 20:19:24 +02:00
|
|
|
partOf = ["versia-server-root.target"];
|
|
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
ExecStart = "${exe}";
|
|
|
|
|
Type = "simple";
|
|
|
|
|
Restart = "always";
|
|
|
|
|
|
|
|
|
|
User = cfg.user;
|
|
|
|
|
Group = cfg.group;
|
|
|
|
|
|
|
|
|
|
StateDirectory = "${name}";
|
|
|
|
|
StateDirectoryMode = "0700";
|
|
|
|
|
RuntimeDirectory = "${name}";
|
|
|
|
|
RuntimeDirectoryMode = "0700";
|
|
|
|
|
|
|
|
|
|
# Set the working directory to the data directory
|
2025-04-15 21:07:47 +02:00
|
|
|
WorkingDirectory = "${pkgs.versia-server-worker}/versia-server-worker";
|
2025-04-15 20:19:24 +02:00
|
|
|
|
|
|
|
|
StandardOutput = "journal";
|
|
|
|
|
StandardError = "journal";
|
|
|
|
|
SyslogIdentifier = "${name}";
|
|
|
|
|
|
|
|
|
|
Environment = [
|
2025-04-15 20:31:25 +02:00
|
|
|
"CONFIG_LOCATION=${configFile}"
|
2025-04-15 20:19:24 +02:00
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
}) (cfg.nodes.worker);
|
|
|
|
|
|
|
|
|
|
systemd.targets.versia-server-root = {
|
|
|
|
|
description = "Versia Server root target, starts and stop all the child nodes.";
|
|
|
|
|
wantedBy = ["multi-user.target"];
|
|
|
|
|
};
|
2025-04-15 14:15:36 +02:00
|
|
|
|
2025-04-15 11:15:17 +02:00
|
|
|
users = {
|
2025-04-15 14:15:36 +02:00
|
|
|
groups = {
|
2025-04-15 20:19:24 +02:00
|
|
|
"${cfg.group}" = {};
|
2025-04-15 14:15:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users = {
|
|
|
|
|
"${cfg.user}" = {
|
|
|
|
|
isSystemUser = true;
|
|
|
|
|
group = cfg.group;
|
|
|
|
|
packages = [pkgs.versia-server pkgs.versia-server-worker];
|
|
|
|
|
};
|
2025-04-15 11:15:17 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|