简体   繁体   中英

Trying to add NixOS non packaged pytest-bdd to a shell.nix environment

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

Solution

So after plenty of hours navigating documentation, I managed to get a working solution that I\'d like to share to avoid people the same pain I experienced.

This is my shell.nix file:

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";
      };

      buildInputs = [ python37.pkgs.glob2 python37.pkgs.parse python37.pkgs.six 
python37.pkgs.py python37.pkgs.parse-type python37.pkgs.Mako python37.pkgs.pytest ];

      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 my_pytestbdd])
).env

And running nix-shell successfully created the environment.

if you're happy to just use a requirements.txt file, you can bend nix to work the normal way with a virtualenv, see the default.nix below:

with import <nixpkgs> {};
with pkgs.python37Packages;

stdenv.mkDerivation {
  name = "impurePythonEnv1d";
  buildInputs = [
    taglib
    openssl
    git
    libxml2
    libzip
    python37Full
    python37Packages.virtualenv
    stdenv
    libffi
    zlib ];
  src = null;
  shellHook = ''
    unset http_proxy
    export GIT_SSL_CAINFO=/etc/ssl/certs/ca-bundle.crt
    SOURCE_DATE_EPOCH=$(date +%s)
    virtualenv --no-setuptools venv
    source venv/bin/activate
    pip install jedi
    pip install ipython==5.2.0
    pip install pylint
    pip install flake8
    pip install -r requirements.txt
  '';
}

then create a requirements.txt and nix-shell it up

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM