Missing runtime dependency with nix develop

Hi there,
I’m trying to get my head around nix develop for setting up Python development environments. At the moment, I’m just trying to get the html-to-json package to work. Following the wiki, I have come up with the following:

# shell.nix
# See https://wiki.nixos.org/wiki/Python
let
  pkgs = import <nixpkgs> {};

  python = pkgs.python3.override {
    self = python;
    packageOverrides = pyfinal: pyprev: {
      html-to-json = pyfinal.callPackage ./html-to-json.nix { };
    };
  };
in pkgs.mkShell {
  packages = [
    (python.withPackages (python-pkgs: [
      # select Python packages here
      python-pkgs.beautifulsoup4
      python-pkgs.html-to-json
    ]))
  ];
}
# html-to-json.nix
{
  lib,
  buildPythonPackage,
  fetchPypi,
  setuptools,
  wheel,
  beautifulsoup4
}:

buildPythonPackage rec {
  pname = "html-to-json";
  version = "2.0.0";

  src = fetchPypi {
    inherit version;
    pname = "html_to_json";
    sha256 = "P8hI9AYY9ET46ZcfiKIv7wQdDLRWlGTeAY3Pjjw3Zp4=";
  };

  # Ensure required dependencies are included
  propagatedBuildInputs = [ beautifulsoup4 ];

  # do not run tests
  doCheck = false;

  # specific to buildPythonPackage, see its reference
  pyproject = true;
  build-system = [
    setuptools
    wheel
  ];
}

When I run nix develop -f shell.nix, I get the error

> Checking runtime dependencies for html_to_json-2.0.0-py2.py3-none-any.whl
       >   - bs4 not installed

I’m a bit confused by this, because

  1. bs4 (beautifulsoup4) is included in propagatedBuildInputs, which I thought was where you’re meant to put dependencies.
  2. When I comment out the python-pkgs.html-to-json, I am rewarded with a working shell, which has bs4 available to import in Python.

I’m at a bit of a loss at this point, so any help would be appreciated! Maybe I’m just dumb, but this seems like a lot of work just to get python up and running :sweat_smile:

It’s not finding the dependency because we have beautifulsoup4 instead of bs4, so we must:

# html-to-json.nix
postPatch = ''
  substituteInPlace setup.py \
    --replace-fail "bs4" "beautifulsoup4"
'';
$ nix-shell
$ python -c "import html_to_json" && echo Success!
Success!

Also, this is not related to the issue, but you should use dependencies instead of propagatedBuildInputs (see buildPythonPackage parameters - nixpkgs/doc/languages-frameworks/python.section.md)

Works like a charm, thank you!

1 Like