Factor out some lib stuff from nix

This commit is contained in:
Rory&
2026-04-06 08:45:35 +02:00
parent 07bdc337eb
commit da7a99c74f
14 changed files with 376 additions and 424 deletions

View File

@@ -1,8 +1,10 @@
{
lib,
secrets,
cfg,
cfg
}:
let
secrets = import ./secrets.nix { inherit lib cfg; };
in
conf:
lib.recursiveUpdate
(lib.recursiveUpdate {

28
nix/lib/options.nix Normal file
View File

@@ -0,0 +1,28 @@
{ lib }:
{
mkEndpointOption =
defaultHost: defaultPort:
lib.mkOption {
type = lib.types.submodule {
options = {
useSsl = lib.mkEnableOption "Use SSL for this endpoint.";
host = lib.mkOption {
type = lib.types.str;
default = defaultHost;
description = "Host to bind to.";
};
localPort = lib.mkOption {
type = lib.types.port;
default = defaultPort;
description = "Port to bind to.";
};
publicPort = lib.mkOption {
type = lib.types.port;
default = 443;
description = "Public port to use in .well-known, defaults to 443.";
};
};
};
default = { };
};
}

View File

@@ -1,7 +1,4 @@
{ lib, config, ... }:
let
cfg = config.services.spacebarchat-server;
in
{ lib, cfg, ... }:
{
options = {
cdnSignaturePath = lib.mkOption {

View File

@@ -8,9 +8,9 @@ self:
}:
let
secrets = import ../secrets.nix { inherit lib config; };
cfg = config.services.spacebarchat-server;
jsonFormat = pkgs.formats.json { };
makeServerTsService = import ../../../lib/makeServerTsService.nix { inherit cfg lib; };
in
{
imports = [ ];
@@ -29,42 +29,37 @@ in
};
};
config = lib.mkIf cfg.adminApi.enable (
let
makeServerTsService = import ../makeServerTsService.nix { inherit cfg lib secrets; };
in
{
assertions = [
(import ./assert-has-connection-string.nix "Admin API" cfg.adminApi.extraConfiguration)
];
config = lib.mkIf cfg.adminApi.enable {
assertions = [
(import ./assert-has-connection-string.nix "Admin API" cfg.adminApi.extraConfiguration)
];
services.spacebarchat-server.settings.admin = {
endpointPublic = "http${if cfg.adminApiEndpoint.useSsl then "s" else ""}://${cfg.adminApiEndpoint.host}:${toString cfg.adminApiEndpoint.publicPort}";
endpointPrivate = "http://127.0.0.1:${builtins.toString cfg.adminApiEndpoint.localPort}";
};
services.spacebarchat-server.settings.admin = {
endpointPublic = "http${if cfg.adminApiEndpoint.useSsl then "s" else ""}://${cfg.adminApiEndpoint.host}:${toString cfg.adminApiEndpoint.publicPort}";
endpointPrivate = "http://127.0.0.1:${builtins.toString cfg.adminApiEndpoint.localPort}";
};
systemd.services.spacebar-admin-api = makeServerTsService {
description = "Spacebar Server - Admin API";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
# CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
ASPNETCORE_URLS = "http://0.0.0.0:${toString cfg.adminApiEndpoint.localPort}";
STORAGE_LOCATION = cfg.cdnPath;
APPSETTINGS_PATH = jsonFormat.generate "appsettings.spacebar-adminapi.json" (lib.recursiveUpdate (import ./default-appsettings-json.nix) cfg.adminApi.extraConfiguration);
}
);
serviceConfig = {
ExecStart = "${self.packages.${pkgs.stdenv.hostPlatform.system}.Spacebar-AdminApi}/bin/Spacebar.AdminApi";
};
systemd.services.spacebar-admin-api = makeServerTsService {
description = "Spacebar Server - Admin API";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
# CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
ASPNETCORE_URLS = "http://0.0.0.0:${toString cfg.adminApiEndpoint.localPort}";
STORAGE_LOCATION = cfg.cdnPath;
APPSETTINGS_PATH = jsonFormat.generate "appsettings.spacebar-adminapi.json" (lib.recursiveUpdate (import ./default-appsettings-json.nix) cfg.adminApi.extraConfiguration);
}
);
serviceConfig = {
ExecStart = "${self.packages.${pkgs.stdenv.hostPlatform.system}.Spacebar-AdminApi}/bin/Spacebar.AdminApi";
};
}
);
};
};
}

