{ pkgs, src }:

let
  condaBuilderEnv = import ./builder-env.nix { inherit pkgs; };
  fake-src = pkgs.runCommand "conda-fake-source-llvmlite" { }
    ''
    mkdir -p $out/fake-conda;

    mkdir conda-sucks
    pushd conda-sucks
    tar xvf ${src} --strip-components=1
    tar cf $out/src.tar .
    patch -p1 < ${../pkgs/llvmlite-callsite.diff}
    patch -p1 < ${../pkgs/llvmlite-abiname.diff}
    popd
    rm -rf conda-sucks

    cat << EOF > $out/fake-conda/meta.yaml
    package:
      name: llvmlite
      version: 0.99  # high version number to entice the conda filth to choose it over others

    source:
      url: ../src.tar

    # Again, we don't specify build dependencies since the conda garbage mistakenly thinks
    # that they are not there if they have been installed from files.
    requirements:
      run:
        - libllvm11
        - python<3.9
        - zlib
    EOF

    cat << EOF > $out/fake-conda/build.sh
    #!/bin/bash
    set -e

    export LD_LIBRARY_PATH=/lib
    python setup.py install \
      --prefix=\$PREFIX \
      --single-version-externally-managed \
      --record=record.txt \
      --no-compile

    EOF
    chmod 755 $out/fake-conda/build.sh
    '';
  conda-zlib = pkgs.fetchurl {
    url = "https://anaconda.org/conda-forge/zlib/1.2.11/download/linux-64/zlib-1.2.11-h36c2ea0_1013.tar.bz2";
    sha256 = "sha256-zsSNs1p97wARv9qiuR5eBdKgrXiLiHGiE+uMrP63QYo=";
  };
  conda-llvm = pkgs.fetchurl {
    url = "https://anaconda.org/conda-forge/llvm/11.1.0/download/linux-64/llvm-11.1.0-h32600fe_2.tar.bz2";
    sha256 = "sha256-E+jnVeGHad3LH+dKqFKH0/lBuQqZKtybXF44uArmNz8=";
  };
  conda-llvm-tools = pkgs.fetchurl {
    url = "https://anaconda.org/conda-forge/llvm-tools/11.1.0/download/linux-64/llvm-tools-11.1.0-hf817b99_2.tar.bz2";
    sha256 = "sha256-Y87krT+d9vdVIPliJVc/szIVBRA3NNcUDdY9Gc9KpXg=";
  };
  conda-llvmdev = pkgs.fetchurl {
    url = "https://anaconda.org/conda-forge/llvmdev/11.1.0/download/linux-64/llvmdev-11.1.0-hf817b99_2.tar.bz2";
    sha256 = "sha256-vN87BWggPfpFp51Qm60R3D5krQ4AQwiEJaqPfVb6x40=";
  };
in
  pkgs.stdenv.mkDerivation {
    name = "conda-llvmlite";
    src = fake-src;
    buildCommand =
      ''
      HOME=`pwd`
      mkdir $out
      cat << EOF > conda-commands.sh
      set -e

      conda create --prefix ./conda_tmp ${conda-zlib} ${conda-llvm} ${conda-llvm-tools} ${conda-llvmdev}
      conda init
      source .bashrc
      conda activate ./conda_tmp

      conda build --no-anaconda-upload --no-test --output-folder $out $src/fake-conda
      EOF
      ${condaBuilderEnv}/bin/conda-builder-env conda-commands.sh

      mkdir -p $out/nix-support
      echo file conda $out/*/*.tar.bz2 >> $out/nix-support/hydra-build-products
      '';
  }