0

I'm trying to install findiff Python package in NixOS follow this guide.

My pkgs/development/python-modules/findiff/default.nix file is:

{ lib
, buildPythonPackage
, fetchPypi
, pytestCheckHook
, pytest-runner
, numpy
, scipy
, sympy
}:

buildPythonPackage rec {
  pname = "findiff";
  version = "0.9.2";

  src = fetchPypi {
    inherit pname version;
    sha256 = "sha256-IWqQhGv+8d3mYdnwJh1byj0su2c0K5fpMTylsvR4c2U=";
  };

  checkInputs = [
    pytestCheckHook
    pytest-runner
  ];

  propagatedBuildInputs = [
    numpy
    scipy
    sympy
  ];

  meta = with lib; {
    description = "A Python package for finite difference derivatives in any number of dimensions";
    homepage    = "https://github.com/maroba/findiff";
    license     = licenses.mit;
    maintainers = with maintainers; [ "f0ma" ];
  };
}

Package was built successfully as /nix/store/cx78cip77nyb0fiwbqk2v2ddl2jvqc8v-python3.9-findiff-0.9.2 with nix-build -A python39Packages.findiff.

Command nix-env -f . -iA python39Packages.findiff shows: installing 'python3.9-findiff-0.9.2'

But I have no idea how to make this package available for python (per-profile, per-user or system-wide) properly (nix-env?, nix-shell?):

>>> import findiff
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'findiff'

Only this dirty way works:

>>> import sys
>>> sys.path.append("/nix/store/cx78cip77nyb0fiwbqk2v2ddl2jvqc8v-python3.9-findiff-0.9.2/lib/python3.9/site-packages")
>>> import findiff
Stanislav Ivanov
  • 1,854
  • 1
  • 16
  • 22
  • One would generally refer to `python39.withPackages (p: [p.findiff])`. Hopefully you _don't_ use nix-env but set up your environment declaratively, and thus can specify arbitrary expressions; personally, I use https://gist.github.com/lheckemann/402e61e8e53f136f239ecd8c17ab1deb (though with flakes being the shiny new way to do things, it's smelling increasingly outdated). – Charles Duffy Aug 14 '22 at 23:28
  • that said, if you've installed `python39Packages.findiff`, you should have a `~/.nix-profile/lib/python3.9/site-packages` entry for `findiff` that symlinks into the nix store, removing any need to refer to the store path directly. – Charles Duffy Aug 14 '22 at 23:29
  • @CharlesDuffy I have `findiff` installed in `~/.nix-profile/lib/python3.9/site-packages/findiff` but I have no `~/.nix-profile/lib/python3.9/` in `sys.path`. Should I add this path explicitly? – Stanislav Ivanov Aug 17 '22 at 18:14
  • _Ideally_, you'd use `python.withPackages` to get a Python interpreter that _does_ have findiff in the default `sys.path`. But if you don't want to do that, you certainly _can_ add the relevant `~/.nix-profile/...` location. – Charles Duffy Aug 17 '22 at 19:25

1 Answers1

2

I found that I can import package description from file findiff.nix (copy of pkgs/development/python-modules/findiff/default.nix) with nixpkgs.overlays in my configuration.nix:

  nixpkgs.overlays = [ (self: super: {
                         findiff = super.callPackage ./findiff.nix {
                             buildPythonPackage = super.python39Packages.buildPythonPackage;
                             fetchPypi = super.python39Packages.fetchPypi;
                             numpy = super.python39Packages.numpy;
                             scipy = super.python39Packages.scipy;
                             sympy = super.python39Packages.sympy;
                             pytestCheckHook = super.python39Packages.pytestCheckHook;
                             pytest-runner = super.python39Packages.pytest-runner;
                         }; } ) ];

After that I can add package in installation list:

(python39.withPackages (p: with p; [
      requests
      numpy
      scipy
      matplotlib
      cartopy
      sympy
      pytest-runner
      findiff # New package
]))

I am not sure that it is a right solution but in works for me.

Stanislav Ivanov
  • 1,854
  • 1
  • 16
  • 22