How to setup the new nvim-treesitter via home-manager?

Hello.

I’m using home-manager to setup my neovim and all was good until nvim-treesitter changed to a new version (nixpkgsnow tracks the plugin from the main branch, which is not backwards-compatible).

I had this home-manager configuration:

programs.neovim.plugins = with pkgs.vimPlugins; [
  {
    plugin = nvim-treesitter.withAllGrammars;
    type = "lua";
    config =
      ''
        require('nvim-treesitter.configs').setup {
          highlight = {
            enable = true,
          },
          indent = {
            enable = true,
          },
        }
      '';
  }
]

This was enough to bring all grammars for syntax highlight. But this config is broken for the new plugin.

I’m trying to configure the new version, but I’m failing to get proper syntax highlight. This is what I currently have:

programs.neovim.plugins = with pkgs.vimPlugins; [
  {
    plugin = nvim-treesitter
    type = "lua";
    config =
      ''
        -- Setup parsers
        require('nvim-treesitter').install({
          'bash',
          'go',
          -- etc
        })

        -- Highlight
        vim.api.nvim_create_autocmd('FileType', {
          pattern = { '<filetypes>' },
          callback = function() vim.treesitter.start() end,
        })

        -- Fold
        vim.wo[0][0].foldexpr = 'v:lua.vim.treesitter.foldexpr()'
        vim.wo[0][0].foldmethod = 'expr'

        -- Indentation
        vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
      '';
  }
]

I also had to add tree-sitter to environment.systemPackages. Also, :checkhealth looks fine.

This new config is not giving me good syntax highlight, like I had before. What am i missing? How do I setup this new version via home-manager?

treesitter doesn’t support the filetype <filetypes>, and it is unlikely you even have such a filetype.

Please register one autocmd for each filetype you actually use.

I have this function:

And I use it like this:

Thanks for the tips, @NobbZ .

I got it working with this:

      {
        plugin = nvim-treesitter;
        type = "lua";
        config =
          /*
          lua
          */
          ''
          -- The new nvim-treesitter (`main` branch) does not start
          -- automatically. This autocmd starts it and auto-installs the
          -- language parser based on the `filetype`.
          vim.api.nvim_create_autocmd({ 'Filetype' }, {
            callback = function(event)
              -- Make sure nvim-treesitter is available
              local ok, nvim_treesitter = pcall(require, 'nvim-treesitter')
              if not ok then return end

              local parsers = require('nvim-treesitter.parsers')

              if not parsers[event.match] or not nvim_treesitter.install then return end

              local ft = vim.bo[event.buf].ft
              local lang = vim.treesitter.language.get_lang(ft)
              nvim_treesitter.install({ lang }):await(function(err)
                if err then
                  vim.notify('Treesitter install error for ft: ' .. ft .. ' err: ' .. err)
                  return
                end

                pcall(vim.treesitter.start, event.buf)
                vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
                -- vim.wo.foldmethod = 'expr'
                vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
              end)
            end,
          })
          '';
      }

My full nvim config is here for the record.