View File

@@ -11,6 +11,7 @@ let
secrets = import ../secrets.nix { inherit lib config; };
cfg = config.services.spacebarchat-server;
jsonFormat = pkgs.formats.json { };
makeServerTsService = import ../../../lib/makeServerTsService.nix { inherit cfg lib secrets; };
in
{
imports = [ ];
@@ -29,37 +30,32 @@ in
};
};
config = lib.mkIf cfg.cdnCs.enable (
let
makeServerTsService = import ../makeServerTsService.nix { inherit cfg lib secrets; };
in
{
assertions = [
(import ./assert-has-connection-string.nix "Admin API" cfg.adminApi.extraConfiguration)
];
config = lib.mkIf cfg.cdnCs.enable {
assertions = [
(import ./assert-has-connection-string.nix "Admin API" cfg.adminApi.extraConfiguration)
];
systemd.services.spacebar-cdn = makeServerTsService {
description = "Spacebar Server - CDN (C#)";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
# CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
ASPNETCORE_URLS = "http://0.0.0.0:${toString cfg.cdnEndpoint.localPort}";
STORAGE_LOCATION = cfg.cdnPath;
APPSETTINGS_PATH = jsonFormat.generate "appsettings.spacebar-cdn.json" (lib.recursiveUpdate (import ./default-appsettings-json.nix) cfg.cdnCs.extraConfiguration);
}
);
serviceConfig = {
ExecStart = "${self.packages.${pkgs.stdenv.hostPlatform.system}.Spacebar-AdminApi}/bin/Spacebar.AdminApi";
};
systemd.services.spacebar-cdn = makeServerTsService {
description = "Spacebar Server - CDN (C#)";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
# CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
ASPNETCORE_URLS = "http://0.0.0.0:${toString cfg.cdnEndpoint.localPort}";
STORAGE_LOCATION = cfg.cdnPath;
APPSETTINGS_PATH = jsonFormat.generate "appsettings.spacebar-cdn.json" (lib.recursiveUpdate (import ./default-appsettings-json.nix) cfg.cdnCs.extraConfiguration);
}
);
serviceConfig = {
ExecStart = "${self.packages.${pkgs.stdenv.hostPlatform.system}.Spacebar-AdminApi}/bin/Spacebar.AdminApi";
};
}
);
};
};
}

View File

