How to: compile Treesitter grammars for Neovim in NixOS

hallettj@beehaw.org to nixos@lemmy.ml – 10 points –

Instead of getting plugins through nixpkgs I prefer to use my neovim-specific plugin manager. (In my case that's lazy.nvim.) Mostly this works without problems - but some setup is required when a plugin needs to compile something. The plugin that has given me the most trouble is Treesitter which wants to compile grammars. Here is how I got that working.

tl;dr: Configure Treesitter to compile grammars with gcc instead of clang.

As has been reported in https://github.com/nvim-treesitter/nvim-treesitter/issues/1449 Treesitter will try to use clang to compile Treesitter grammars, and on NixOS for some reason clang is not able to locate necessary C++ libraries. The fix that works for me is to configure Treesitter to use gcc instead. Here is the relevant part of my plugin config:

return {
  'nvim-treesitter/nvim-treesitter',
  build = ':TSUpdate',
  config = function()
    -- Set compiler to get grammar installation working in NixOS. See
    -- https://github.com/nvim-treesitter/nvim-treesitter/issues/1449
    require('nvim-treesitter.install').compilers = { 'gcc' }
    require('nvim-treesitter.configs').setup {
      ensure_installed = 'all', -- "all", or list of languages
      ignore_install = { 't32' }, -- t32 is failing to download for me
    }
  end,
}

I still had a problem with the t32 grammar, but I don't need that one so I disable it.

Of course you need to make sure that gcc is available. You could put it in your user profile, but I prefer to make sure by using the extraPackages option from Home Manager's neovim module. Here's my full config:

programs.neovim = {
  enable = true;
  defaultEditor = true;
  withPython3 = true;
  extraPackages = with pkgs; [
    fd
    gh # for github integration
    ripgrep

    # needed to compile fzf-native for telescope-fzf-native.nvim
    gcc
    gnumake

    # language servers
    nil # Nix LSP
    lua-language-server

    nixpkgs-fmt # I have nil configured to call this for formatting
  ];
};
0

No comments yet. You could be first!