nixvim
nixvim copied to clipboard
Allow defining setup options in extraPlugins (WIP)
Description
Often times, I want to add some plugin that is not present on nixvim. To do this, I'd end up adding the plugin package to extraPlugins
, and then set the config options somewhere else.
With this PR I am aiming to allow things to be more centralized, initially by making it possible to automatically call the setup
function.
Example
Before
{
extraPlugins = [ pkgs.vimPlugins.orgmode ];
extraConfigLua = ''
require('orgmode').setup({
org_agenda_files = {'~/org/**/*'},
org_default_notes_file = '~/org/refile.org'
})
'';
}
After
{
extraPlugins = with pkgs.vimPlugins; [{
package = orgmode;
packageName = "orgmode";
setup = {
org_agenda_files = [ "~/org/**/*" ];
org_default_notes_file = "~/org/refile.org";
};
}];
}
Obviously, with only one plugin, this isn't that much help, but I hope you can see how this can come in handy when you have a lot of external plugins!
Notes
This is very much a WIP, I have only defined the options for now, but I wanted to get some feedback!
TODO
- [X] Define options
- [ ] Generate lua code
- [ ] Allow adding arbitrary lua/vimscript code
- [ ] Allow setting lazy-loading options
- [ ] Allow setting keymaps
- [ ] Allow setting global variables
This is a lovely idea. :)
My current approach for doing this is:
{pkgs, ...}:
let
pluginModule = {pkg, cfg}: {...}: {
extraPlugins = [pkg];
extraConfigLua = cfg;
};
in
{
imports = [
(pluginModule {
pkg = pkgs.vimPlugins.lazy-lsp-nvim;
cfg = ''
require('lazy-lsp').setup({
excluded_servers = {
"denols",
"efm", -- not using it?
"diagnosticls",
"zk",
"sqls",
"tailwindcss",
},
})
'';
})
];
# ...
}
It's a little bulky and leverages imports rather than plugins, but a little more ergonomic than separating configs from plugins.
It's a little bulky and leverages imports rather than plugins, but a little more ergonomic than separating configs from plugins.
@shazow
Cool! Could you share how you import that expression? (I didn't quite get how the currying works so it's applied to the programs.nixvim
attribute.)
You could get rid of some of the other verbosity by doing something like this:
{ pkgs , ... }:
let
pluginModule = {pkg, cfg ? "", kmaps ? []}: {
programs.nixvim = {
extraPlugins = [pkg];
extraConfigLua = cfg;
keymaps = kmaps;
};
};
in {
# https://github.com/nix-community/nixvim/pull/365#issuecomment-1736032460
imports = map(p: pluginModule p) [
{
pkg = pkgs.vimPlugins.FTerm-nvim;
kmaps = [
{
mode = "n";
key = "<leader>lg";
action =
"<CMD>lua require('FTerm').scratch({ config = { auto_close = true }, cmd = '${pkgs.lazygit}/bin/lazygit' })<CR>";
options = { silent = true; };
}
{
mode = "n";
key = "<leader>tt";
action = "<CMD>lua require('FTerm').toggle()<CR>";
options = { silent = true; };
}
{
mode = "t";
key = "<C-Bslash>";
action = "<CMD>lua require('FTerm').toggle()<CR>";
options = { silent = true; };
}
];
}
{
pkg = pkgs.vimPlugins.formatter-nvim;
cfg = builtins.readFile ./formatter-nvim.lua;
}
];
}
With this you also get to enjoy the lua
syntax highlighting while editing the plugin configs, and probably could also easier include other lua
configs from others. And you get to keep your keymaps also with the plugin to which they apply.
@ppenguin
You could get rid of some of the other verbosity by doing something like this: ...
Good point, I really should collapse things into the keymaps attrset. Didn't think of that.
Cool! Could you share how you import that expression? (I didn't quite get how the currying works so it's applied to the
programs.nixvim
attribute.)
The easiest thing to do is probably save it in its own moreplugins.nix
file and add it to your programs.nixvim.imports = [ ./moreplugins.nix ];
You could also do it inline if you prefer (didn't test this, just off the cuff):
{
programs.nixvim = let
pluginModule = {pkg, cfg ? "", kmaps ? []}: {
programs.nixvim = {
extraPlugins = [pkg];
extraConfigLua = cfg;
keymaps = kmaps;
};
};
in {
# ...
imports = [
(pluginModule {
pkg = pkgs.vimPlugins.lazy-lsp-nvim;
cfg = ''
require('lazy-lsp').setup({
excluded_servers = {
"denols",
"efm", -- not using it?
"diagnosticls",
"zk",
"sqls",
"tailwindcss",
},
})
'';
})
];
};
};
That said, I ended up refactoring this into a more sophisticated module that I might upstream still!
https://github.com/shazow/nixfiles/blob/main/pkgs/nvim/modules/plugins.nix
How I'm using it: https://github.com/shazow/nixfiles/blob/main/pkgs/nvim/config/default.nix#L145
This might be a good idea indeed. Feel free to make a PR when you have an implementation draft. Personally, I do not have extra time to spend on nixvim besides the core maintenance work (fixing bugs + adding required plugins). However, I will be glad to review and eventually accept such addition to the project :)
I don't have time to polish it up at the moment but will do that next time I'm playing with my nvim configs again, or anyone else feel free to beat me to it! :)
This sound great, cant wait.