-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.nix
260 lines (244 loc) · 7.28 KB
/
module.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
{
config,
lib,
pkgs,
nix2nvimrc,
...
}:
let
inherit (lib) types mkOption;
exprType = types.submodule {
options = {
type = mkOption { type = types.enum [ "lua" ]; };
expression = mkOption { type = types.str; };
};
};
keymapType = types.submodule {
options = {
mode = mkOption { type = types.either types.str (types.listOf types.str); };
lhs = mkOption { type = types.str; };
rhs = mkOption { type = types.either types.str exprType; };
opts = mkOption { type = types.attrs; };
};
};
expressions = {
lua = mkOption {
type = types.listOf types.str;
default = [ ];
description = "Lua expressions to execute";
};
vim = mkOption {
type = types.listOf (types.either types.str types.path);
default = [ ];
description = "Vim expressions or files to execute";
};
};
luaFunctionCallType =
name:
types.submodule {
options = {
modulePath = mkOption {
type = types.nullOr types.str;
default = null;
description = "Module path used to function";
};
function = mkOption {
type = types.str;
default = name;
};
args = mkOption {
type = types.anything;
default = { };
description = "Argument passed to function";
};
};
};
configType = types.submodule {
options = {
after = mkOption {
type = types.listOf types.str;
default = [ ];
description = "This configuration has to be executed after given configurations.";
};
setup = mkOption {
type = types.nullOr (luaFunctionCallType "setup");
default = null;
description = "Lua plugin setup";
};
plugins = mkOption {
type = types.listOf types.package;
default = [ ];
description = "Vim plugin packages";
};
vars = mkOption {
type = types.attrs;
default = { };
description = "Vim global variables";
};
opts = mkOption {
type = types.attrs;
default = { };
description = "Vim options";
};
keymaps = mkOption {
type = types.listOf keymapType;
default = [ ];
};
treesitter.parsers = mkOption {
type = types.attrsOf types.path;
default = { };
description = "Attribute set where name is the language and the the value a path to the parser.";
};
lspconfig = mkOption {
type = types.nullOr lspconfigType;
default = null;
};
} // expressions;
};
lspconfigType = types.submodule {
options = {
servers = mkOption {
type = types.attrsOf (
types.submodule {
options = {
config = mkOption {
type = types.attrs;
default = { };
};
};
}
);
};
capabilities = mkOption {
type = types.either types.attrs types.path;
default = nix2nvimrc.luaExpr "vim.lsp.protocol.make_client_capabilities()";
};
keymaps = mkOption {
type = types.listOf keymapType;
default = [ ];
};
opts = mkOption {
type = types.attrs;
default = { };
description = "Vim options";
};
on_attach = mkOption {
type = types.either types.attrs types.path;
default = nix2nvimrc.luaExpr "function(client, bufnr) end";
};
};
};
in
{
options = {
configs = mkOption {
type = types.attrsOf configType;
description = "NVim configurations";
};
enableFns = mkOption {
type = types.listOf (types.functionTo types.bool);
default = [ ];
description = "Enable functions for a configuration module";
};
out = mkOption {
internal = true;
type = types.str;
};
opt = mkOption {
internal = true;
type = types.attrsOf types.anything;
};
var = mkOption {
internal = true;
type = types.attrsOf types.anything;
};
config = mkOption {
internal = true;
type = types.attrsOf types.anything;
};
};
config =
let
inherit (lib) optional optionals toposort;
inherit (nix2nvimrc) toLuaFn toLua;
configs =
let
res = toposort (a: b: builtins.elem a.name b.after) (
map (name: config.configs.${name} // { inherit name; }) (
builtins.filter (
name:
builtins.foldl' (last: enableFn: last && (enableFn config.configs.${name})) true config.enableFns
) (builtins.attrNames config.configs)
)
);
in
res.result or (throw "Config has a cyclic dependency");
in
{
out =
let
vim2str = vim: if builtins.isPath vim then "source ${vim}" else vim;
lspconfigWrapper = "lspconfigWrapper";
lspUsed = builtins.any (c: c.lspconfig != null) configs;
configToLua =
c:
[ "-- ${c.name} (${builtins.concatStringsSep " " (map (p: p.name) c.plugins)})" ]
++ (map (
k:
toLuaFn "vim.api.nvim_set_var" [
k
c.vars.${k}
]
) (builtins.attrNames c.vars))
++ (map (
m:
toLuaFn "vim.keymap.set" [
m.mode
m.lhs
m.rhs
m.opts
]
) c.keymaps)
++ (map (k: "vim.opt[${toLua k}] = ${toLua c.opts.${k}}") (builtins.attrNames c.opts))
++ (optional (c.setup != null) (
toLuaFn "require'${
if c.setup.modulePath != null then c.setup.modulePath else c.name
}'.${c.setup.function}" [ c.setup.args ]
))
++ c.lua
++ (map (v: toLuaFn "vim.cmd" [ v ]) (map vim2str c.vim))
++ (map (
l:
toLuaFn "vim.treesitter.language.add" [
l
{ path = c.treesitter.parsers.${l}; }
]
) (builtins.attrNames c.treesitter.parsers))
++ (optional (c.lspconfig != null) (toLuaFn lspconfigWrapper [ c.lspconfig ]));
init_lua =
[ "-- generated by nix2nvimrc" ]
++ optionals (packages != [ ]) [
"vim.opt.packpath:append('${pkgs.vimUtils.packDir packages}')"
"vim.opt.runtimepath:append('${pkgs.vimUtils.packDir packages}')"
]
++ optional lspUsed "local ${lspconfigWrapper} = ${toLua ./nix-lspconfig.lua}"
++ builtins.concatMap (
c:
let
lua = configToLua c;
in
if builtins.length lua <= 1 && c.plugins == [ ] then
builtins.trace "Warning: configuration '${c.name}' has no configuration" [ ]
else
lua
) configs
++ [ "-- vim: set filetype=lua:" ];
packages.nix-nvimconfig.start = builtins.foldl' (a: b: a ++ b.plugins) [ ] configs;
in
builtins.concatStringsSep "\n" init_lua;
opt = builtins.foldl' (a: b: a // b.opts) { } configs;
var = builtins.foldl' (a: b: a // b.vars) { } configs;
config = config.configs;
_module.args.nix2nvimrc = import ./lib.nix;
};
}