@@ -8,10 +8,10 @@ self:
}:
let
secrets = import ../secrets.nix { inherit lib config; };
cfg = config.services.spacebarchat-server;
offloadCfg = cfg.offload;
jsonFormat = pkgs.formats.json { };
makeServerTsService = import ../../../lib/makeServerTsService.nix { inherit cfg lib; };
in
{
imports = [ ];
@@ -48,50 +48,43 @@ in
};
};
config = lib.mkIf offloadCfg.enable (
let
makeServerTsService = import ../makeServerTsService.nix { inherit cfg lib secrets; };
in
{
assertions = [
(import ./assert-has-connection-string.nix "Gateway Offload" offloadCfg.extraConfiguration)
];
config = lib.mkIf offloadCfg.enable {
assertions = [
(import ./assert-has-connection-string.nix "Gateway Offload" offloadCfg.extraConfiguration)
];
services.spacebarchat-server.settings.offload = {
gateway = {
identifyUrl = lib.mkIf offloadCfg.gateway.enableIdentify "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/Identify";
guildMembersUrl = lib.mkIf offloadCfg.gateway.enableGuildMembers "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/GuildMembers";
guildSyncUrlUrl = lib.mkIf offloadCfg.gateway.enableGuildSync "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/GuildSync";
lazyRequestUrl = lib.mkIf offloadCfg.gateway.enableLazyRequest "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/LazyRequest";
channelStatusesUrl = lib.mkIf offloadCfg.gateway.enableChannelStatuses "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/ChannelStatuses";
channelInfoUrl = lib.mkIf offloadCfg.gateway.enableChannelInfo "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/ChannelInfo";
};
services.spacebarchat-server.settings.offload = {
gateway = {
identifyUrl = lib.mkIf offloadCfg.gateway.enableIdentify "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/Identify";
guildMembersUrl = lib.mkIf offloadCfg.gateway.enableGuildMembers "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/GuildMembers";
guildSyncUrlUrl = lib.mkIf offloadCfg.gateway.enableGuildSync "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/GuildSync";
lazyRequestUrl = lib.mkIf offloadCfg.gateway.enableLazyRequest "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/LazyRequest";
channelStatusesUrl = lib.mkIf offloadCfg.gateway.enableChannelStatuses "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/ChannelStatuses";
channelInfoUrl = lib.mkIf offloadCfg.gateway.enableChannelInfo "http://127.0.0.1:${builtins.toString offloadCfg.listenPort}/_spacebar/offload/gateway/ChannelInfo";
};
};
systemd.services.spacebar-cs-offload = makeServerTsService {
description = "Spacebar Server - C# offload";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
# CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
ASPNETCORE_URLS = "http://0.0.0.0:${toString offloadCfg.listenPort}";
STORAGE_LOCATION = cfg.cdnPath;
APPSETTINGS_PATH = jsonFormat.generate "appsettings.spacebar-offload.json" (
lib.recursiveUpdate (import ./default-appsettings-json.nix) offloadCfg.extraConfiguration
);
}
);
serviceConfig = {
ExecStart = "${lib.getExe self.packages.${pkgs.stdenv.hostPlatform.system}.Spacebar-Offload}";
};
systemd.services.spacebar-cs-offload = makeServerTsService {
description = "Spacebar Server - C# offload";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
# CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
ASPNETCORE_URLS = "http://0.0.0.0:${toString offloadCfg.listenPort}";
STORAGE_LOCATION = cfg.cdnPath;
APPSETTINGS_PATH = jsonFormat.generate "appsettings.spacebar-offload.json" (lib.recursiveUpdate (import ./default-appsettings-json.nix) offloadCfg.extraConfiguration);
}
);
serviceConfig = {
ExecStart = "${lib.getExe self.packages.${pkgs.stdenv.hostPlatform.system}.Spacebar-Offload}";
};
}
);
};
};
}

View File

@@ -8,9 +8,9 @@ self:
}:
let
secrets = import ../secrets.nix { inherit lib config; };
cfg = config.services.spacebarchat-server;
jsonFormat = pkgs.formats.json { };
makeServerTsService = import ../../../lib/makeServerTsService.nix { inherit cfg lib; };
in
{
imports = [ ];
@@ -34,38 +34,33 @@ in
};
};
config = lib.mkIf cfg.uApi.enable (
let
makeServerTsService = import ../makeServerTsService.nix { inherit cfg lib secrets; };
in
{
assertions = [
(import ./assert-has-connection-string.nix "uAPI" cfg.uApi.extraConfiguration)
];
config = lib.mkIf cfg.uApi.enable {
assertions = [
(import ./assert-has-connection-string.nix "uAPI" cfg.uApi.extraConfiguration)
];
systemd.services.spacebar-uapi = makeServerTsService {
description = "Spacebar Server - C# API overlay";
# after = [ "spacebar-api.service" ];
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
# CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
ASPNETCORE_URLS = "http://0.0.0.0:${toString cfg.uApi.listenPort}";
STORAGE_LOCATION = cfg.cdnPath;
APPSETTINGS_PATH = jsonFormat.generate "appsettings.spacebar-uapi.json" (lib.recursiveUpdate (import ./default-appsettings-json.nix) cfg.uApi.extraConfiguration);
}
);
serviceConfig = {
ExecStart = "${self.packages.${pkgs.stdenv.hostPlatform.system}.Spacebar-UApi}/bin/Spacebar.UApi";
};
systemd.services.spacebar-uapi = makeServerTsService {
description = "Spacebar Server - C# API overlay";
# after = [ "spacebar-api.service" ];
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
# CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
ASPNETCORE_URLS = "http://0.0.0.0:${toString cfg.uApi.listenPort}";
STORAGE_LOCATION = cfg.cdnPath;
APPSETTINGS_PATH = jsonFormat.generate "appsettings.spacebar-uapi.json" (lib.recursiveUpdate (import ./default-appsettings-json.nix) cfg.uApi.extraConfiguration);
}
);
serviceConfig = {
ExecStart = "${self.packages.${pkgs.stdenv.hostPlatform.system}.Spacebar-UApi}/bin/Spacebar.UApi";
};
}
);
};
};
}

