{ inputs, pkgs, config, ... }:
{
  # Import all PlasmaCloud modules (chainfire, flaredb, iam, plasmavmc, prismnet, flashdns, fiberlb, lightningstor, creditservice).
  imports = [ inputs.self.nixosModules.plasmacloud ];

  networking.hostName = "plasmacloud-single";
  networking.firewall.allowedTCPPorts = [ 8080 8081 8082 8083 8084 8085 8086 8087 9000 9001 9002 ];

  # Enable all services with default ports and data dirs.
  services.chainfire = {
    enable = true;
    dataDir = "/var/lib/chainfire";
  };

  services.flaredb = {
    enable = true;
    dataDir = "/var/lib/flaredb";
    settings = {
      chainfire_endpoint = "http://127.0.0.1:${toString config.services.chainfire.port}";
    };
  };

  services.iam = {
    enable = true;
    dataDir = "/var/lib/iam";
    settings = {
      flaredb_endpoint = "http://127.0.0.1:${toString config.services.flaredb.port}";
    };
  };

  services.plasmavmc.enable = true;
  services.prismnet.enable = true;
  services.flashdns.enable = true;
  services.fiberlb.enable = true;
  services.apigateway = {
    enable = true;
    authProviders = [{
      name = "iam";
      providerType = "grpc";
      endpoint = "http://127.0.0.1:${toString config.services.iam.port}";
    }];
    creditProviders = [{
      name = "creditservice";
      providerType = "grpc";
      endpoint = "http://127.0.0.1:${toString config.services.creditservice.grpcPort}";
    }];
    routes = [
      {
        name = "iam-rest";
        pathPrefix = "/iam";
        upstream = "http://127.0.0.1:8083";
        stripPrefix = true;
        auth = {
          provider = "iam";
          mode = "required";
        };
      }
      {
        name = "credit-rest";
        pathPrefix = "/credit";
        upstream = "http://127.0.0.1:${toString config.services.creditservice.httpPort}";
        stripPrefix = true;
        auth = {
          provider = "iam";
          mode = "required";
        };
        credit = {
          provider = "creditservice";
          mode = "optional";
          units = 1;
          commitOn = "success";
        };
      }
    ];
  };
  services.lightningstor.enable = true;
  services.creditservice.enable = true;

  environment.systemPackages = with inputs.self.packages.${pkgs.system}; [
    chainfire-server flaredb-server iam-server plasmavmc-server
    prismnet-server flashdns-server fiberlb-server apigateway-server lightningstor-server
    creditservice-server
  ];
}
