Trying to understand buildpythonPacakge

I’m trying to understand the buildPythonPackage function used to package python libraries,

In my understanding the following code should build:

{
  pkgs ? import <nixpkgs> { },
}:

with pkgs;
with pkgs.python3Packages;
let

  hr = buildPythonPackage rec {
    pname = "hiredis";
    version = "1.0.1";
    src = fetchPypi {
      pname = "hiredis";
      inherit version;
      sha256 = "sha256-qlndY7s/c23k/C0IARRCnV02nfsyZfdxd46DSdZ6l6Q=";
    };

    dependencies = [ ];
    doCheck = false;

    nativeBuildInputs = [ openssl ];
    disabledTests = [ ];
    pythonImportsCheck = [ "hiredis" ];
  };

  hiredisPkg = callPackage hr { };
in
mkShell { buildInputs = [ hiredisPkg ]; }

However it fails with

nix-shell
error:
       … while calling the 'derivationStrict' builtin

         at /builtin/derivation.nix:9:12: (source not available)

       … while evaluating derivation 'nix-shell'
         whose name attribute is located at /nix/store/0qd773b63yg8435w8hpm13zqz7iipcbs-source/pkgs/stdenv/generic/make-derivation.nix:331:7

       … while evaluating attribute 'buildInputs' of derivation 'nix-shell'

         at /nix/store/0qd773b63yg8435w8hpm13zqz7iipcbs-source/pkgs/stdenv/generic/make-derivation.nix:378:7:

          377|       depsHostHost                = elemAt (elemAt dependencies 1) 0;
          378|       buildInputs                 = elemAt (elemAt dependencies 1) 1;
             |       ^
          379|       depsTargetTarget            = elemAt (elemAt dependencies 2) 0;

       error: opening file '/nix/store/bkm7w97sf1rxsm9jvkng1pdsjw2rvcig-python3.11-hiredis-1.0.1/default.nix': No such file or directory

I’m not sure why the builder looks out for default.nix here.

Your hr value is already a “derivation”, it doesn’t need to be callPackaged; callPackage is a dependency injection mechanism, its first argument is a lambda that declares the “inputs” that have to be “injected” in order to “construct” the output; the two are equivalent:

let
  hr1 = buildPythonPackage rec { ... };
  hr2 = callPackage ({ buildPythonPackage, openssl, ... }: buildPythonPackage rec { ... }) { }
in
...

…except that the 2nd will also expose an override function, whereas the former only exposes overridePythonAttrs

2 Likes

Thanks for the explanation, that did the trick.