NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.08k stars 14.13k forks source link

neovim and coc-python: no python interpreter #98166

Closed SomeoneSerge closed 3 years ago

SomeoneSerge commented 4 years ago

Describe the bug

A clear and concise description of what the bug is.

The neovim with vimPlugins."coc-python" in either configure.packages.myVimPackages.start or plug.plugins doesn't bring python into scope, resulting in

To Reproduce Steps to reproduce the behavior:

  1. Either home-manager with neovim.plugins = [coc-nvim coc-python]
  2. Or shell.nix with
  with import <nixpkgs> {};

  let
      neovimPythonPackages = p: with p; [
                  jedi
                  flake8
                  black
                  pylint
                 ];
      neovim = pkgs.neovim.override {
                          extraPython3Packages = neovimPythonPackages;
                          withPython3 = true;
                          withNodeJs = true;
                          configure = {
                              customRC = builtins.readFile ~/.config/nixpkgs/nvim-init-template.vim;
                              plug.plugins = with pkgs.vimPlugins;  [
                                  coc-nvim
                                  coc-python
                              ];
                          };
                     };
  in
  mkShell {
      buildInputs = [neovim];
  }
  1. Execute nix-shell in the directory with shell.nix (or home-manager switch)
  2. Visit any python file, nvim example.py
  3. Check if python is in PATH: :! which python
  4. Check if e.g. node is in PATH: :! which node

Expected behavior

A clear and concise description of what you expected to happen.

  1. Editor opens normally
  2. Possibly, a path to python (not sure if it should be available with :!, but regardless)
  3. Possibly, a path to node

Observed behaviour

If applicable, add screenshots to help explain your problem.

  1. A popup suggesting to download pylint. An error reporting "No Python interpreter"
  2. "no python in $PATH"
  3. A path to node

Additional context Add any other context about the problem here.

Notify maintainers

@vcunat (found in vim-plugins/overrides.nix)

Metadata Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

 - system: `"x86_64-linux"`
 - host os: `Linux 5.4.62, NixOS, 20.09pre242769.61525137fd1 (Nightingale)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.3.7`
 - channels(serge): `"home-manager, nixpkgs-21.03pre243200.c413f10dafa"`
 - channels(root): `"nixos-20.09pre242769.61525137fd1"`
 - nixpkgs: `/home/serge/.nix-defexpr/channels/nixpkgs`

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute: [pkgs.vimPlugins.coc-python, pkgs.neovim]
# a list of nixos modules affected by the problem
module:
SomeoneSerge commented 4 years ago

Upd: found that python executable is exposed to nvim, as it is supposed to, through g:python3_host_prog and not PATH:

:echo g:python3_host_prog
/nix/store/gsb3ahrqpd99i4frbrsdb2br3chi88gl-neovim-0.4.4/bin/nvim-python3

Apparently the solution would be to allow to populate the coc-settings.json with python.linting.pylintPath, etc.

I temporarily resorted to the following hack and it kind of works:

# home.nix
  xdg.configFile."nvim/coc-settings.json".text = ''
    {
      "python.linting.pylintPath": "nix-shell -p 'python3.withPackages(ps: [ps.pylint])' --run 'python -m pylint'"
    }
  '';

The UX I'd expect is that adding coc-python to neovim plugins would automatically include pylint, black, isort, etc in the closure and that neovim would be exposed to their executables. I could work on that some time later, but I wouldn't know how to approach the problem. I find nixpkgs somewhat overwhelming

carletes commented 4 years ago

I'm just a NixOS newbie, so I might be doing something very wrong here, but for black and flake8 the following works for me:

# configuration.nix

home-manager = {
  # ..
  programs = {
    neovim = {
      extraPackages = with pkgs; [
        (python3.withPackages (ps: with ps; [
           black
            flake8
        ]))
      ];

This seems to install black and flake8 and expose them in the PATH to the Neovim process, so you would not need your xdg.configFile hack.

I still have not managed to get jedi working, though: Python complains about module jedi not being found, even when I add it to the extraPython3Packages option.

t184256 commented 4 years ago

@carletes, modules from extraPython3Packages ends up available to nvim-python3. Try to make sure it's the one you invoke (e.g., as nvim-python3 -m jedi).

SomeoneSerge commented 4 years ago

@carletes for myself, I hotfixed jedi by explicitly providing path to its site-packages: https://github.com/newkozlukov/dotfiles/blob/7058ff268c6d802621135831b5271ecc781a7c2c/home/program/neovim/coc.nix#L5 (here pylinters comes from https://github.com/newkozlukov/dotfiles/blob/7058ff268c6d802621135831b5271ecc781a7c2c/overlays/pylinters.nix#L33 but probably most of this terrible messy hack is unnecessary; I just don't have time to clean it up)

I didn't check @t184256's solution now, but when I just opened this issue, extraPython3Packages didn't work (at least for me, at least with jedi)

Also, didn't know about nvim-python3, will look into where's it constructed and how - later

Thanks

carletes commented 4 years ago

Try to make sure it's the one you invoke (e.g., as nvim-python3 -m jedi).

@t184256: Thanks for the hint! Yes, I did a /nix/store/<blahblah>/nvim-python3 -m jedi in another terminal and it worked (as in nvim-python3 as able to locate the jedi module).

All I had to do then was to set python.pythonPath to pnvim-python3 in my coc-settings.json and then everything works.

For black and flake8 I made them accessible to nvim's PATH by adding them toextraPackages`, and that seems to do the trick.

To sum up: The following configuration (I use home-manager driven from /etc/nixos/configuration.nix) works for me:

# configuration.nix
{
  home-manager = {
    programs = {
      neovim = {
        enable =true;
        withPython3 = true;
        plugins = with pkgs.vimPlugins; [
          coc-nvim
          coc-python
        ];
        extraPackages = with pkgs; [
          (python3.withPackages (ps: with ps; [
            black
            flake8
          ]))
        ];
        extraPython3Packages = (ps: with ps; [
          jedi
        ]);
      };
    };
  };
  xdg.configFile."nvim/coc-settings.json".text = builtins.readFile ./my-coc-settings.json;
}

And here are the relevant bits of my-coc-settings.json:

{
  "python.formatting.provider": "black",
  "python.jediEnabled": true,
  "python.linting.flake8Enabled": true,
  "python.pythonPath": "nvim-python3"
}
stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

HT-Moh commented 3 years ago

works for me let g:python3_host_prog='/usr/bin/python3' (add to your vimrc)

SomeoneSerge commented 3 years ago

@HT-Moh I'm almost sure that just withPython3 started working since some commit a rather long time ago - the issue can be probably closed already

Also, I recently switched to neovim-nightly with nvim-lspconfig instead of coc and I feel happy

evanreichard commented 1 year ago

I know this is outdated but a search dropped me here and I wanted to document the fix that worked for me for those in the future. (NixOS newbie as well)

Turns out in 2022 coc-python is archived and you should be using coc-pyright. I didn't have to play with extraPython3Packages or extraPackages at all. Once I replaced coc-python with coc-pyright all was good.