forked from NixNeovim/NixNeovim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnixneovim.nix
381 lines (321 loc) · 11.1 KB
/
nixneovim.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
{ homeManager ? true, isDocsBuild ? false, state ? 9999, haumea }: # function that returns a package
{ lib, config, pkgs, ... }:
with lib;
let
cfg = config.programs.nixneovim;
mappings = helpers.keymapping;
inherit (helpers) augroups;
pluginWithConfigType = types.submodule {
options = {
config = mkOption {
type = types.lines;
description = "vimscript for this plugin to be placed in init.vim";
default = "";
};
optional = mkEnableOption "optional" // {
description = "Don't load by default (load with :packadd)";
};
plugin = mkOption {
type = types.package;
description = "vim plugin";
};
};
};
helpers = haumea.lib.load {
src = ./helpers;
inputs = {
inherit lib config;
usePluginDefaults = config.programs.nixneovim.usePluginDefaults;
};
};
plugins =
let
src = haumea.lib.load {
src = ./src;
inputs = {
inherit helpers config pkgs lib state;
};
};
in src.plugins //
src.environments //
src.colorschemes; # TODO: add the other plugins
in {
imports = [
# manually importing './plugins' prevents infinite loop
# (import ./plugins { inherit pkgs lib config helpers plugins; })
({
imports = lib.mapAttrsToList
(key: value: value)
# if key == "numb" || key == "bamboo" then
# lib.trace
# key
# lib.traceSeqN 1 value.options.programs.nixneovim
# value
# else
# value)
plugins;
})
];
options = {
programs.nixneovim = {
enable = mkEnableOption "enable nixneovim";
defaultEditor = mkOption {
type = types.bool;
default = false;
description = "Configures neovim to be the default editor using the EDITOR environment variable.";
};
viAlias = mkOption {
type = types.bool;
default = false;
description = ''
Symlink <command>vi</command> to <command>nvim</command> binary.
'';
};
vimAlias = mkOption {
type = types.bool;
default = false;
description = ''
Symlink <command>vim</command> to <command>nvim</command> binary.
'';
};
package = mkOption {
type = types.nullOr types.package;
default = null;
description = "The package to use for neovim.";
};
# plugins = mkOption {
# type = types.attrsOf types.anything;
# default = {};
# };
# colorschems = mkOption {
# type = types.anything;
# default = {};
# };
extraPlugins = mkOption {
type = with types; listOf (either package pluginWithConfigType);
default = [ ];
description = "List of vim plugins to install.";
};
usePluginDefaults = mkOption {
type = types.bool;
default = false;
description = ''
When false, NixNeovim will output the lua config with all available options.
This way, when a default in a plugin changes, your config will stay the same.
When true, NixNeovim will output the lua config only with options you have set in you config.
This way, all other values will have the default set by the plugin author.
When the defaults change, your setup will change.
Setting this to true, will significantly reduce the number of lines in your init.lua, depending on the number of plugins enabled.
'';
};
colorscheme = mkOption {
type = types.nullOr types.str;
description = "The name of the colorscheme";
default = null;
};
extraConfigLua = mkOption {
type = types.lines;
default = "";
description = "Extra contents for init.lua";
};
extraLuaPreConfig = mkOption {
type = types.lines;
default = "";
description = "Extra contents for init.lua before everything else";
};
extraLuaPostConfig = mkOption {
type = types.lines;
default = "";
description = "Extra contents for init.lua after everything else";
};
extraConfigVim = mkOption {
type = types.lines;
default = "";
description = "Extra contents for init.vim";
};
extraPackages = mkOption {
type = types.listOf types.package;
default = [ ];
example = "[ pkgs.shfmt ]";
description = "Extra packages to be made available to neovim";
};
configure = mkOption {
type = types.attrsOf types.anything;
default = { };
description = "Internal option";
};
options = mkOption {
type = types.attrsOf types.anything;
default = { };
description = "The configuration options, e.g. line numbers";
};
globals = mkOption {
type = types.attrsOf types.anything;
default = { };
description = "Global variables";
};
mappings = mkOption {
type = types.submodule {
options =
let
inherit (mappings) mapOptions;
in {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
visual = mapOptions "visual and select";
terminal = mapOptions "terminal";
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = mapOptions "visual only";
operator = mapOptions "operator-pending";
insertCommand = mapOptions "insert and command-line";
lang = mapOptions "insert, command-line and lang-arg";
command = mapOptions "command-line";
};
};
default = { };
description = ''
Custom keybindings for any mode.
For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp.
'';
example = ''
maps = {
normalVisualOp.";" = ":"; # Same as noremap ; :
normal."<leader>m" = {
silent = true;
action = "<cmd>make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
};
'';
};
augroups = mkOption {
default = { };
type = types.attrsOf (types.submodule augroups.augroupOptions);
description = ''
Custom autocmd groups
'';
example = ''
augroups.highlightOnYank = {
autocmds = [{
event = "TextYankPost";
pattern = "*";
luaCallback = ''\''
vim.highlight.on_yank {
higroup = (
vim.fn['hlexists'] 'HighlightedyankRegion' > 0 and 'HighlightedyankRegion' or 'IncSearch'
),
timeout = 200,
}
''\'';
}];
};
'';
};
};
};
config =
let
neovimConfig = pkgs.neovimUtils.makeNeovimConfig {
configure = cfg.configure;
plugins = cfg.extraPlugins;
};
extraWrapperArgs = optionalString (cfg.extraPackages != [ ])
''--prefix PATH : "${makeBinPath cfg.extraPackages}"'';
package = if (cfg.package != null) then cfg.package else pkgs.neovim;
wrappedNeovim = pkgs.wrapNeovimUnstable package (neovimConfig // {
wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs + " "
+ extraWrapperArgs;
});
luaGlobals =
let
list = mapAttrsToList
(option: value:
"vim.g.${option} = ${helpers.converter.toLuaObject value}"
)
cfg.globals;
in concatStringsSep "\n" list;
luaOptions =
let
list = mapAttrsToList
(option: value:
"vim.o.${option} = ${helpers.converter.toLuaObject value}"
)
cfg.options;
in concatStringsSep "\n" list;
luaConfig = ''
${cfg.extraLuaPreConfig}
--------------------------------------------------
-- Globals --
--------------------------------------------------
${luaGlobals}
--------------------------------------------------
-- Options --
--------------------------------------------------
${luaOptions}
--------------------------------------------------
-- Keymappings --
--------------------------------------------------
${mappings.luaString cfg.mappings}
--------------------------------------------------
-- Augroups --
--------------------------------------------------
${augroups.luaString cfg.augroups}
--------------------------------------------------
-- Extra Config (Lua) --
--------------------------------------------------
${cfg.extraConfigLua}
${
# Set colorscheme after setting globals.
# Some colorschemes depends on variables being set before setting the colorscheme.
optionalString
(cfg.colorscheme != "" && cfg.colorscheme != null)
"vim.cmd([[colorscheme ${cfg.colorscheme}]])"
}
${cfg.extraLuaPostConfig}
'';
configure = {
# Make sure that globals are set before plugins are setup.
# This is becuase you might want to define variables or global functions
# that the plugin configuration depend upon.
customRC =
cfg.extraConfigVim
+ luaConfig;
packages.nixneovim = {
start = filter (f: f != null) (map
(x:
if x ? plugin && x.optional == true then null else (x.plugin or x))
cfg.extraPlugins);
opt = filter (f: f != null)
(map (x: if x ? plugin && x.optional == true then x.plugin else null)
cfg.extraPlugins);
};
};
in (mkIf cfg.enable (
if isDocsBuild then { }
else if homeManager then
{
programs.neovim = {
enable = true;
# defaultEditor = cfg.defaultEditor;
viAlias = cfg.viAlias;
vimAlias = cfg.vimAlias;
package = mkIf (cfg.package != null) cfg.package;
extraPackages = cfg.extraPackages;
extraConfig = cfg.extraConfigVim;
extraLuaConfig = luaConfig;
plugins = cfg.extraPlugins;
} // (optionalAttrs (state > 2211) { defaultEditor = cfg.defaultEditor; }); # only add defaultEditor when over nixpkgs release 22-11
}
else
{
environment.systemPackages = [ wrappedNeovim ];
programs.neovim = {
defaultEditor = cfg.defaultEditor;
viAlias = cfg.viAlias;
vimAlias = cfg.vimAlias;
configure = configure;
};
environment.etc."xdg/nvim/sysinit.vim".text = neovimConfig.neovimRcContent;
}
));
}