{ lib
, rustPlatform
, rustfmt
, stdenv
, pkg-config
, openssl
}:

let
  root = ../../.;
  # Keep the build input stable and small. NixOS, infra, docs, and script-only
  # changes should not perturb the Rust source hash for config-only deploys.
  src = lib.cleanSourceWith {
    src = root;
    filter = path: type:
      let
        rel = lib.removePrefix "${toString root}/" (toString path);
      in
      rel == ""
        || rel == "Cargo.toml"
        || rel == "Cargo.lock"
        || rel == "crates"
        || lib.hasPrefix "crates/" rel
        || rel == "third_party"
        || rel == "third_party/iroh-org"
        || rel == "third_party/iroh-org/iroh-gossip"
        || lib.hasPrefix "third_party/iroh-org/iroh-gossip/" rel
        || rel == "third_party/iroh-live"
        || rel == "third_party/iroh-live/iroh-moq"
        || lib.hasPrefix "third_party/iroh-live/iroh-moq/" rel
        || rel == "third_party/iroh-live/web-transport-iroh"
        || lib.hasPrefix "third_party/iroh-live/web-transport-iroh/" rel
        || rel == "apps"
        || rel == "apps/tauri"
        || rel == "apps/tauri/Cargo.toml"
        || rel == "apps/tauri/build.rs"
        || rel == "apps/tauri/tauri.conf.json"
        || rel == "apps/tauri/gen"
        || lib.hasPrefix "apps/tauri/gen/" rel
        || rel == "apps/tauri/src"
        || lib.hasPrefix "apps/tauri/src/" rel;
  };
in
rustPlatform.buildRustPackage {
  pname = "ec-node";
  version = "0.0.0";
  inherit src;

  cargoLock = {
    lockFile = ../../Cargo.lock;
  };

  cargoBuildFlags = [ "-p" "ec-node" ];

  nativeBuildInputs = [
    pkg-config
    rustfmt
  ];

  buildInputs =
    [
      openssl
    ];

  doCheck = false;

  meta = with lib; {
    description = "every.channel node (ingest + chunk + MoQ publish)";
    mainProgram = "ec-node";
    platforms = platforms.unix;
    license = licenses.agpl3Only;
  };
}
