5

I have a shell.nix that I use for Python development that looks like this:

with import <nixpkgs> {};

((
python37.withPackages (ps: with ps; [
  matplotlib
  spacy
  pandas
  spacy_models.en_core_web_lg
  plotly
])).override({ignoreCollisions=true; })).env

It works fine for these packages. The problem is, I also want to use colormath, which doesn't seem to be in nixpkgs. How can I import that package?

I can generate a requirements.nix with pypi2nix -V python3 -e colormath, and I've tried to import it with something like this:

with import <nixpkgs> {};

let colormath = import ./requirements.nix { inherit pkgs; }
in ((
python37.withPackages (ps: with ps; [
  matplotlib
  spacy
  ...
  colormath
])).override({ignoreCollisions=true; })).env

Edit: here's a gist of the output of requirements.nix.

I've also tried to make a python package nix expression, as in Nixpkgs, and it seems to build OK:

{ buildPythonPackage
, fetchPypi
, networkx
, numpy
, lib
, pytest
}:

buildPythonPackage rec {
  pname = "colormath";
  version = "3.0.0";

  src = fetchPypi {
    inherit version;
    inherit pname;
    sha256 = "3d4605af344527da0e4f9f504fad7ddbebda35322c566a6c72e28edb1ff31217";
  };

  checkInputs = [ pytest ];

  checkPhase = ''
    pytest
  '';

  # Tests seem to hang
  # doCheck = false;

  propagatedBuildInputs = [ networkx numpy ];

  meta = {
    homepage = "https://github.com/gtaylor/python-colormath";
    license = lib.licenses.bsd2;
    description = "Color math and conversion library.";
  };
}

(I even made a pull request for it.) But I just don't know how to import this into my development environment.

I'm still off. Is there an easy way to combine nixpkgs and non-nixpkgs python modules?

Jonathan
  • 10,571
  • 13
  • 67
  • 103
  • 1
    What errors are you getting? Or what about your approach mentioned do you think is "not easy"? – Chris Stryczynski Jan 04 '20 at 20:55
  • I'm getting `error: anonymous function at /colormath.nix:1:1 called without required argument 'buildPythonPackage', at /nix/store/3ncjqs079xp7isfxsq2rfbsxhrk0ax3n-nixpkgs-20.03pre203529.e8f2764fe8d/nixpkgs/lib/customisation.nix:69:16 (use '--show-trace' to show detailed location information)` I was hoping it'd just be something like: `python37.withPackages ... spacy pandas pypi.colormath`. – Jonathan Jan 05 '20 at 00:56
  • Can you show the output of requirement.nix? I think you're just missing one parameter. According to https://github.com/nix-community/pypi2nix. possibly (colormath.colormath). – Chris Stryczynski Jan 05 '20 at 09:31
  • I added a gist of requirements.nix, but I think the issue is something like, pypi2nix is creating a new python environment with pypi packages, and that's invisible to the environment I'm creating with `python37.withPackages`, maybe. – Jonathan Jan 05 '20 at 16:31

1 Answers1

5

I did solve a similar problem like that:

with import <nixpkgs> {};

( let
    colormath = pkgs.python37Packages.buildPythonPackage rec {
      pname = "colormath";
      version = "3.0.0";

      src = pkgs.python37Packages.fetchPypi{
        inherit version;
        inherit pname;
        sha256 = "05qjycgxp3p2f9n6lmic68sxmsyvgnnlyl4z9w7dl9s56jphaiix";
      };

      buildInputs = [ pkgs.python37Packages.numpy pkgs.python37Packages.networkx ];
    };

  in pkgs.python37.buildEnv.override rec {
    extraLibs = [
      pkgs.python37
      pkgs.python37Packages.matplotlib
      pkgs.python37Packages.spacy
      pkgs.python37Packages.pandas
      pkgs.python37Packages.spacy_models.en_core_web_lg
      pkgs.python37Packages.plotly
      colormath
    ];
  }
).env

There is probably room for improvements but this worked for me.

tlo
  • 1,571
  • 1
  • 25
  • 38