Help packaging copilotchat-nvim

Hi All,

I’m trying to build a vim plugin that uses remote plugins that require some python packages.

Plugin: GitHub - CopilotC-Nvim/CopilotChat.nvim: Chat with GitHub Copilot in Neovim

{
  vimUtils,
  fetchFromGitHub,
  python3,
  buildEnv,
  lib,
  stdenv,
  makeWrapper,
  ...
}: let
  pname = "copilotchat-nvim";
  version = "1.3.0";
  src = fetchFromGitHub {
    owner = "CopilotC-Nvim";
    repo = "CopilotChat.nvim";
    rev = "v${version}";
    sha256 = "sha256-vbqtaRejGt+xXi1dfzclKL5/ZynEzBJfqXUHO+sC880=";
  };
  meta = {
    description = "Chat with GitHub Copilot in Neovim";
    homepage = "https://github.com/CopilotC-Nvim/CopilotChat.nvim/";
    license = lib.licenses.gpl3;
  };
  # Define the Python environment with the required packages
  pythonEnv = python3.withPackages (ps:
    with ps; [
      python-dotenv
      requests
      prompt-toolkit
      tiktoken
    ]);
  # Build the Vim plugin
  vimPlugin = vimUtils.buildVimPlugin {
    pname = "${pname}-lua";
    inherit src version meta;
    propagatedBuildInputs = [pythonEnv];
  };
in
  buildEnv {
    name = pname;
    paths = [vimPlugin pythonEnv];
    buildInputs = [pythonEnv];
  }

I found something online that I am using as a base. I can get it to run if I add the python packages to my Neovim config in home-manager. But ideally, I would like to have it neatly packaged and self-contained with this nix expression. How can I make the python packages available at run time to the package, and should I even be using buildEnv?

I am currently getting the following error, when I run :UpdateRemotePlugins

Encountered ModuleNotFoundError loading plugin at /nix/store/id11ap5bpk825l8bvnma4hdfr11mwnjx-vimplugin-copilotchat-nvim-lua-1.3.0/rplugin/python3/CopilotC
hat: No module named 'dotenv'
Traceback (most recent call last):
  File "/nix/store/iq776hdh95v2s36z0gqy0sjjinihgg3w-python3-3.11.7-env/lib/python3.11/site-packages/pynvim/plugin/host.py", line 187, in _load
    module = _handle_import(directory, name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/iq776hdh95v2s36z0gqy0sjjinihgg3w-python3-3.11.7-env/lib/python3.11/site-packages/pynvim/plugin/host.py", line 40, in _handle_import
    return importlib.import_module(name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/y027d3bvlaizbri04c1bzh28hqd6lj01-python3-3.11.7/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
ModuleNotFoundError: No module named 'dotenv'

remote/host: python3 host registered plugins []
remote/host: generated rplugin manifest: /home/haseeb/.local/share/nvim/rplugin.vim

Clearly, the plugin does not see the installed python packages :thinking:.

However, if I do the following, it works with my NixVim setup this works.

{
  programs.nixvim = {
    extraPlugins = with pkgs; [

      copilotchat-nvim
    ];

    # For copilot-chat
    extraPython3Packages = p: [
      p.python-dotenv
      p.requests
      p.prompt-toolkit
      p.tiktoken
    ];
 };
}

Link: home-manager/editors/nvim/plugins/ai/default.nix · beb1d483a62d3ecf069a9aacc1ee3176cff19a9a · Haseeb Majid / dotfiles · GitLab

Thanks a ton!

1 Like