如果 python 包不在 nixpkgs 中,我如何将它添加到 shell.nix?

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

我有一个 shell.nix 用于 Python 开发,如下所示:

with import <nixpkgs> {};

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

它适用于这些软件包。问题是,我也想用colormath,nixpkgs里好像没有。我怎样才能导入那个包?

我可以用 pypi2nix -V python3 -e colormath 生成一个 requirements.nix,我试过用这样的东西导入它:

with import <nixpkgs> {};

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

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

我也试过制作一个 python 包 nix 表达式,就像在 Nixpkgs 中一样,它似乎构建正常:

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

(我什至为此提出了拉取请求。)但我只是不知道如何将其导入我的开发环境。

我还在休息。有没有一种简单的方法来组合 nixpkgs 和非 nixpkgs python 模块?

我确实解决了类似的问题:

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

可能还有改进的余地,但这对我有用。