简体   繁体   中英

How can I add a python package to a shell.nix, if it's not in nixpkgs?

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?

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.

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