b-src / lazy-nix-helper.nvim

Neovim plugin allowing a single neovim configuration with the Lazy plugin manager to be used on NixOS and other platforms
MIT License
83 stars 0 forks source link

treesitter grammars #24

Open BirdeeHub opened 6 months ago

BirdeeHub commented 6 months ago

Using lazy in this method prevents plugins added via nvim-treesitter.withPlugins or nvim-treesitter.withAllGrammars from being loaded.

However, the paths for the grammars are generated within wrapNeovimUnstable, used by nixos module and home manager module for neovim, and thus their paths are not readily passed through to the lua, as wrapNeovimUnstable occurs long after any nix config that you do, because it happens within the final wrapper used. If you do

 customRC = ''${nvim-treesitter.withAllGrammars}''

this results in JUST the path for treesitter being passed, without including any of the grammars.

This effectively means that using this plugin while using nix prevents you from using treesitter with any parsers not included by default.

If you could get these paths, they could be passed directly to treesitter, however, as mentioned, this is difficult.

If you want to get these paths without generating 2 separate pack dirs, you would most likely need to include the source for wrapNeovim and wrapNeovimUnstable in your code, and then edit them such that they can pass the paths out. If you are ok with the duplication, it can be done slightly, but not much, easier by replicating just some of the logic.

In addition, it removes anything else you added to your runtime path via nix. This removal of stuff from the runtime path is also what removes these grammars. Most schemes to configure neovim from within nix include stuff in the runtime path, and thus those would also cease to work unless each individual file and bit of config is directly sourced within customRC manually.

BirdeeHub commented 6 months ago

In addition, lazy chokes on any build steps included in lua if it detects a nix folder for the plugin, as it cannot modify a store directory.

When this happens, it then disables the plugin, despite the fact that it has already been built by nix, and thus would work if simply added. If you comment out the build step, these plugins that previously had build steps work.

I would suggest using packer, which just downloads the stuff and adds it to runtimepath without hijacking any loading, and then only running packer if not within nix. This is because lazy removes the entire runtime path before doing its work.

I have seen many lazy wrappers for nix, none have solved all issues. They are either hard to use, or they cannot load one thing or another. It seems a better path would be to simply have a plugin manager that JUST downloads the plugin, like packer, that you only run when not within a nix environment if you want the same folder to work with or without nix.

If you can prevent lazy from removing stuff from the runtime path, either selectively or all together, this would fix the issue, albiet in an unsatisfying way.

BirdeeHub commented 6 months ago

Specifically, wrapNeovimUnstable finds the directories of these grammars by calling vimUtils.packDir here:

https://github.com/NixOS/nixpkgs/blob/d02d818f22c777aa4e854efc3242ec451e5d462a/pkgs/applications/editors/neovim/wrapper.nix#L44-L53

And the function vimUtils.packDir does this.

https://github.com/NixOS/nixpkgs/blob/08a62091a13d6ce63fb319a2673da65f2ce77a7f/pkgs/applications/editors/vim/plugins/vim-utils.nix#L175-L206

wrapNeovimUnstable is called by wrapNeovim, after calling makeNeovimConfig on its arguments and passing them to wrapNeovimUnstable.

wrapNeovim is called by the nixos and home manager modules. and your options get passed through this whole chain, and the grammars are only added in at the end by the above 2 links. wrapNeovimUnstable is the last "stop" so to speak. So passing out of it requires digging in quite far.

I will be adding the ability to pass this info out eventually to my nixCats scheme, and passing it through nixCats by default as a table that can be consumed via your plugin, but since your plugin cant load treesitter grammars, they would need to be passed separately to treesitter instead of lazy. But that still doesnt solve the issue of compatibility between your plugin and my scheme because lazy still removes the path I add for the config directory from the runtime path, and the table I pass out wouldnt be accessible when using the home manager or nixos modules because it would be in nixCats and not those. So as a result a way to choose items to NOT remove from the runtimepath would still be necessary