View File

@@ -8,10 +8,12 @@ self:
}:
let
secrets = import ./secrets.nix { inherit lib config; };
secrets = import ../../lib/secrets.nix { inherit lib config cfg; };
cfg = config.services.spacebarchat-server;
jsonFormat = pkgs.formats.json { };
configFile = (import ./config-file.nix { inherit config lib pkgs; });
sbOptions = import ../../lib/options.nix { inherit lib; };
makeServerTsService = import ../../lib/makeServerTsService.nix { inherit cfg lib; };
in
{
imports = [
@@ -24,155 +26,140 @@ in
(import ./cs/admin-api.nix self)
(import ./cs/uapi.nix self)
];
options.services.spacebarchat-server =
let
mkEndpointOptions = import ./options-subtypes/mkEndpointOptions.nix { inherit lib; };
in
{
enable = lib.mkEnableOption "Spacebar server";
package = lib.mkPackageOption self.packages.${pkgs.stdenv.hostPlatform.system} "spacebar-server" { default = "default"; };
databaseFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
Path to a file containing a definition of the `DATABASE` environment variable database connection string.
Example content: `DATABASE=postgres://username:password@host-IP:port/databaseName`.
See https://docs.spacebar.chat/setup/server/database/.
'';
};
options.services.spacebarchat-server = {
enable = lib.mkEnableOption "Spacebar server";
package = lib.mkPackageOption self.packages.${pkgs.stdenv.hostPlatform.system} "spacebar-server" { default = "default"; };
databaseFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
Path to a file containing a definition of the `DATABASE` environment variable database connection string.
Example content: `DATABASE=postgres://username:password@host-IP:port/databaseName`.
See https://docs.spacebar.chat/setup/server/database/.
'';
};
serverName = lib.mkOption {
type = lib.types.str;
description = "The server name for this Spacebar instance (aka. common name, usually the domain where your well known is hosted).";
};
apiEndpoint = mkEndpointOptions "api.sb.localhost" 3001;
gatewayEndpoint = mkEndpointOptions "gateway.sb.localhost" 3003;
cdnEndpoint = mkEndpointOptions "cdn.sb.localhost" 3003;
adminApiEndpoint = mkEndpointOptions "admin-api.sb.localhost" 3004;
webrtcEndpoint = mkEndpointOptions "voice.sb.localhost" 3005;
serverName = lib.mkOption {
type = lib.types.str;
description = "The server name for this Spacebar instance (aka. common name, usually the domain where your well known is hosted).";
};
apiEndpoint = sbOptions.mkEndpointOption "api.sb.localhost" 3001;
gatewayEndpoint = sbOptions.mkEndpointOption "gateway.sb.localhost" 3003;
cdnEndpoint = sbOptions.mkEndpointOption "cdn.sb.localhost" 3003;
adminApiEndpoint = sbOptions.mkEndpointOption "admin-api.sb.localhost" 3004;
webrtcEndpoint = sbOptions.mkEndpointOption "voice.sb.localhost" 3005;
cdnPath = lib.mkOption {
type = lib.types.str;
default = "./files";
description = "Path to store CDN files.";
};
cdnPath = lib.mkOption {
type = lib.types.str;
default = "./files";
description = "Path to store CDN files.";
};
extraEnvironment = lib.mkOption {
default = { };
description = ''
Environment variables passed to spacebarchat-server.
See https://docs.spacebar.chat/setup/server/configuration/env for supported values.
'';
type = lib.types.submodule {
freeformType =
with lib.types;
attrsOf (oneOf [
str
bool
int
]);
options = {
THREADS = lib.mkOption {
type = lib.types.ints.positive;
default = 1;
description = "Number of threads to run Spacebar on when using bundle. Make sure you've enabled RabbitMQ if using more than one.";
};
extraEnvironment = lib.mkOption {
default = { };
description = ''
Environment variables passed to spacebarchat-server.
See https://docs.spacebar.chat/setup/server/configuration/env for supported values.
'';
type = lib.types.submodule {
freeformType =
with lib.types;
attrsOf (oneOf [
str
bool
int
]);
options = {
THREADS = lib.mkOption {
type = lib.types.ints.positive;
default = 1;
description = "Number of threads to run Spacebar on when using bundle. Make sure you've enabled RabbitMQ if using more than one.";
};
};
};
};
settings = lib.mkOption {
type = jsonFormat.type;
default = { };
description = ''
Configuration for spacebarchat-server.
See https://docs.spacebar.chat/setup/server/configuration for supported values.
'';
settings = lib.mkOption {
type = jsonFormat.type;
default = { };
description = ''
Configuration for spacebarchat-server.
See https://docs.spacebar.chat/setup/server/configuration for supported values.
'';
};
}
// secrets.options;
config = lib.mkIf cfg.enable {
services.spacebarchat-server.uApi.extraConfiguration.Spacebar.UApi.FallbackApiEndpoint = "http://127.0.0.1:${toString cfg.apiEndpoint.localPort}";
systemd.services.spacebar-api = makeServerTsService {
description = "Spacebar Server - API";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString cfg.apiEndpoint.localPort;
STORAGE_LOCATION = cfg.cdnPath;
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-api";
};
}
// secrets.options;
};
config = lib.mkIf cfg.enable (
let
makeServerTsService = import ./makeServerTsService.nix { inherit cfg lib secrets; };
in
{
assertions = [
# {
# assertion = lib.all (map (key: !(key == "CONFIG_PATH" || key == "CONFIG_READONLY" || key == "PORT" || key == "STORAGE_LOCATION")) (lib.attrNames cfg.extraEnvironment));
# message = "You cannot set CONFIG_PATH, CONFIG_READONLY, PORT or STORAGE_LOCATION in extraEnvironment, these are managed by the NixOS module.";
# }
];
services.spacebarchat-server.uApi.extraConfiguration.Spacebar.UApi.FallbackApiEndpoint = "http://127.0.0.1:${toString cfg.apiEndpoint.localPort}";
systemd.services.spacebar-api = makeServerTsService {
description = "Spacebar Server - API";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString cfg.apiEndpoint.localPort;
STORAGE_LOCATION = cfg.cdnPath;
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-api";
};
systemd.services.spacebar-gateway = makeServerTsService {
description = "Spacebar Server - Gateway";
# after = [ "spacebar-api.service" ];
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString cfg.gatewayEndpoint.localPort;
STORAGE_LOCATION = cfg.cdnPath;
APPLY_DB_MIGRATIONS = "false";
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-gateway";
};
};
systemd.services.spacebar-gateway = makeServerTsService {
description = "Spacebar Server - Gateway";
# after = [ "spacebar-api.service" ];
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString cfg.gatewayEndpoint.localPort;
STORAGE_LOCATION = cfg.cdnPath;
APPLY_DB_MIGRATIONS = "false";
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-gateway";
};
systemd.services.spacebar-cdn = lib.mkIf (!cfg.cdnCs.enable) (makeServerTsService {
description = "Spacebar Server - CDN";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString cfg.cdnEndpoint.localPort;
STORAGE_LOCATION = cfg.cdnPath;
APPLY_DB_MIGRATIONS = "false";
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-cdn";
};
systemd.services.spacebar-cdn = lib.mkIf (!cfg.cdnCs.enable) (makeServerTsService {
description = "Spacebar Server - CDN";
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString cfg.cdnEndpoint.localPort;
STORAGE_LOCATION = cfg.cdnPath;
APPLY_DB_MIGRATIONS = "false";
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-cdn";
};
});
}
);
});
};
}

