How can I get torch-geometric to work using Nix?
I am trying to get the Python package torch-geometric to work using Nix (I am on NixOS). Currently, I use mach-nix to try and setup a Python environment. However, the difficulty is that some of the dependencies should be downloaded from a separate file server (not pypi), i.e. https://pytorch-geometric.com/whl/torch-1.8.0+cpu.html. I am first trying to setup an environment containing a single torch-geometric dependency: torch-sparse.
Currently I have the following shell.nix:
{ pkgs ? import &lt;nixpkgs&gt; {} }:

let
  mach-nix = import (builtins.fetchGit {
    url = &quot;https://github.com/DavHau/mach-nix/&quot;;
    ref = &quot;refs/tags/3.3.0&quot;;
  }) {
    python = &quot;python38&quot;;
  };
  sparse = mach-nix.buildPythonPackage {
    pname = &quot;torch_sparse&quot;;
    version = &quot;0.6.9&quot;;
    requirements = ''
      torch
      scipy
      pytest
      pytest-cov
      pytest-runner
    '';
    src = builtins.fetchGit {
      url = &quot;https://github.com/rusty1s/pytorch_sparse&quot;;
      ref = &quot;refs/tags/0.6.9&quot;;
    };
  };
in mach-nix.mkPython {
  requirements = &quot;torch-sparse&quot;;
  packagesExtra = [
    sparse
  ];
}

Which, upon running nix-shell, fails with the following error message:
running build_ext
error: [Errno 2] No such file or directory: 'which'
builder for '/nix/store/fs9nrrd2a233xp5d6njy6639yjbxp4g0-python3.8-torch_sparse-0.6.9.drv' failed with exit code 1

I tried adding the which package to either checkInputs and buildInputs, but that does not solve the problem. Evidently, I try to build the package directly from its GitHub repo, as I am unsure on how to reference a wheel package in mach-nix. I am relatively new to the NixOS environment, and, quite frankly, I am completely lost.
How should I go about installing a Python package such as torch-sparse or torch-geometric? Am I even using the correct tools?

I have managed to come up with a working Nix expression. I will leave the answer here for future reference. Running the following expression using nix-shell will create a shell with torch-1.8.0 and torch-geometric-1.7.0 and their required dependencies.
{ pkgs ? import &lt;nixpkgs&gt; { } }:

