1
0
mirror of https://github.com/nix-community/home-manager synced 2024-06-03 05:23:32 +02:00

programs.neovim: default to init.lua (#3233)

We change the current logic: instead of writing an init.vim which loads
lua/init-home-manager.lua, we write an init.lua that sources init.vim

This commit also avoids writing any of these files if the plugins have
no config.
This commit is contained in:
Matthieu Coudron 2022-09-22 10:39:55 +02:00 committed by GitHub
parent f17819f4f1
commit bd83eab622
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 23 deletions

View File

@ -35,10 +35,10 @@ let
pluginWithConfigType = types.submodule { pluginWithConfigType = types.submodule {
options = { options = {
config = mkOption { config = mkOption {
type = types.lines; type = types.nullOr types.lines;
description = description =
"Script to configure this plugin. The scripting language should match type."; "Script to configure this plugin. The scripting language should match type.";
default = ""; default = null;
}; };
type = mkOption { type = mkOption {
@ -326,7 +326,7 @@ in {
defaultPlugin = { defaultPlugin = {
type = "viml"; type = "viml";
plugin = null; plugin = null;
config = ""; config = null;
optional = false; optional = false;
runtime = { }; runtime = { };
}; };
@ -337,7 +337,7 @@ in {
allPlugins; allPlugins;
suppressNotVimlConfig = p: suppressNotVimlConfig = p:
if p.type != "viml" then p // { config = ""; } else p; if p.type != "viml" then p // { config = null; } else p;
neovimConfig = pkgs.neovimUtils.makeNeovimConfig { neovimConfig = pkgs.neovimUtils.makeNeovimConfig {
inherit (cfg) extraPython3Packages withPython3 withRuby viAlias vimAlias; inherit (cfg) extraPython3Packages withPython3 withRuby viAlias vimAlias;
@ -353,26 +353,33 @@ in {
programs.neovim.generatedConfigs = let programs.neovim.generatedConfigs = let
grouped = lib.lists.groupBy (x: x.type) pluginsNormalized; grouped = lib.lists.groupBy (x: x.type) pluginsNormalized;
concatConfigs = lib.concatMapStrings (p: p.config); concatConfigs = lib.concatMapStrings (p: p.config);
in mapAttrs (name: vals: concatConfigs vals) grouped; configsOnly = lib.foldl
(acc: p: if p.config != null then acc ++ [ (p.config) ] else acc) [ ];
in mapAttrs (name: vals: lib.concatStringsSep "\n" (configsOnly vals))
grouped;
home.packages = [ cfg.finalPackage ]; home.packages = [ cfg.finalPackage ];
xdg.configFile = mkMerge ( xdg.configFile =
# writes runtime let hasLuaConfig = hasAttr "lua" config.programs.neovim.generatedConfigs;
(map (x: x.runtime) pluginsNormalized) ++ [{ in mkMerge (
"nvim/init.vim" = mkIf (neovimConfig.neovimRcContent != "") { # writes runtime
text = neovimConfig.neovimRcContent + lib.optionalString (map (x: x.runtime) pluginsNormalized) ++ [{
(hasAttr "lua" config.programs.neovim.generatedConfigs) "nvim/init.vim" = mkIf (neovimConfig.neovimRcContent != "") {
"lua require('init-home-manager')"; text = neovimConfig.neovimRcContent;
};
"nvim/lua/init-home-manager.lua" =
mkIf (hasAttr "lua" config.programs.neovim.generatedConfigs) {
text = config.programs.neovim.generatedConfigs.lua;
}; };
"nvim/coc-settings.json" = mkIf cfg.coc.enable { "nvim/init.lua" = let
source = jsonFormat.generate "coc-settings.json" cfg.coc.settings; luaRcContent =
}; lib.optionalString (neovimConfig.neovimRcContent != "")
}]); "vim.cmd.source ${config.xdg.configHome}/nvim/init.vim"
+ lib.optionalString hasLuaConfig
config.programs.neovim.generatedConfigs.lua;
in mkIf (luaRcContent != "") { text = luaRcContent; };
"nvim/coc-settings.json" = mkIf cfg.coc.enable {
source = jsonFormat.generate "coc-settings.json" cfg.coc.settings;
};
}]);
programs.neovim.finalPackage = pkgs.wrapNeovimUnstable cfg.package programs.neovim.finalPackage = pkgs.wrapNeovimUnstable cfg.package
(neovimConfig // { (neovimConfig // {

View File

@ -12,10 +12,14 @@ with lib;
withRuby = false; withRuby = false;
extraPython3Packages = (ps: with ps; [ jedi pynvim ]); extraPython3Packages = (ps: with ps; [ jedi pynvim ]);
# plugins without associated config should not trigger the creation of init.vim
plugins = with pkgs.vimPlugins; [ fugitive ({ plugin = vim-sensible; }) ];
}; };
nmt.script = '' nmt.script = ''
vimrc="home-files/.config/nvim/init.vim" nvimFolder="home-files/.config/nvim"
assertPathNotExists "$vimrc" assertPathNotExists "$nvimFolder/init.vim"
assertPathNotExists "$nvimFolder/init.lua"
''; '';
}; };
} }

View File

@ -1,4 +1,3 @@
" plugin-specific config " plugin-specific config
autocmd FileType c setlocal commentstring=//\ %s autocmd FileType c setlocal commentstring=//\ %s
autocmd FileType c setlocal comments=:// autocmd FileType c setlocal comments=://