View File

@@ -8,53 +8,47 @@ self:
}:
let
secrets = import ./secrets.nix { inherit lib config; };
cfg = config.services.spacebarchat-server;
jsonFormat = pkgs.formats.json { };
configFile = (import ./config-file.nix { inherit config lib pkgs; });
makeServerTsService = import ../../lib/makeServerTsService.nix { inherit cfg lib; };
in
{
options.services.spacebarchat-server = {
extraGatewayPorts = lib.mkOption {
type = lib.types.listOf lib.types.port;
description = "Extra gateway ports";
default = [];
default = [ ];
};
};
config = lib.mkIf cfg.enable (
let
makeServerTsService = import ./makeServerTsService.nix { inherit cfg lib secrets; };
in
{
systemd.services = builtins.listToAttrs (
map (port: {
name = "spacebar-gateway-${toString port}";
value = makeServerTsService {
description = "Spacebar Server - Gateway (extra port ${toString port})";
# after = [ "spacebar-api.service" ];
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString port;
STORAGE_LOCATION = cfg.cdnPath;
APPLY_DB_MIGRATIONS = "false";
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-gateway";
};
config = lib.mkIf cfg.enable {
systemd.services = builtins.listToAttrs (
map (port: {
name = "spacebar-gateway-${toString port}";
value = makeServerTsService {
description = "Spacebar Server - Gateway (extra port ${toString port})";
# after = [ "spacebar-api.service" ];
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString port;
STORAGE_LOCATION = cfg.cdnPath;
APPLY_DB_MIGRATIONS = "false";
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-gateway";
};
}) cfg.extraGatewayPorts
);
}
);
};
}) cfg.extraGatewayPorts
);
};
}

