fork/home-manager

git clone https://git.julia.blue/fork/home-manager

KFearsneovim: expose `finalPackage`dd88dbc6

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