{
  lib,
  stdenv,
  fetchFromGitHub,
}:

let
  capnproto.src = fetchFromGitHub {
    owner = "capnproto";
    repo = "capnproto";
    tag = "v1.0.2";
    hash = "sha256-LVdkqVBTeh8JZ1McdVNtRcnFVwEJRNjt0JV2l7RkuO8=";
  };
in
stdenv.mkDerivation {
  pname = "ekam";
  version = "0-unstable-2021-09-18";

  src = fetchFromGitHub {
    owner = "capnproto";
    repo = "ekam";
    rev = "77c338f8bd8f4a2ce1e6199b2a52363f1fccf388";
    hash = "sha256-KONQMvbRW+acjEPmbgzCidZSu2l0AsXIbX/6sOiPi2A=";
  };

  # The capnproto *source* is required to build ekam.
  # https://github.com/capnproto/ekam/issues/5
  #
  # Specifically, the git version of the source is required, as
  # capnproto release tarballs do not include ekam rule files.
  postUnpack = ''
    mkdir -p $sourceRoot/deps
    cp -r ${capnproto.src} $sourceRoot/deps/capnproto
  '';

  postPatch = ''
    # A single capnproto test file expects to be able to write to
    # /var/tmp.  We change it to use /tmp because /var is not available
    # under nix-build.
    substituteInPlace deps/capnproto/c++/src/kj/filesystem-disk-test.c++ \
      --replace "/var/tmp" "/tmp"
  '';

  # NIX_ENFORCE_PURITY prevents ld from linking against anything outside
  # of the nix store -- but ekam builds capnp locally and links against it,
  # so that causes the build to fail. So, we turn this off.
  #
  # See: https://wiki.nixos.org/wiki/Development_environment_with_nix-shell#Troubleshooting
  preBuild = ''
    unset NIX_ENFORCE_PURITY
  '';

  enableParallelBuilding = true;

  postBuild = ''
    rm bin/{capnp*,ekam-bootstrap}
  '';

  installPhase = ''
    runHook preInstall
    mkdir $out
    cp -r bin $out
    runHook postInstall
  '';

  meta = {
    description = ''Build system ("make" in reverse)'';
    longDescription = ''
      Ekam ("make" spelled backwards) is a build system which automatically
      figures out what to build and how to build it purely based on the
      source code. No separate "makefile" is needed.
    '';
    homepage = "https://github.com/capnproto/ekam";
    license = lib.licenses.asl20;
    platforms = lib.platforms.linux;
    maintainers = [ lib.maintainers.garrison ];
  };
}