View File

@@ -1,7 +1,6 @@
{
config,
lib,
pkgs,
...
}:

View File

@@ -1,25 +0,0 @@
{ lib }:
defaultHost: defaultPort:
lib.mkOption {
type = lib.types.submodule {
options = {
useSsl = lib.mkEnableOption "Use SSL for this endpoint.";
host = lib.mkOption {
type = lib.types.str;
default = defaultHost;
description = "Host to bind to.";
};
localPort = lib.mkOption {
type = lib.types.port;
default = defaultPort;
description = "Port to bind to.";
};
publicPort = lib.mkOption {
type = lib.types.port;
default = 443;
description = "Public port to use in .well-known, defaults to 443.";
};
};
};
default = { };
}

View File

@@ -9,83 +9,74 @@ self:
let
cfg = config.services.spacebarchat-server;
secrets = import ./secrets.nix { inherit lib config; };
configFile = (import ./config-file.nix { inherit config lib pkgs; });
makeServerTsService = import ../../lib/makeServerTsService.nix { inherit cfg lib; };
in
{
options.services.spacebarchat-server.pion-sfu =
let
mkEndpointOptions = import ./options-subtypes/mkEndpointOptions.nix { inherit lib; };
in
{
enable = lib.mkEnableOption "Enable Spacebar Pion SFU";
openFirewall = lib.mkEnableOption "Allow SFU port in firewall";
package = lib.mkPackageOption self.packages.${pkgs.stdenv.hostPlatform.system} "Pion SFU" { default = "pion-sfu"; };
options.services.spacebarchat-server.pion-sfu = {
enable = lib.mkEnableOption "Enable Spacebar Pion SFU";
openFirewall = lib.mkEnableOption "Allow SFU port in firewall";
package = lib.mkPackageOption self.packages.${pkgs.stdenv.hostPlatform.system} "Pion SFU" { default = "pion-sfu"; };
publicIp = lib.mkOption {
type = lib.types.str;
description = "Public IP address of the server.";
};
listenPort = lib.mkOption {
type = lib.types.port;
default = 6000;
description = "UDP port the SFU will listen on.";
publicIp = lib.mkOption {
type = lib.types.str;
description = "Public IP address of the server.";
};
listenPort = lib.mkOption {
type = lib.types.port;
default = 6000;
description = "UDP port the SFU will listen on.";
};
};
config = lib.mkIf cfg.pion-sfu.enable {
networking.firewall.allowedUDPPorts = lib.mkIf cfg.pion-sfu.openFirewall [ cfg.pion-sfu.listenPort ];
services.spacebarchat-server.settings.regions = {
default = "default";
available = [
{
id = "default";
name = "Default Region";
endpoint = cfg.webrtcEndpoint.host + ":" + toString cfg.webrtcEndpoint.publicPort;
vip = false;
custom = false;
deprecated = false;
}
];
};
systemd.services.spacebar-webrtc = makeServerTsService {
description = "Spacebar Server - WebRTC";
requires = [ "spacebar-sfu.service" ];
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString cfg.webrtcEndpoint.localPort;
APPLY_DB_MIGRATIONS = "false";
WRTC_LIBRARY = "@spacebarchat/pion-webrtc";
WRTC_PUBLIC_IP = cfg.pion-sfu.publicIp;
WRTC_PORT_MIN = toString cfg.pion-sfu.listenPort;
WRTC_PORT_MAX = toString cfg.pion-sfu.listenPort;
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-webrtc";
};
};
config = lib.mkIf cfg.pion-sfu.enable (
let
makeServerTsService = import ./makeServerTsService.nix { inherit cfg lib secrets; };
in
{
networking.firewall.allowedUDPPorts = lib.mkIf cfg.pion-sfu.openFirewall [ cfg.pion-sfu.listenPort ];
services.spacebarchat-server.settings.regions = {
default = "default";
available = [
{
id = "default";
name = "Default Region";
endpoint = cfg.webrtcEndpoint.host + ":" + toString cfg.webrtcEndpoint.publicPort;
vip = false;
custom = false;
deprecated = false;
}
];
systemd.services.spacebar-sfu = makeServerTsService {
description = "Spacebar Server - Pion SFU";
serviceConfig = {
ExecStart = "${lib.getExe cfg.pion-sfu.package} -ip ${cfg.pion-sfu.publicIp} -port ${toString cfg.pion-sfu.listenPort}";
};
systemd.services.spacebar-webrtc = makeServerTsService {
description = "Spacebar Server - WebRTC";
requires = [ "spacebar-sfu.service" ];
environment = builtins.mapAttrs (_: val: builtins.toString val) (
{
# things we set by default...
EVENT_TRANSMISSION = "unix";
EVENT_SOCKET_PATH = "/run/spacebar/";
}
// cfg.extraEnvironment
// {
# things we force...
CONFIG_PATH = configFile;
CONFIG_READONLY = 1;
PORT = toString cfg.webrtcEndpoint.localPort;
APPLY_DB_MIGRATIONS = "false";
WRTC_LIBRARY = "@spacebarchat/pion-webrtc";
WRTC_PUBLIC_IP = cfg.pion-sfu.publicIp;
WRTC_PORT_MIN = toString cfg.pion-sfu.listenPort;
WRTC_PORT_MAX = toString cfg.pion-sfu.listenPort;
}
);
serviceConfig = {
ExecStart = "${cfg.package}/bin/start-webrtc";
};
};
systemd.services.spacebar-sfu = makeServerTsService {
description = "Spacebar Server - Pion SFU";
serviceConfig = {
ExecStart = "${lib.getExe cfg.pion-sfu.package} -ip ${cfg.pion-sfu.publicIp} -port ${toString cfg.pion-sfu.listenPort}";
};
};
}
);
};
};
}

View File

@@ -25,7 +25,7 @@ in
services.spacebarchat-server =
let
sbLib = import ../modules/default/lib.nix;
sbLib = import ../lib/mkEndpoint.nix;
csConnectionString = "Host=127.0.0.1; Username=postgres; Password=postgres; Database=spacebar; Port=5432; Include Error Detail=true; Maximum Pool Size=1000; Command Timeout=6000; Timeout=600;";
cfg = {
enable = true;