-
Notifications
You must be signed in to change notification settings - Fork 4
/
korora-neovim.nix
340 lines (329 loc) · 8.73 KB
/
korora-neovim.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
attrs@{ lib, korora, ... }:
let
e = import ./korora-lua.nix attrs;
k = korora;
h = e.helpers;
struct =
name: props: verify:
(k.struct name props).override {
total = false;
unknown = false;
verify = h.mkVerify verify;
};
lazyType = name: mkType: k.typedef' name (v: (mkType true).verify v);
types = {
# {{{ Helper types
oneOrMany =
type:
k.union [
type
(k.listOf type)
];
luaValue = k.any;
strictLuaLiteral =
(k.struct "lua literal" {
value = k.string;
__luaEncoderTag = k.enum "lua literal tag" [ "lua" ];
}).override
{ unknown = true; };
derivation = k.typedef "derivation" lib.isDerivation;
path = k.typedef "path" lib.isPath;
functionCheckedWith =
arg: type:
k.typedef' "${h.toPretty arg} -> ${type.name}" (
f:
if lib.isFunction f then
type.verify (f arg)
else
"Expected function, but got ${h.toPretty f} instead"
);
lazy = types.functionCheckedWith "";
luaEagerOrLazy =
type:
k.union [
type
(types.lazy type)
];
luaLiteral = types.luaEagerOrLazy types.strictLuaLiteral;
# }}}
# {{{ Lazy key
lazyKey = struct "lazy key" {
mapping = k.string;
action = k.union [
types.luaLiteral
k.string
];
mode = k.string;
desc = k.string;
expr = k.bool;
ft = types.oneOrMany k.string;
} [ (h.propExists "mapping") ];
# }}}
# {{{ Lazy module
lazyModule = lazyType "actually lazy lazy module" (
_:
struct "lazy module"
{
package = k.string;
dir = k.union [
k.string
types.derivation
types.path
];
version = k.string;
tag = k.string;
name = k.string;
main = k.string;
lazy = k.bool;
dependencies = struct "lazy dependencies" {
lua = k.listOf (
k.union [
k.string
types.lazyModule
]
);
nix = k.listOf types.derivation;
} [ ];
enabled = k.bool;
cond = types.oneOrMany types.luaLiteral;
init = types.luaEagerOrLazy (
k.union [
types.strictLuaLiteral
types.strictTempestConfig
]
);
config = k.union [
k.bool
(types.luaEagerOrLazy (
k.union [
types.strictLuaLiteral
types.strictTempestConfig
]
))
];
event = types.oneOrMany k.string;
cmd = types.oneOrMany k.string;
ft = types.oneOrMany k.string;
keys = types.oneOrMany (
k.union [
k.string
types.lazyKey
]
);
passthrough = types.luaValue;
opts = types.luaValue;
}
[
(h.propOnlyOne [
"dir"
"package"
])
(h.propImplies "tag" "package")
(h.propImplies "version" "package")
]
);
# }}}
# {{{ Tempest key
tempestKey =
struct "tempest key"
{
mapping = k.string;
action = types.luaValue;
mode = k.string;
desc = k.string;
expr = k.bool;
silent = k.bool;
ft = types.oneOrMany k.string;
buffer = k.union [
k.bool
k.number
types.luaLiteral
];
}
[
(h.propExists "mapping")
(h.propExists "action")
];
# }}}
# {{{ Tempest autocmd
tempestAutocmd =
struct "tempest autocommand"
{
event = types.oneOrMany k.string;
pattern = types.oneOrMany k.string;
group = k.string;
action = k.union [
types.tempestConfig
types.luaLiteral
];
}
[
(h.propExists "event")
(h.propExists "group")
(h.propExists "action")
];
# }}}
# {{{ Tempest config
strictTempestConfig = struct "tempest config" {
vim = types.luaValue;
callback = k.union [
types.luaLiteral
types.tempestConfig
];
setup = k.attrsOf types.luaValue;
keys = types.luaEagerOrLazy (types.oneOrMany types.tempestKey);
autocmds = types.luaEagerOrLazy (types.oneOrMany types.tempestAutocmd);
mkContext = types.luaValue;
cond = types.oneOrMany types.luaValue;
} [ ];
tempestConfig = lazyType "lazy tempest config" (_: types.strictTempestConfig);
# }}}
# {{{ Neovim env
neovimEnv = k.enum "neovim env" [
"neovide"
"firenvim"
"vscode"
];
# }}}
# {{{ Neovim config
neovimConfig = struct "neovim configuration" {
pre = k.attrsOf types.tempestConfig;
lazy = k.attrsOf types.lazyModule;
post = k.attrsOf types.tempestConfig;
} [ ];
# }}}
};
mkLib =
{ tempestModule }:
assert h.hasType k.string tempestModule;
rec {
inherit (e) encode;
inherit (h) lua;
# {{{ Common generation helpers
importFrom =
path: tag:
assert lib.isPath path;
assert h.hasType k.string tag;
lua "dofile(${encode (toString path)}).${tag}";
foldedList =
value:
assert h.hasType k.attrs value;
{
inherit value;
__luaEncoderTag = "foldedList";
};
thunk = code: _: lua code;
return = code: lua "return ${encode code}";
vim = lua "vim";
print = lua "print";
require = lua "require";
tempest = lua "D.tempest";
do = actions: lua (lib.concatStringsSep "\n" (lib.forEach actions encode));
args = values: lua (lib.concatStringsSep ", " (lib.forEach values encode));
none = lua "";
tempestConfigure =
given: context:
lua ''
D.tempest.configure(
${encode given},
${context}
)
'';
tempestBufnr =
given:
lua ''
function(_, bufnr)
return D.tempest.configure(
${encode given},
{ bufnr = bufnr}
)
end
'';
keymap = mode: mapping: action: desc: {
inherit
mode
mapping
action
desc
;
};
nmap = mapping: action: desc: { inherit mapping action desc; };
unmap = mapping: {
inherit mapping;
action = "<nop>";
};
blacklist =
given:
assert h.hasType (types.oneOrMany types.neovimEnv) given;
# lua
lua ''
D.tempest.blacklist(${encode given})
'';
whitelist =
given:
assert h.hasType (types.oneOrMany types.neovimEnv) given;
# lua
lua ''
D.tempest.whitelist(${encode given})
'';
# :p => expands path
# :h => returns the head of the path
notmp = lua ''vim.fn.expand("%:p:h") ~= "/tmp"'';
# }}}
# {{{ Main config generation entrypoint
generateConfig =
rawConfig:
assert h.hasType types.neovimConfig rawConfig;
let
config = {
lazy = { };
pre = { };
post = { };
} // rawConfig;
collectNixDeps =
lazyModule:
if lazyModule ? dependencies then
let
nix = lazyModule.dependencies.nix or [ ];
other = lib.pipe (lazyModule.dependencies.lua or [ ]) [
(lib.lists.map collectNixDeps)
lib.lists.flatten
];
in
nix ++ other
else
[ ];
dependencies = lib.pipe config.lazy [
(lib.mapAttrsToList (_: collectNixDeps))
lib.lists.flatten
];
processedLazyModules = lib.mapAttrs (
name: module:
{ inherit name; } // module // { dependencies = (module.dependencies or { }).lua or null; }
) config.lazy;
luaConfig = ''
local M = {}
local D = {
tempest = require(${encode tempestModule}),
}
-- {{{ Pre-plugin config
M.pre = ${encode (foldedList config.pre)}
-- }}}
-- {{{ Lazy modules
M.lazy = ${encode (foldedList processedLazyModules)}
D.tempest.prepareLazySpec(M.lazy)
-- }}}
-- {{{ Post-plugin config
M.post = ${encode (foldedList config.post)}
-- }}}
return M
'';
in
{
inherit dependencies;
lua = luaConfig;
};
# }}}
};
in
mkLib