{
  self,
  config,
  lib,
  ...
}: let
  inherit (lib.lists) filter;
  inherit (builtins) attrNames;

  clusterConfig = import "${self}/data/piped/pipedClusterConfig.nix";
  inherit (clusterConfig) hosts ports;

  currentHostName = config.networking.hostName;

  joinString = lib.concatStringsSep "," ([
      "localhost:${toString ports.cockroachDB}"
    ]
    ++ (
      map
      (hostName: hosts.${hostName}.joinString)
      (filter (hostName: hostName != currentHostName) (attrNames hosts))
    ));
in {
  systemd.services.haproxy.wantedBy = ["piped-backend.service"];

  services.cockroachdb-bin = {
    enable = true;
    certsDir = "/var/lib/cockroachdb-certs";
    join = joinString;
    extraArgs = ["--advertise-addr=${hosts.${currentHostName}.advertiseAddr}"];
    listen = {
      port = ports.cockroachDB;
      address = "0.0.0.0";
    };
    http = {
      address = "0.0.0.0";
      port = ports.cockroachDB_HTTP;
    };
  };
}