Pytest-bdd on shell.nix

Hi,

I'd like to create a simple python project, and the nixos documentation
was helpul. So I have a shell.nix file with the following content:

with import <nixpkgs> {};

(
  python3.withPackages (ps: [ps.ipython ps.flask ps.pytest])
).env

Then I run nix-shell on the project folder and everything seems fine.

The problem I have is that I want to add pytest-bdd but it seems that
isn't packaged for NixOS, as nix search pytest-bdd doesn't return
any results.

So I try behave instead of pytest-bdd because it is already
packaged for NixOS, but unfortunately the behave binary isn't
available, while ipython and flask are. And the behave
documentation stats a binary should be available.

So I decided to try a different approach for pytest-bdd and I
followed the example on https://nixos.org/nixpkgs/manual/#python
(15.17.1.2.1. Packaging a library).

with import <nixpkgs> {};

(
  let
    my_pytestbdd = python37.pkgs.buildPythonPackage rec {
      pname = "pytest-bdd";
      version = "3.2.1";

      src = python37.pkgs.fetchPypi {
        inherit pname version;
        sha256 = "1ibyr40g3p6xbx1m59as3s9spyadz8wyc7zwqyzibphrw4pkvrqp";
      };

      doCheck = false;

      meta = {
        homepage = "https://github.com/pytest-dev/pytest-bdd/";
        description = "BDD library for the py.test runner";
      };
    };

  in python37.withPackages (ps: [ps.ipython ps.flask ps.pytest ps.glob2 my_pytestbdd])
).env

And I'm stuck with this message:

Processing ./pytest_bdd-3.2.1-py2.py3-none-any.whl
Collecting glob2 (from pytest-bdd==3.2.1)
  ERROR: Could not find a version that satisfies the requirement glob2 (from pytest-bdd==3.2.1) (from versions: none)
ERROR: No matching distribution found for glob2 (from pytest-bdd==3.2.1)
builder for '/nix/store/d94madfidxgn5r0k9kivfidn4p2cvyjk-python3.7-pytest-bdd-3.2.1.drv' failed with exit code 1
cannot build derivation '/nix/store/ypc7hpylzvmxx8lmk5cfg8jfhrfalzgn-python3-3.7.5-env.drv': 1 dependencies couldn't be built
error: build of '/nix/store/ypc7hpylzvmxx8lmk5cfg8jfhrfalzgn-python3-3.7.5-env.drv' failed

your python.withPackages … might have glob2, but the derivation that’s trying to add pytest-bdd doesn’t… you’ll want to add propagatedBuildInputs:

      src = python37.pkgs.fetchPypi {
        inherit pname version;
        sha256 = "1ibyr40g3p6xbx1m59as3s9spyadz8wyc7zwqyzibphrw4pkvrqp";
      };

      propagatedBuildInputs = with python37.pkgs [ pytest glob2 Mako parse parse_type py six ];

the other option is to just do python37.withPackages.... which includes virtualenv, create a virtual environment, and then just use pip to install missing packages locally

Hi @jonringer,

and thanks for the response, it worked and I like to avoid virtual environments as much as I can.

Now I’m looking into how to publish pytest-bdd as a python module for nixpkgs, but I don’t know how to test it as the last step on the documentation fails for me:

  1. clone nixpkgs repo
  2. created pkgs/development/python-modules/pytest-bdd/
  3. modified: pkgs/top-level/python-packages.nix
  4. run nix-build -A pytest-bdd

Step 4 fails. It’s true that I declared my module on python-modules.nix instead of all-modules.nix, but I did it because all python modules were declared there.

I wonder if this a good place or should I create a new topic?

documentation: Nixpkgs 23.11 manual | Nix & NixOS

it’s a language specific package, so you need to add it to the related directories, for python this is:

top-level/python-modules.nix # for entering attribute e.g. pythonPackages.package
pkgs/development/python-modules/<package>/default.nix # nix expression

pretty standard module addition example: pythonPackages.mpld3: init at 0.3 by unode · Pull Request #70457 · NixOS/nixpkgs · GitHub

1 Like

and the build command you’re looking for is nix-build -A python3Packages.pytest-bdd

1 Like

Thanks a lot, you’ve been extremely helpful.

I’ve created my first PR for nixpkgs nixos/python: add pytest-bdd module. by jm2dev · Pull Request #72495 · NixOS/nixpkgs · GitHub

gotta start from somewhere :slight_smile:

Hi again,

regarding the PR, once it has been reviewed when do you think it will be merged?

Thanks.