-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrapper.nix
313 lines (278 loc) · 7.98 KB
/
wrapper.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
{
lib,
makeBinaryWrapper,
neovim-node-client,
python3,
perl,
bundlerEnv,
ruby,
nodejs,
linkFarm,
runCommand,
buildEnv,
writeText,
lndir,
stdenvNoCC,
envsubst,
callPackage,
}@callPackageArgs:
lib.makeOverridable (
{
neovim,
withPython3,
extraPython3Packages,
withNodeJs,
withRuby,
withPerl,
extraLuaPackages,
plugins,
viAlias,
vimAlias,
extraBinPath,
wrapperArgs,
vimlFiles,
luaFiles,
initViml,
initLua,
appName,
desktopEntry,
devExcludedPlugins,
devPluginPaths,
dev ? false,
}@mnwWrapperArgs:
let
splitPlugins = lib.partition (x: x.optional or false) (
plugins ++ lib.optionals (!dev) devExcludedPlugins
);
optPlugins = splitPlugins.right;
startPlugins =
let
/*
Stolen from viperML
Can't call lib.unique here because of module system errors
about the same speed as using concatMap but removes a let in
*/
findDeps = builtins.foldl' (
x: y:
builtins.concatLists [
x
[
y
]
(findDeps (y.dependencies or [ ]))
]
) [ ];
in
/*
Gross edge case of optional plugin's
dependency being loaded non-optionally
only nixpkgs plugins have dependencies though
so it should be okay
*/
lib.subtractLists optPlugins ((findDeps splitPlugins.wrong) ++ (findDeps optPlugins));
allPython3Dependencies =
ps:
lib.pipe startPlugins [
(lib.concatMap (plugin: (plugin.python3Dependencies or (_: [ ])) ps))
lib.unique
];
generatedInitLua =
let
luaEnv = neovim.lua.withPackages extraLuaPackages;
inherit (neovim.lua.pkgs) luaLib;
devRtp = lib.optionalString (dev && devPluginPaths != [ ]) ''
vim.opt.runtimepath:prepend('${lib.concatStringsSep "," devPluginPaths}')
vim.opt.runtimepath:append('${lib.concatMapStringsSep "," (p: "${p}/after") devPluginPaths}')
'';
providerLua =
lib.pipe
{
node = withNodeJs;
python = false;
python3 = withPython3;
ruby = withRuby;
perl = withPerl;
}
[
(builtins.mapAttrs (
prog: withProg:
if withProg then
"vim.g.${prog}_host_prog='${providers}/bin/neovim-${prog}-host'"
else
"vim.g.loaded_${prog}_provider=0"
))
builtins.attrValues
lib.concatLines
];
sourceLua = lib.concatMapStringsSep "\n" (x: "dofile('${x}')") (
luaFiles ++ (lib.optional (initLua != "") (writeText "init.lua" initLua))
);
sourceVimL = lib.concatMapStringsSep "\n" (x: "vim.cmd('source ${x}')") (
vimlFiles ++ (lib.optional (initViml != "") (writeText "init.vim" initViml))
);
in
writeText "init.lua" ''
vim.env.PATH = vim.env.PATH .. ":${lib.makeBinPath ([ providers ] ++ extraBinPath)}"
package.path = "${luaLib.genLuaPathAbsStr luaEnv};$LUA_PATH" .. package.path
package.cpath = "${luaLib.genLuaCPathAbsStr luaEnv};$LUA_CPATH" .. package.cpath
vim.opt.packpath:append('$out')
vim.opt.runtimepath:append('$out')
${devRtp}
${providerLua}
${sourceLua}
${sourceVimL}
'';
builtConfigDir = buildEnv {
name = "neovim-pack-dir";
nativeBuildInputs = [ envsubst ];
paths =
let
vimFarm =
name: plugins:
linkFarm "${name}-configdir" (
map (drv: {
name = "pack/mnw/${name}/${
if (drv ? pname && (builtins.tryEval drv.pname).success) then drv.pname else drv.name
}";
path = drv;
}) plugins
);
in
[
(vimFarm "start" startPlugins)
(vimFarm "opt" optPlugins)
]
++ lib.optional (allPython3Dependencies python3.pkgs != [ ]) (
runCommand "vim-python3-deps" { } ''
mkdir -p $out/pack/mnw/start/__python3_dependencies
ln -s ${python3.withPackages allPython3Dependencies}/${python3.sitePackages} $out/pack/mnw/start/__python3_dependencies/python3
''
);
postBuild = ''
mkdir $out/nix-support
for i in $(find -L $out -name propagated-build-inputs ); do
cat "$i" >> $out/nix-support/propagated-build-inputs
done
# Semi-cursed helptag generation
mkdir -p $out/doc
pushd $out/doc
for ppath in ../pack/mnw/*/*/doc
do
if [ ! -e "$ppath/tags" ]; then
PLUGIN_DIR=$(basename ''${ppath::-4})
ln -snf "$ppath" "$PLUGIN_DIR"
fi
done
if [ -n "$(ls $out/doc)" ]; then
${lib.getExe neovim} -es --headless -N -u NONE -i NONE -n -V1 \
-c "helptags $out/doc" -c "quit!"
fi
popd
source '${neovim.lua}/nix-support/utils.sh'
if declare -f -F "_addToLuaPath" > /dev/null; then
_addToLuaPath "$out"
if [[ -v LUA_PATH ]]; then
LUA_PATH="$LUA_PATH;"
fi
if [[ -v LUA_CPATH ]]; then
LUA_CPATH="$LUA_CPATH;"
fi
fi
envsubst < '${generatedInitLua}' > "$out/init.lua"
'';
};
providers =
let
pythonEnv = python3.withPackages (
ps: [ ps.pynvim ] ++ (extraPython3Packages ps) ++ (allPython3Dependencies ps)
);
perlEnv = perl.withPackages (p: [
p.NeovimExt
p.Appcpanminus
]);
in
buildEnv {
name = "neovim-providers";
paths =
lib.optionals withNodeJs [
nodejs
neovim-node-client
]
++ lib.optionals withRuby [
(bundlerEnv {
name = "neovim-ruby-env";
gemdir = ./ruby_provider;
postBuild = ''
rm $out/bin/{bundle,bundler}
'';
})
ruby
];
nativeBuildInputs = [ makeBinaryWrapper ];
postBuild = ''
${lib.optionalString withPython3 ''
makeWrapper ${lib.getExe pythonEnv} $out/bin/neovim-python3-host \
--unset PYTHONPATH \
--unset PYTHONSAFEPATH
''}
${lib.optionalString withPerl "ln -s ${lib.getExe perlEnv} $out/bin/neovim-perl-host"}
'';
};
wrapperArgsStr = lib.escapeShellArgs (
[
"--set-default"
"NVIM_APPNAME"
appName
"--add-flags"
"-u ${builtConfigDir}/init.lua"
]
++ wrapperArgs
);
in
stdenvNoCC.mkDerivation {
pname = "mnw";
version = lib.getVersion neovim;
nativeBuildInputs = [
makeBinaryWrapper
lndir
];
dontUnpack = true;
strictDeps = true;
dontRewriteSymlinks = true;
installPhase = ''
runHook preInstall
# symlinkJoin
mkdir -p $out
lndir -silent ${neovim} $out
wrapProgram $out/bin/nvim ${wrapperArgsStr}
${lib.optionalString vimAlias "ln -s $out/bin/nvim $out/bin/vim"}
${lib.optionalString viAlias "ln -s $out/bin/nvim $out/bin/vi"}
${lib.optionalString (!desktopEntry) "rm -rf $out/share/applications"}
runHook postInstall
'';
# For debugging
passthru =
{
inherit builtConfigDir;
}
// lib.optionalAttrs (!dev) {
devMode = (callPackage ./wrapper.nix callPackageArgs) (mnwWrapperArgs // { dev = true; });
};
# From nixpkgs
meta = {
inherit (neovim.meta)
description
longDescription
homepage
mainProgram
license
maintainers
platforms
;
# To prevent builds on hydra
hydraPlatforms = [ ];
# prefer wrapper over the package
priority = (neovim.meta.priority or 0) - 2;
};
}
)