let
  python = pkgs.python38;
  pytorch-180 = let
    pyVerNoDot = builtins.replaceStrings [ &quot;.&quot; ] [ &quot;&quot; ] python.pythonVersion;
    unsupported = throw &quot;Unsupported system&quot;;
    version = &quot;1.8.0&quot;;
  in python.pkgs.buildPythonPackage {
    inherit version;

    pname = &quot;pytorch&quot;;

    format = &quot;wheel&quot;;

    src = pkgs.fetchurl {
      name = &quot;torch-${version}-cp38-cp38-linux_x86_64.whl&quot;;
      url =
        &quot;https://download.pytorch.org/whl/cu111/torch-${version}%2Bcu111-cp38-cp38-linux_x86_64.whl&quot;;
      hash = &quot;sha256-4NYiAkYfGXm3orLT8Y5diepRMAg+WzJelncy2zJp+Ho=&quot;;
    };

    nativeBuildInputs = with pkgs; [ addOpenGLRunpath patchelf ];

    propagatedBuildInputs = with python.pkgs; [
      future
      numpy
      pyyaml
      requests
      typing-extensions
    ];

    postInstall = ''
      # ONNX conversion
      rm -rf $out/bin
    '';

    postFixup = let rpath = pkgs.lib.makeLibraryPath [ pkgs.stdenv.cc.cc.lib ];
    in ''
      find $out/${python.sitePackages}/torch/lib -type f \( -name '*.so' -or -name '*.so.*' \) | while read lib; do
        echo &quot;setting rpath for $lib...&quot;
        patchelf --set-rpath &quot;${rpath}:$out/${python.sitePackages}/torch/lib&quot; &quot;$lib&quot;
        addOpenGLRunpath &quot;$lib&quot;
      done
    '';

    pythonImportsCheck = [ &quot;torch&quot; ];

    meta = with pkgs.lib; {
      description =
        &quot;Open source, prototype-to-production deep learning platform&quot;;
      homepage = &quot;https://pytorch.org/&quot;;
      changelog = &quot;https://github.com/pytorch/pytorch/releases/tag/v${version}&quot;;
      license = licenses.unfree; # Includes CUDA and Intel MKL.
      platforms = platforms.linux;
      maintainers = with maintainers; [ danieldk ];
    };
  };
  sparse = with python.pkgs;
    buildPythonPackage rec {
      pname = &quot;torch_sparse&quot;;
      version = &quot;0.6.9&quot;;

      src = pkgs.fetchurl {
        name = &quot;${pname}-${version}-cp38-cp38-linux_x86_64.whl&quot;;
        url =
          &quot;https://pytorch-geometric.com/whl/torch-1.8.0+cpu/${pname}-${version}-cp38-cp38-linux_x86_64.whl&quot;;
        hash = &quot;sha256-6dmZNQ0FlwKdfESKhvv8PPwzgsJFWlP8tYXWu2JLiMk=&quot;;
      };

      format = &quot;wheel&quot;;

      propagatedBuildInputs = [ pytorch-180 scipy ];
      # buildInputs = [ pybind11 ];
      # nativeBuildInputs = [ pytest-runner pkgs.which ];

      doCheck = false;

      postInstall = ''
        rm -rf $out/${python.sitePackages}/test
      '';
    };
  scatter = with python.pkgs;
    buildPythonPackage rec {
      pname = &quot;torch_scatter&quot;;
      version = &quot;2.0.7&quot;;

      src = pkgs.fetchurl {
        name = &quot;${pname}-${version}-cp38-cp38-linux_x86_64.whl&quot;;
        url =
          &quot;https://pytorch-geometric.com/whl/torch-1.8.0+cpu/${pname}-${version}-cp38-cp38-linux_x86_64.whl&quot;;
        hash = &quot;sha256-MRoFretgyEpq+7aJZc0399Kd+f28Uhn5+CxW5ZIKwcg=&quot;;
      };

      format = &quot;wheel&quot;;

      propagatedBuildInputs = [ pytorch-180 ];

      doCheck = false;

      postInstall = ''
        rm -rf $out/${python.sitePackages}/test
      '';
    };
  cluster = with python.pkgs;
    buildPythonPackage rec {
      pname = &quot;torch_cluster&quot;;
      version = &quot;1.5.9&quot;;

      src = pkgs.fetchurl {
        name = &quot;${pname}-${version}-cp38-cp38-linux_x86_64.whl&quot;;
        url =
          &quot;https://pytorch-geometric.com/whl/torch-1.8.0+cpu/${pname}-${version}-cp38-cp38-linux_x86_64.whl&quot;;
        hash = &quot;sha256-E2nywtiZ7m7VA1J7AY7gAHYvyN9H3zl/W0/WsZLzwF8=&quot;;
      };

      format = &quot;wheel&quot;;

      propagatedBuildInputs = [ pytorch-180 ];

      doCheck = false;

      postInstall = ''
        rm -rf $out/${python.sitePackages}/test
      '';
    };
  spline = with python.pkgs;
    buildPythonPackage rec {
      pname = &quot;torch_spline_conv&quot;;
      version = &quot;1.2.1&quot;;

      src = pkgs.fetchurl {
        name = &quot;${pname}-${version}-cp38-cp38-linux_x86_64.whl&quot;;
        url =
          &quot;https://pytorch-geometric.com/whl/torch-1.8.0+cpu/${pname}-${version}-cp38-cp38-linux_x86_64.whl&quot;;
        hash = &quot;sha256-ghSzoxoqSccPAZzfcHJEPYySQ/KYqQ90mFsOdt1CjUw=&quot;;
      };

      format = &quot;wheel&quot;;

      propagatedBuildInputs = [ pytorch-180 ];

      doCheck = false;

      postInstall = ''
        rm -rf $out/${python.sitePackages}/test
      '';
    };
  python-louvain = with python.pkgs;
    buildPythonPackage rec {
      pname = &quot;python-louvain&quot;;
      version = &quot;0.15&quot;;

      src = fetchPypi {
        inherit pname version;
        sha256 = &quot;1sqp97fwh4asx0jr72x8hil8z8fcg2xq92jklmh2m599pvgnx19a&quot;;
      };

      propagatedBuildInputs = [ numpy networkx ];

      doCheck = false;
    };
  googledrivedownloader = with python.pkgs;
    buildPythonPackage rec {
      pname = &quot;googledrivedownloader&quot;;
      version = &quot;0.4&quot;;

      src = fetchPypi {
        inherit pname version;
        sha256 = &quot;0172l1f8ys0913wcr16lzx87vsnapppih62qswmvzwrggcrw2d2b&quot;;
      };

      doCheck = false;
    };
  geometric = with python.pkgs;
    buildPythonPackage rec {
      pname = &quot;torch_geometric&quot;;
      version = &quot;1.7.0&quot;;

      src = fetchPypi {
        inherit pname version;
        sha256 = &quot;1a7ym34ynhk5gb3yc5v4qkmkrkyjbv1fgisrsk0c9xay66w7nwz9&quot;;
      };

      propagatedBuildInputs = [
        pytorch-180
        numpy
        scipy
        tqdm
        networkx
        scikit-learn
        requests
        pandas
        rdflib
        jinja2
        numba
        ase
        h5py
        python-louvain
        googledrivedownloader
      ];
      nativeBuildInputs = [ pytest-runner ];

      doCheck = false;

      # postInstall = ''
      #   rm -rf $out/${python.sitePackages}/test
      # '';
    };
  python-with-pkgs = python.withPackages
    (ps: with ps; [ pytorch-180 scatter sparse cluster spline geometric ps ]);
in pkgs.mkShell { buildInputs = [ python-with-pkgs ]; }

