Can't Get Neovim Treesitter to Work

Hi! I am configuring Neovim from scratch on NixOS and having trouble getting Treesitter to work. I am aware of NixVim, but I am trying to get lazy.nvim to work because I use machines where I am not able to use Nix. Here are my configurations:

# home-manager.nix
{pkgs, ...}: {
  programs.neovim = {
    enable = true;

    plugins = with pkgs.vimPlugins; [
      lazy-nvim
    ];
  };
}
-- init.lua
vim.g.mapleader = " "
vim.opt.termguicolors = true
vim.opt.guicursor = ""
vim.cmd("set expandtab")
vim.cmd("set tabstop=2")
vim.cmd("set softtabstop=2")
vim.cmd("set shiftwidth=2")
vim.cmd("set number")
vim.cmd("set relativenumber")

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"https://github.com/folke/lazy.nvim.git",
		"--branch=stable",
		lazypath,
	})
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
	spec = {
		{ import = "plugins" },
	},
})

require("nvim-treesitter.configs").setup({
	ensure_installed = { "all" },
	highlight = {
		enable = true,
	},
})

vim.cmd.colorscheme("catppuccin")
-- treesitter.lua
return {
	{
		"nvim-treesitter/nvim-treesitter",
		branch = "master",
		lazy = false,
		build = ":TSUpdate",
	},
}

And this is the error I’m getting:

Error detected while processing /home/<username>/Dots/.config/nvim/init.lua:
E5113: Error while calling lua chunk: /home/<username>/Dots/.config/nvim/init.lua:30: module 'nvim-treesitter.configs' not found:
        no field package.preload['nvim-treesitter.configs']
        cache_loader: module 'nvim-treesitter.configs' not found
        cache_loader_lib: module 'nvim-treesitter.configs' not found
        no file '/nix/store/dzqf1rfxpi24npgnb8k5831s17li4748-luajit-2.1.1741730670-env/share/lua/5.1/nvim-treesitter/configs.lua'
        no file '/nix/store/dzqf1rfxpi24npgnb8k5831s17li4748-luajit-2.1.1741730670-env/share/lua/5.1/nvim-treesitter/configs/init.lua'
        no file '/home/<username>/.local/share/nvim/lazy-rocks/telescope.nvim/share/lua/5.1/nvim-treesitter/configs.lua'
        no file '/home/<username>/.local/share/nvim/lazy-rocks/telescope.nvim/share/lua/5.1/nvim-treesitter/configs/init.lua'
        no file '/nix/store/dzqf1rfxpi24npgnb8k5831s17li4748-luajit-2.1.1741730670-env/lib/lua/5.1/nvim-treesitter/configs.so'
        no file '/home/<username>/.local/share/nvim/lazy-rocks/telescope.nvim/lib/lua/5.1/nvim-treesitter/configs.so'
        no file '/home/<username>/.local/share/nvim/lazy-rocks/telescope.nvim/lib64/lua/5.1/nvim-treesitter/configs.so'
        no file '/nix/store/dzqf1rfxpi24npgnb8k5831s17li4748-luajit-2.1.1741730670-env/lib/lua/5.1/nvim-treesitter.so'
        no file '/home/<username>/.local/share/nvim/lazy-rocks/telescope.nvim/lib/lua/5.1/nvim-treesitter.so'
        no file '/home/<username>/.local/share/nvim/lazy-rocks/telescope.nvim/lib64/lua/5.1/nvim-treesitter.so'
stack traceback:
        [C]: in function 'require'
        /home/<username>/Dots/.config/nvim/init.lua:30: in main chunk

I have seen a comment on Reddit where a guy installed Treesitter in his Home Manager configuration and then did some symlink shenanigans, but I couldn’t get that to work, even after taking code straight from his repository.

Any help with getting this to work using lazy.nvim will be greatly appreciated. Please let me know if I should provide any other information.

You could add nvim-treesitter.withAllGrammars to your plugins list and then it should work.

Thanks for your reply! I have tried that and it made no difference.

Edit: It has made a difference. I am getting a different error now:

Error detected while processing /home/<username>/Dots/.config/nvim/init.lua:
Could not create parser dir ' /nix/store/spxvfb3ssjqxim4rxvg8hriylgjyzira-vim-pack-dir/pack/myNeovimPackages/start/nvim-treesitter/pars
er ':  Vim:E739: Cannot create directory /nix/store/spxvfb3ssjqxim4rxvg8hriylgjyzira-vim-pack-dir/pack/myNeovimPackages/start/nvim-tree
sitter/parser: read-only file system

You could look at nixCats at nixCats-nvim/templates/LazyVim at main · BirdeeHub/nixCats-nvim · GitHub . You could share your config with Nix and non-nix machines.

Update

TL;DR No perfect solution yet, but I’m closer. Will mark the ultimate solution as the solution of the thread.

I have managed to get Treesitter to work. It had nothing to do with my Home Manager configuration. The issue was that Treesitter was trying to install parsers into the Nix store, which cannot do. All I had to do was to change the parsers’ install directory:

-- treesitter.lua
return {
	{
		"nvim-treesitter/nvim-treesitter",
		build = ":TSUpdate",
		config = function()
			require("nvim-treesitter.configs").setup({
				parser_install_dir = vim.fn.stdpath("data") .. "/lazy/nvim-treesitter/",
				ensure_installed = { "lua" },
				sync_install = false,
				auto_install = false,
				highlight = {
					enable = true,
					additional_vim_regex_highlighting = false,
				},
				indent = {
					enable = true,
				},
			})
		end,
	},
}

Treesitter works as expected; I am experiencing a different issue, however: Treesitter shows the compilation messages every time I start Neovim. I’m not sure if that’s normal or not. It’s a minor issue, but I’ll keep looking for a solution and will keep the thread updated and I will mark the final comment as the solution.