Newer
Older
percord / nix / modules / default / gw-sharding.nix
self:
{
  config,
  lib,
  pkgs,
  spacebar,
  ...
}:

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; });
in
{
  options.services.spacebarchat-server = {
    extraGatewayPorts = lib.mkOption {
      type = lib.types.listOf lib.types.port;
      description = "Extra gateway ports";
    };
  };

  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";
            };
          };
        }) cfg.extraGatewayPorts
      );
    }
  );
}