How to get nvim-treesitter working for LaTeX files

Hello everyone,

I am on Fedora (have se-linux turned off as I got problems during the installation) and I using home-manger. I am trying to get nvim-treesitter working for LaTeX files, however I am not able to.

The nix channel I am using is:

> nix-channel --list
home-manager https://github.com/nix-community/home-manager/archive/master.tar.gz

The relevant part of my home-manager config for neovim is:

  programs.neovim = {
    enable = true;
    vimAlias = true;
    viAlias = true;
    vimdiffAlias = true;
    plugins = with pkgs.vimPlugins; [ 
      (nvim-treesitter.withPlugins (
          plugins: with plugins; [
            tree-sitter-c
            tree-sitter-cpp
            tree-sitter-html
            tree-sitter-latex
            tree-sitter-lua
            tree-sitter-nix
            tree-sitter-python
            tree-sitter-rust
            tree-sitter-toml
          ]
        ))
      coc-clangd
      coc-nvim
      coc-pyright
      coc-vimtex
      neoformat
      vim-commentary
      vim-monokai 
      vimtex 
    ];
    extraConfig = ''
      set relativenumber
      set nu
      set nowrap
      set cursorline
      colorscheme monokai
      syntax off
       
      set hidden
      set nobackup
      set nowritebackup
      set cmdheight=2
      set updatetime=300
      set shortmess+=c
      set signcolumn=yes
 
      let mapleader="ö"
 
      " for vimtex:
      nmap <leader>m :VimtexTocOpen<CR> 
      nmap <leader>v :VimtexView<CR> 
      nmap <leader>c :VimtexCompile<CR> 
     
      let g:vimtex_view_general_viewer='evince'

      " for nvim-treesitter:
      lua <<EOF
      require'nvim-treesitter.configs'.setup {
        ensure_installed = "all", -- one of "all", "maintained" (parsers with maintainers), or a list of languages
        ignore_install = { "javascript" }, -- List of parsers to ignore installing
        highlight = {
          enable = true,              -- false will disable the whole extension
          disable = { "c", "rust" },  -- list of language that will be disabled
          -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
          -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
          -- Using this option may slow down your editor, and you may see some duplicate highlights.
          -- Instead of true it can also be a list of languages
          additional_vim_regex_highlighting = false,
        },
      }
      EOF
    '';
   };


I searched and found similar to my issues. Here are a couple of them:

Furthermore I found that latex.so does not exist on my machine:

> ls ~/.local/share/nvim/site/parser
astro.so*       dart.so*        fusion.so*    heex.so*    lalrpop.so*          pioasm.so*  scheme.so*         turtle.so*
bash.so*        dockerfile.so*  gdscript.so*  help.so*    ledger.so*           prisma.so*  scss.so*           typescript.so*
beancount.so*   dot.so*         gleam.so*     hjson.so*   llvm.so*             pug.so*     slint.so*          vala.so*
bibtex.so*      eex.so*         glimmer.so*   hocon.so*   make.so*             ql.so*      solidity.so*       verilog.so*
clojure.so*     elixir.so*      glsl.so*      http.so*    markdown.so*         query.so*   sparql.so*         vim.so*
cmake.so*       elm.so*         gomod.so*     java.so*    ninja.so*            rasi.so*    supercollider.so*  vue.so*
comment.so*     elvish.so*      go.so*        jsdoc.so*   norg.so*             regex.so*   surface.so*        wgsl.so*
commonlisp.so*  erlang.so*      gowork.so*    json5.so*   ocaml_interface.so*  rego.so*    svelte.so*         yaml.so*
cooklang.so*    fennel.so*      graphql.so*   jsonc.so*   ocaml.so*            r.so*       swift.so*          yang.so*
c_sharp.so*     fish.so*        hack.so*      json.so*    pascal.so*           rst.so*     tlaplus.so*        zig.so*
css.so*         foam.so*        haskell.so*   julia.so*   perl.so*             ruby.so*    todotxt.so*
cuda.so*        fortran.so*     hcl.so*       kotlin.so*  php.so*              scala.so*   tsx.so*

I am not sure how to get this working. Does anyone have a solution for this problem?

I beleive the issue is with the inner with plugins; statement. It needs to reference an attribute set of packaged grammars, with pkgs.tree-sitter-grammars; which has exactly that works.

Additionally you’ll want to remove the ensure_installed = "all" from the plugin config as nix will have taken care of the installation of the grammars. Leaving that config will cause errors as the plugin will try to install them and requires a compiler to do so.

The .so files will end up in the nix store and the path provided to neovim so you wont find them in ~/.local/share/nvim/site/parser anymore, you may even want to delete that directory after you have the nix config working as it may conflict with the nix installed grammars. You can verify the grammars are installed with nix using :TSInstallInfo once you’ve switched to the new configuration.

Hello, I updated my system, completely removed the folder ~/.local/share/nvim and changed my home-manager config to this:

  programs.neovim = {
    enable = true;
    withPython3 = true;
    plugins = with pkgs.vimPlugins; [ 
      # (nvim-treesitter.withPlugins (plugins: pkgs.tree-sitter.allGrammars))
      (nvim-treesitter.withPlugins (
        plugins: with plugins; [
          tree-sitter-c
          tree-sitter-cpp
          tree-sitter-dockerfile
          tree-sitter-fish
          tree-sitter-go
          tree-sitter-html
          tree-sitter-json
          tree-sitter-latex
          tree-sitter-lua
          tree-sitter-nix
          tree-sitter-python
          tree-sitter-r
          tree-sitter-regex
          tree-sitter-rust
          tree-sitter-toml
          tree-sitter-vim
          tree-sitter-yaml
        ]
      ))
      coc-nvim
      coc-pyright
      coc-rust-analyzer
      coc-clangd
      dracula-vim
      nord-nvim
      vimtex
      vim-nix
      vim-fugitive
      vim-commentary
    ];
    extraPackages = with pkgs; [
      (python3.withPackages (ps: with ps; [
        black
        flake8
      ]))
    ];
    extraPython3Packages = (ps: with ps; [
      jedi
    ]);
    extraConfig = ''
      set number
      colorscheme dracula
      syntax off
      :let mapleader = "ö"
      
      " nerd-tree keymapping
      nnoremap <leader>n :NERDTreeToggle<CR>
      nnoremap <C-n> :NERDTree<CR>
      nnoremap <C-f> :NERDTreeFind<CR>
      
      " vimtex key mapping
      nnoremap <leader>m :VimtexTocOpen<CR>
      nnoremap <leader>v :VimtexView<CR>
      nnoremap <leader>cl :VimtexClean<CR>
      nnoremap <leader>co :VimtexCompile<CR>

      " treesitter
      lua << EOF
      require'nvim-treesitter.configs'.setup {
        highlight = {
          enable = true,
        }
      }
      EOF
    '';
  };

Now the I seem to have no issues and everything works as planned. Thanks for the detailed explanation!