-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefault.nix
254 lines (248 loc) · 7.38 KB
/
default.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
{
flake-parts-lib,
lib,
...
}:
with lib; let
inherit (flake-parts-lib) mkPerSystemOption;
pluginSpec = with types; {
options = {
enabled = mkOption {
type = nullOr (oneOf [bool str]);
default = null;
};
src = mkOption {
type = nullOr (oneOf [attrs path]);
default = null;
};
package = mkOption {
type = nullOr package;
default = null;
};
dev = mkOption {
type = nullOr bool;
default = null;
};
name = mkOption {
type = nullOr str;
default = null;
};
lazy = mkOption {
type = nullOr bool;
default = null;
};
dependencies = mkOption {
type = attrsOf (submodule pluginSpec);
default = {};
};
init = mkOption {
type = nullOr (oneOf [package path str]);
default = null;
};
config = mkOption {
type = nullOr (oneOf [attrs bool package path str]);
default = null;
};
opts = mkOption {
type = attrs;
default = {};
};
event = mkOption {
type = nullOr (oneOf [str (listOf str)]);
default = null;
};
cmd = mkOption {
type = nullOr (oneOf [str (listOf str)]);
default = null;
};
ft = mkOption {
type = nullOr (oneOf [str (listOf str)]);
default = null;
};
keys = mkOption {
type = nullOr str;
default = null;
};
main = mkOption {
type = nullOr str;
default = null;
};
priority = mkOption {
type = nullOr int;
default = null;
};
paths = mkOption {
type = listOf package;
default = [];
};
cpath = mkOption {
# TODO: nullOr (functionTo str);
type = nullOr str;
default = null;
};
};
};
in {
options = {
perSystem = mkPerSystemOption ({
config,
pkgs,
...
}: {
options = with types; {
neovim = {
lazy = {
package = mkOption {
type = package;
default = pkgs.vimPlugins.lazy-nvim;
};
settings = mkOption {
type = submodule {
freeformType = attrsOf anything;
options = {
dev = {
path = mkOption {
type = nullOr (oneOf [path str]);
default = null;
};
};
install = {
missing = mkOption {
type = bool;
default = false;
};
};
};
};
};
plugins = mkOption {
type = attrsOf (submodule pluginSpec);
default = {};
};
};
build = {
lazy = mkOption {
type = str;
internal = true;
};
plugins = mkOption {
type = package;
internal = true;
};
};
};
};
});
};
config = {
perSystem = {
config,
pkgs,
...
}: let
cfg = config.neovim.lazy;
in {
neovim = let
mapPluginsRec = fn: mapAttrsToList (name: attrs: (fn name attrs) ++ (mapAttrsToList fn attrs.dependencies)) cfg.plugins;
in {
build = let
inherit (config.neovim) build;
inherit (pkgs.vimUtils) buildVimPlugin;
mkPlugin = name: attrs:
if attrs.package != null
then attrs.package
else
buildVimPlugin {
inherit name;
inherit (attrs) src;
leaveDotGit = true; # So some lazy features (commands) work properly
};
in {
lazy = let
toPlugin' = name: attrs: let
package = mkPlugin name attrs;
in
{
inherit name;
dir = "${package}";
}
// optionalAttrs (isBool attrs.enabled) {inherit (attrs) enabled;}
// optionalAttrs (isString attrs.enabled) {
enabled = lib.generators.mkLuaInline attrs.enabled;
}
// optionalAttrs (isBool attrs.dev && attrs.dev && isString cfg.settings.dev.path) {
dir = "${cfg.settings.dev.path}/${name}";
}
// optionalAttrs (attrs.lazy != null) {inherit (attrs) lazy;}
// optionalAttrs (attrs.dependencies != {}) {
dependencies = let
deps = mapAttrs toPlugin' attrs.dependencies;
in
attrValues deps;
}
// optionalAttrs (isString attrs.init) {
init = lib.generators.mkLuaInline attrs.init;
}
// optionalAttrs (isDerivation attrs.init || isPath attrs.init) {
init = lib.generators.mkLuaInline ''dofile "${attrs.init}"'';
}
// optionalAttrs (isBool attrs.config) {
inherit (attrs) config;
}
// optionalAttrs (isString attrs.config) {
config = lib.generators.mkLuaInline attrs.config;
}
// optionalAttrs (isDerivation attrs.config || isPath attrs.config) {
config = lib.generators.mkLuaInline ''dofile "${attrs.config}"'';
}
// optionalAttrs (isAttrs attrs.config) {
config = true;
opts = attrs.config;
}
// optionalAttrs (attrs.event != null) {inherit (attrs) event;}
// optionalAttrs (attrs.cmd != null) {inherit (attrs) cmd;}
// optionalAttrs (attrs.ft != null) {inherit (attrs) ft;}
// optionalAttrs (attrs.keys != null) {
keys = lib.generators.mkLuaInline attrs.keys;
}
// optionalAttrs (attrs.main != null) {inherit (attrs) main;}
// optionalAttrs (attrs.priority != null) {inherit (attrs) priority;};
in
lib.generators.toLua {} (lib.recursiveUpdate cfg.settings {
spec = mapAttrsToList toPlugin' cfg.plugins;
performance.rtp.reset = false;
});
plugins =
pkgs.runCommand "plugins.lua" {
nativeBuildInputs = with pkgs; [stylua];
passAsFile = ["text"];
preferLocalBuild = true;
allowSubstitutes = false;
text = ''
-- Generated by Nix (via github:willruggiano/neovim.nix)
vim.opt.rtp:prepend "${cfg.package}"
require("lazy").setup(${build.lazy})
'';
} ''
target=$out
mkdir -p "$(dirname "$target")"
if [ -e "$textPath" ]; then
mv "$textPath" "$target"
else
echo -n "$text" > "$target"
fi
stylua --config-path ${../../stylua.toml} $target
'';
};
cpaths = let
cpaths = mapPluginsRec (_: attrs: optional (attrs.cpath != null) attrs.cpath);
in
mkAfter (flatten cpaths);
paths = let
paths = mapPluginsRec (name: attrs: attrs.paths);
in
mkAfter (flatten paths);
};
};
};
}