python38Packages.pynvim does not work when added to packages, works from nix-shell

I’m trying to get the neovim-nightly overlay working with Python support, and I’m confused about why pynvim isn’t accessible when added to home.packages:

$ python
Python 3.8.9 (default, Apr  2 2021, 11:20:07)
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import neovim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'neovim'

But it is accessible when run from nix-shell:

$ nix-shell -p python38Packages.pynvim
[nix-shell:~]$ python
Python 3.8.9 (default, Apr  2 2021, 11:20:07)
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import neovim
>>> neovim
<module 'neovim' from '/nix/store/4hwxwz7ihqkmf4xi0d6g8iy4rr2r3m72-python3.8-pynvim-0.4.3/lib/python3.8/site-packages/neovim/__init__.py'>

Relevant parts of my home-manager config:

  ...
  nixpkgs.overlays = [
    (import (builtins.fetchTarball {
      url = https://github.com/nix-community/neovim-nightly-overlay/archive/master.tar.gz;
    }))
  ];

  home.packages = with pkgs; [
    neovim-nightly

    python38Full
    python38Packages.ipython
    python38Packages.pynvim
    python38Packages.jedi
    python38Packages.python-language-server
    ...

I’d love to be able to run nvim outside of a nix-shell and have Python work. Any ideas what I might be doing wrong?

python3Packages.<pkg> only works with nix-shell because the shellHook will add the package to PYTHONPATH, so python becomes aware that the package is available.

If you’re installing the packages as part of configuration.nix or home.nix, then you will want to use pythonX.withPackages (this is essentially nix’s virutalenv). This will essentially create a site packages for that interpreter which has the packages that you specificy. Please look at https://github.com/NixOS/nixpkgs/blob/29647c9b582338b23577be9f9bddba4295e16111/doc/languages-frameworks/python.section.md#ad-hoc-temporary-python-environment-with-nix-shell-ad-hoc-temporary-python-environment-with-nix-shell for further details.

Ah, thank you @jonringer, I was wondering what the withPackages nuance was and that explains a lot.

1 Like