fork/home-manager
git clone https://git.julia.blue/fork/home-manager
dd88dbc6
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . neovim ; 8 9fileType = ( import ../lib/file-type.nix { 10inherit ( config . home ) homeDirectory ; 11inherit lib pkgs ; 12}). fileType ; 13 14jsonFormat = pkgs . formats . json { }; 15 16pluginWithConfigType = types . submodule { 17options = { 18config = mkOption { 19type = types . nullOr types . lines ; 20description = 21"Script to configure this plugin. The scripting language should match type." ; 22default = null ; 23}; 24 25type = mkOption { 26type = 27types . either ( types . enum [ "lua" "viml" "teal" "fennel" ]) types . str ; 28description = 29"Language used in config. Configurations are aggregated per-language." ; 30default = "viml" ; 31}; 32 33optional = mkEnableOption "optional" // { 34description = "Don't load by default (load with :packadd)" ; 35}; 36 37plugin = mkOption { 38type = types . package ; 39description = "vim plugin" ; 40}; 41 42runtime = mkOption { 43default = { }; 44# passing actual "${xdg.configHome}/nvim" as basePath was a bit tricky 45# due to how fileType.target is implemented 46type = fileType "programs.neovim.plugins._.runtime" 47"{var}`xdg.configHome/nvim`" "nvim" ; 48example = literalExpression '' 49{ "ftplugin/c.vim".text = "setlocal omnifunc=v:lua.vim.lsp.omnifunc"; } 50'' ; 51description = '' 52Set of files that have to be linked in nvim config folder. 53'' ; 54}; 55}; 56}; 57 58allPlugins = cfg . plugins ++ optional cfg . coc . enable { 59type = "viml" ; 60plugin = cfg . coc . package ; 61config = cfg . coc . pluginConfig ; 62optional = false ; 63}; 64 65luaPackages = cfg . finalPackage . unwrapped . lua . pkgs ; 66resolvedExtraLuaPackages = cfg . extraLuaPackages luaPackages ; 67 68extraMakeWrapperArgs = lib . optionalString ( cfg . extraPackages != [ ]) 69''--suffix PATH : " ${ lib . makeBinPath cfg . extraPackages } "'' ; 70extraMakeWrapperLuaCArgs = 71lib . optionalString ( resolvedExtraLuaPackages != [ ]) '' 72--suffix LUA_CPATH ";" " ${ 73lib . concatMapStringsSep ";" luaPackages . getLuaCPath 74resolvedExtraLuaPackages 75} "'' ; 76extraMakeWrapperLuaArgs = lib . optionalString ( resolvedExtraLuaPackages != [ ]) 77'' 78--suffix LUA_PATH ";" " ${ 79lib . concatMapStringsSep ";" luaPackages . getLuaPath 80resolvedExtraLuaPackages 81} "'' ; 82in { 83imports = [ 84( mkRemovedOptionModule [ "programs" "neovim" "withPython" ] 85"Python2 support has been removed from neovim." ) 86( mkRemovedOptionModule [ "programs" "neovim" "extraPythonPackages" ] 87"Python2 support has been removed from neovim." ) 88( mkRemovedOptionModule [ "programs" "neovim" "configure" ] '' 89programs.neovim.configure is deprecated. 90Other programs.neovim options can override its settings or ignore them. 91Please use the other options at your disposal: 92configure.packages.*.opt -> programs.neovim.plugins = [ { plugin = ...; optional = true; }] 93configure.packages.*.start -> programs.neovim.plugins = [ { plugin = ...; }] 94configure.customRC -> programs.neovim.extraConfig 95'' ) 96]; 97 98options = { 99programs . neovim = { 100enable = mkEnableOption "Neovim" ; 101 102viAlias = mkOption { 103type = types . bool ; 104default = false ; 105description = '' 106Symlink {command}`vi` to {command}`nvim` binary. 107'' ; 108}; 109 110vimAlias = mkOption { 111type = types . bool ; 112default = false ; 113description = '' 114Symlink {command}`vim` to {command}`nvim` binary. 115'' ; 116}; 117 118vimdiffAlias = mkOption { 119type = types . bool ; 120default = false ; 121description = '' 122Alias {command}`vimdiff` to {command}`nvim -d`. 123'' ; 124}; 125 126withNodeJs = mkOption { 127type = types . bool ; 128default = false ; 129description = '' 130Enable node provider. Set to `true` to 131use Node plugins. 132'' ; 133}; 134 135withRuby = mkOption { 136type = types . nullOr types . bool ; 137default = true ; 138description = '' 139Enable ruby provider. 140'' ; 141}; 142 143withPython3 = mkOption { 144type = types . bool ; 145default = true ; 146description = '' 147Enable Python 3 provider. Set to `true` to 148use Python 3 plugins. 149'' ; 150}; 151 152extraPython3Packages = mkOption { 153# In case we get a plain list, we need to turn it into a function, 154# as expected by the function in nixpkgs. 155# The only way to do so is to call `const`, which will ignore its input. 156type = with types ; 157let fromType = listOf package ; 158in coercedTo fromType ( flip warn const '' 159Assigning a plain list to extraPython3Packages is deprecated. 160Please assign a function taking a package set as argument, so 161extraPython3Packages = [ pkgs.python3Packages.xxx ]; 162should become 163extraPython3Packages = ps: [ ps.xxx ]; 164'' ) ( functionTo fromType ); 165default = _ :[ ]; 166defaultText = literalExpression "ps: [ ]" ; 167example = 168literalExpression "pyPkgs: with pyPkgs; [ python-language-server ]" ; 169description = '' 170The extra Python 3 packages required for your plugins to work. 171This option accepts a function that takes a Python 3 package set as an argument, 172and selects the required Python 3 packages from this package set. 173See the example for more info. 174'' ; 175}; 176 177# We get the Lua package from the final package and use its 178# Lua packageset to evaluate the function that this option was set to. 179# This ensures that we always use the same Lua version as the Neovim package. 180extraLuaPackages = mkOption { 181type = with types ; 182let fromType = listOf package ; 183in coercedTo fromType ( flip warn const '' 184Assigning a plain list to extraLuaPackages is deprecated. 185Please assign a function taking a package set as argument, so 186extraLuaPackages = [ pkgs.lua51Packages.xxx ]; 187should become 188extraLuaPackages = ps: [ ps.xxx ]; 189'' ) ( functionTo fromType ); 190default = _ :[ ]; 191defaultText = literalExpression "ps: [ ]" ; 192example = literalExpression "luaPkgs: with luaPkgs; [ luautf8 ]" ; 193description = '' 194The extra Lua packages required for your plugins to work. 195This option accepts a function that takes a Lua package set as an argument, 196and selects the required Lua packages from this package set. 197See the example for more info. 198'' ; 199}; 200 201generatedConfigViml = mkOption { 202type = types . lines ; 203visible = true ; 204readOnly = true ; 205description = '' 206Generated vimscript config. 207'' ; 208}; 209 210generatedConfigs = mkOption { 211type = types . attrsOf types . lines ; 212visible = true ; 213readOnly = true ; 214example = literalExpression '' 215{ 216viml = ''' 217" Generated by home-manager 218map <leader> , 219'''; 220 221lua = ''' 222-- Generated by home-manager 223vim.opt.background = "dark" 224'''; 225}'' ; 226description = '' 227Generated configurations with as key their language (set via type). 228'' ; 229}; 230 231package = mkOption { 232type = types . package ; 233default = pkgs . neovim-unwrapped ; 234defaultText = literalExpression "pkgs.neovim-unwrapped" ; 235description = "The package to use for the neovim binary." ; 236}; 237 238finalPackage = mkOption { 239type = types . package ; 240readOnly = true ; 241description = "Resulting customized neovim package." ; 242}; 243 244defaultEditor = mkOption { 245type = types . bool ; 246default = false ; 247description = '' 248Whether to configure {command}`nvim` as the default 249editor using the {env}`EDITOR` environment variable. 250'' ; 251}; 252 253extraConfig = mkOption { 254type = types . lines ; 255default = "" ; 256example = '' 257set nobackup 258'' ; 259description = '' 260Custom vimrc lines. 261'' ; 262}; 263 264extraLuaConfig = mkOption { 265type = types . lines ; 266default = "" ; 267example = '' 268vim.opt.nobackup = true 269'' ; 270description = '' 271Custom lua lines. 272'' ; 273}; 274 275extraPackages = mkOption { 276type = with types ; listOf package ; 277default = [ ]; 278example = literalExpression "[ pkgs.shfmt ]" ; 279description = "Extra packages available to nvim." ; 280}; 281 282plugins = mkOption { 283type = with types ; listOf ( either package pluginWithConfigType ); 284default = [ ]; 285example = literalExpression '' 286with pkgs.vimPlugins; [ 287yankring 288vim-nix 289{ plugin = vim-startify; 290config = "let g:startify_change_to_vcs_root = 0"; 291} 292] 293'' ; 294description = '' 295List of vim plugins to install optionally associated with 296configuration to be placed in init.vim. 297 298This option is mutually exclusive with {var}`configure`. 299'' ; 300}; 301 302coc = { 303enable = mkEnableOption "Coc" ; 304 305package = mkOption { 306type = types . package ; 307default = pkgs . vimPlugins . coc-nvim ; 308defaultText = literalExpression "pkgs.vimPlugins.coc-nvim" ; 309description = "The package to use for the CoC plugin." ; 310}; 311 312settings = mkOption { 313inherit ( jsonFormat ) type ; 314default = { }; 315example = literalExpression '' 316{ 317"suggest.noselect" = true; 318"suggest.enablePreview" = true; 319"suggest.enablePreselect" = false; 320"suggest.disableKind" = true; 321languageserver = { 322haskell = { 323command = "haskell-language-server-wrapper"; 324args = [ "--lsp" ]; 325rootPatterns = [ 326"*.cabal" 327"stack.yaml" 328"cabal.project" 329"package.yaml" 330"hie.yaml" 331]; 332filetypes = [ "haskell" "lhaskell" ]; 333}; 334}; 335}; 336'' ; 337description = '' 338Extra configuration lines to add to 339{file}`$XDG_CONFIG_HOME/nvim/coc-settings.json` 340See 341<https://github.com/neoclide/coc.nvim/wiki/Using-the-configuration-file> 342for options. 343'' ; 344}; 345 346pluginConfig = mkOption { 347type = types . lines ; 348default = "" ; 349description = "Script to configure CoC. Must be viml." ; 350}; 351}; 352}; 353}; 354 355config = let 356defaultPlugin = { 357type = "viml" ; 358plugin = null ; 359config = null ; 360optional = false ; 361runtime = { }; 362}; 363 364# transform all plugins into a standardized attrset 365pluginsNormalized = 366map ( x :defaultPlugin // ( if ( x ?plugin ) then x else { plugin = x ; })) 367allPlugins ; 368 369suppressNotVimlConfig = p : 370if p . type != "viml" then p // { config = null ; } else p ; 371 372neovimConfig = pkgs . neovimUtils . makeNeovimConfig { 373inherit ( cfg ) extraPython3Packages withPython3 withRuby viAlias vimAlias ; 374withNodeJs = cfg . withNodeJs || cfg . coc . enable ; 375plugins = map suppressNotVimlConfig pluginsNormalized ; 376customRC = cfg . extraConfig ; 377}; 378 379in mkIf cfg . enable { 380 381programs . neovim . generatedConfigViml = neovimConfig . neovimRcContent ; 382 383programs . neovim . generatedConfigs = let 384grouped = lib . lists . groupBy ( x :x . type ) pluginsNormalized ; 385concatConfigs = lib . concatMapStrings ( p :p . config ); 386configsOnly = lib . foldl 387( acc :p :if p . config != null then acc ++ [ p . config ] else acc ) [ ]; 388in mapAttrs ( name :vals :lib . concatStringsSep "\n" ( configsOnly vals )) 389grouped ; 390 391home . packages = [ cfg . finalPackage ]; 392 393home . sessionVariables = mkIf cfg . defaultEditor { EDITOR = "nvim" ; }; 394 395xdg . configFile = 396let hasLuaConfig = hasAttr "lua" config . programs . neovim . generatedConfigs ; 397in mkMerge ( 398# writes runtime 399( map ( x :x . runtime ) pluginsNormalized ) ++ [{ 400"nvim/init.lua" = let 401luaRcContent = 402lib . optionalString ( neovimConfig . neovimRcContent != "" ) 403"vim.cmd [[source ${ 404pkgs . writeText "nvim-init-home-manager.vim" 405neovimConfig . neovimRcContent 406} ]]" + config . programs . neovim . extraLuaConfig 407+ lib . optionalString hasLuaConfig 408config . programs . neovim . generatedConfigs . lua ; 409in mkIf ( luaRcContent != "" ) { text = luaRcContent ; }; 410 411"nvim/coc-settings.json" = mkIf cfg . coc . enable { 412source = jsonFormat . generate "coc-settings.json" cfg . coc . settings ; 413}; 414}]); 415 416programs . neovim . finalPackage = pkgs . wrapNeovimUnstable cfg . package 417( neovimConfig // { 418wrapperArgs = ( lib . escapeShellArgs neovimConfig . wrapperArgs ) + " " 419+ extraMakeWrapperArgs + " " + extraMakeWrapperLuaCArgs + " " 420+ extraMakeWrapperLuaArgs ; 421wrapRc = false ; 422}); 423 424programs . bash . shellAliases = mkIf cfg . vimdiffAlias { vimdiff = "nvim -d" ; }; 425programs . fish . shellAliases = mkIf cfg . vimdiffAlias { vimdiff = "nvim -d" ; }; 426programs . zsh . shellAliases = mkIf cfg . vimdiffAlias { vimdiff = "nvim -d" ; }; 427}; 428}