diff --git a/nix/modules/default/makeServerTsService.nix b/nix/lib/makeServerTsService.nix similarity index 97% rename from nix/modules/default/makeServerTsService.nix rename to nix/lib/makeServerTsService.nix index 7a6112510..dd930c9b2 100644 --- a/nix/modules/default/makeServerTsService.nix +++ b/nix/lib/makeServerTsService.nix @@ -1,8 +1,10 @@ { lib, - secrets, - cfg, + cfg }: +let + secrets = import ./secrets.nix { inherit lib cfg; }; +in conf: lib.recursiveUpdate (lib.recursiveUpdate { diff --git a/nix/modules/default/lib.nix b/nix/lib/mkEndpoint.nix similarity index 100% rename from nix/modules/default/lib.nix rename to nix/lib/mkEndpoint.nix diff --git a/nix/lib/options.nix b/nix/lib/options.nix new file mode 100644 index 000000000..b1e1da45e --- /dev/null +++ b/nix/lib/options.nix @@ -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 = { }; + }; +} diff --git a/nix/modules/default/secrets.nix b/nix/lib/secrets.nix similarity index 98% rename from nix/modules/default/secrets.nix rename to nix/lib/secrets.nix index f28720964..71381e490 100644 --- a/nix/modules/default/secrets.nix +++ b/nix/lib/secrets.nix @@ -1,7 +1,4 @@ -{ lib, config, ... }: -let - cfg = config.services.spacebarchat-server; -in +{ lib, cfg, ... }: { options = { cdnSignaturePath = lib.mkOption { diff --git a/nix/modules/default/cs/admin-api.nix b/nix/modules/default/cs/admin-api.nix index 531b50aa3..2d3ad2f2a 100644 --- a/nix/modules/default/cs/admin-api.nix +++ b/nix/modules/default/cs/admin-api.nix @@ -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"; }; - } - ); + }; + }; } diff --git a/nix/modules/default/cs/cdn-cs.nix b/nix/modules/default/cs/cdn-cs.nix index 42432dab8..f2f56fa79 100644 --- a/nix/modules/default/cs/cdn-cs.nix +++ b/nix/modules/default/cs/cdn-cs.nix @@ -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"; }; - } - ); + }; + }; } diff --git a/nix/modules/default/cs/offload-cs.nix b/nix/modules/default/cs/offload-cs.nix index 9186edcb7..72324f180 100644 --- a/nix/modules/default/cs/offload-cs.nix +++ b/nix/modules/default/cs/offload-cs.nix @@ -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}"; }; - } - ); + }; + }; } diff --git a/nix/modules/default/cs/uapi.nix b/nix/modules/default/cs/uapi.nix index 00caa214f..23f220ecf 100644 --- a/nix/modules/default/cs/uapi.nix +++ b/nix/modules/default/cs/uapi.nix @@ -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"; }; - } - ); + }; + }; } diff --git a/nix/modules/default/default.nix b/nix/modules/default/default.nix index fefb25ce0..5602c2589 100644 --- a/nix/modules/default/default.nix +++ b/nix/modules/default/default.nix @@ -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"; - }; - }); - } - ); + }); + }; } diff --git a/nix/modules/default/gw-sharding.nix b/nix/modules/default/gw-sharding.nix index 9358aca92..8e79dc788 100644 --- a/nix/modules/default/gw-sharding.nix +++ b/nix/modules/default/gw-sharding.nix @@ -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 + ); + }; } diff --git a/nix/modules/default/integration-nginx.nix b/nix/modules/default/integration-nginx.nix index 1dfa1faa9..2a86e5fd9 100644 --- a/nix/modules/default/integration-nginx.nix +++ b/nix/modules/default/integration-nginx.nix @@ -1,7 +1,6 @@ { config, lib, - pkgs, ... }: diff --git a/nix/modules/default/options-subtypes/mkEndpointOptions.nix b/nix/modules/default/options-subtypes/mkEndpointOptions.nix deleted file mode 100644 index 979ec3043..000000000 --- a/nix/modules/default/options-subtypes/mkEndpointOptions.nix +++ /dev/null @@ -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 = { }; -} diff --git a/nix/modules/default/pion-sfu.nix b/nix/modules/default/pion-sfu.nix index aa2fbf240..92bcf5330 100644 --- a/nix/modules/default/pion-sfu.nix +++ b/nix/modules/default/pion-sfu.nix @@ -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}"; - }; - }; - } - ); + }; + }; } diff --git a/nix/testVm/configuration.nix b/nix/testVm/configuration.nix index bd1e54b82..a610e8edf 100644 --- a/nix/testVm/configuration.nix +++ b/nix/testVm/configuration.nix @@ -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;