Skip to content

Commit 8884291

Browse files
author
Matthieu Coudron
committed
lua: introduced a lua lib
Goal is to improve separation between packages and utilities. Can help with autocompletion/navigate nixpkgs faster. Also it will help standardize how LUA_PATH is exported across packages, so that one can more easily make lua changes across nixpkgs (for instance changing where lua modules are installed).
1 parent 0b6d33c commit 8884291

11 files changed

Lines changed: 162 additions & 97 deletions

File tree

pkgs/applications/editors/neovim/tests.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ rec {
133133
configure.pathogen.pluginNames = [ "vim-nix" ];
134134
};
135135

136-
nvimWithLuaPackages = wrapNeovim2 "with-lua-packages" (makeNeovimConfig {
136+
nvimWithLuaPackages = wrapNeovim2 "-with-lua-packages" (makeNeovimConfig {
137137
extraLuaPackages = ps: [ps.mpack];
138138
customRC = ''
139139
lua require("mpack")
140140
'';
141141
});
142142

143143
nvim_with_lua_packages = runTest nvimWithLuaPackages ''
144+
export HOME=$TMPDIR
144145
${nvimWithLuaPackages}/bin/nvim -i NONE --noplugin -es
145146
'';
146147
})

pkgs/applications/editors/neovim/utils.nix

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ let
7878
++ (extraPython3Packages ps)
7979
++ (lib.concatMap (f: f ps) pluginPython3Packages));
8080

81-
lua = neovim-unwrapped.lua;
82-
luaEnv = lua.withPackages(ps: extraLuaPackages ps);
81+
luaEnv = neovim-unwrapped.lua.withPackages(extraLuaPackages);
8382

8483
# Mapping a boolean argument to a key that tells us whether to add or not to
8584
# add to nvim's 'embedded rc' this:
@@ -115,8 +114,8 @@ let
115114
] ++ lib.optionals (binPath != "") [
116115
"--suffix" "PATH" ":" binPath
117116
] ++ lib.optionals (luaEnv != null) [
118-
"--prefix" "LUA_PATH" ";" "${luaEnv}/share/lua/${lua.luaversion}/?.lua"
119-
"--prefix" "LUA_CPATH" ";" "${luaEnv}/lib/lua/${lua.luaversion}/?.so"
117+
"--prefix" "LUA_PATH" ";" (neovim-unwrapped.lua.pkgs.lib.genLuaPathAbsStr luaEnv)
118+
"--prefix" "LUA_CPATH" ";" (neovim-unwrapped.lua.pkgs.lib.genLuaCPathAbsStr luaEnv)
120119
];
121120

122121

pkgs/development/interpreters/lua-5/build-lua-package.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ builtins.removeAttrs attrs ["disabled" "checkInputs" "externalDeps" "extraVariab
238238
inherit externalDeps;
239239
} // passthru;
240240

241-
meta = with lib.maintainers; {
241+
meta = {
242242
platforms = lua.meta.platforms;
243243
# add extra maintainer(s) to every package
244244
maintainers = (meta.maintainers or []) ++ [ ];

pkgs/development/interpreters/lua-5/interpreter.nix

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{ lib, stdenv, fetchurl, readline
22
, compat ? false
33
, callPackage
4-
, packageOverrides ? (self: super: {})
4+
, makeWrapper
5+
, packageOverrides ? (final: prev: {})
56
, sourceVersion
67
, hash
78
, patches ? []
@@ -10,7 +11,9 @@
1011
, staticOnly ? stdenv.hostPlatform.isStatic
1112
}:
1213
let
13-
luaPackages = callPackage ../../lua-modules {lua=self; overrides=packageOverrides;};
14+
luaPackages = callPackage ../../lua-modules {
15+
lua=self; overrides=packageOverrides;
16+
};
1417

1518
plat = if stdenv.isLinux then "linux"
1619
else if stdenv.isDarwin then "macosx"
@@ -31,21 +34,32 @@ self = stdenv.mkDerivation rec {
3134
sha256 = hash;
3235
};
3336

34-
LuaPathSearchPaths = luaPackages.getLuaPathList luaversion;
35-
LuaCPathSearchPaths = luaPackages.getLuaCPathList luaversion;
37+
LuaPathSearchPaths = luaPackages.lib.luaPathList;
38+
LuaCPathSearchPaths = luaPackages.lib.luaCPathList;
3639
setupHook = luaPackages.lua-setup-hook LuaPathSearchPaths LuaCPathSearchPaths;
3740

41+
nativeBuildInputs = [ makeWrapper ];
3842
buildInputs = [ readline ];
3943

4044
inherit patches;
4145

42-
postPatch = lib.optionalString (!stdenv.isDarwin && !staticOnly) ''
46+
# we can't pass flags to the lua makefile because for portability, everything is hardcoded
47+
postPatch = ''
48+
{
49+
echo -e '
50+
#undef LUA_PATH_DEFAULT
51+
#define LUA_PATH_DEFAULT "./share/lua/${luaversion}/?.lua;./?.lua;./?/init.lua"
52+
#undef LUA_CPATH_DEFAULT
53+
#define LUA_CPATH_DEFAULT "./lib/lua/${luaversion}/?.so;./?.so;./lib/lua/${luaversion}/loadall.so"
54+
'
55+
} >> src/luaconf.h
56+
'' + lib.optionalString (!stdenv.isDarwin && !staticOnly) ''
4357
# Add a target for a shared library to the Makefile.
4458
sed -e '1s/^/LUA_SO = liblua.so/' \
4559
-e 's/ALL_T *= */&$(LUA_SO) /' \
4660
-i src/Makefile
4761
cat ${./lua-dso.make} >> src/Makefile
48-
'';
62+
'' ;
4963

5064
# see configurePhase for additional flags (with space)
5165
makeFlags = [

pkgs/development/interpreters/lua-5/wrap-lua.nix

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
, makeWrapper
55
}:
66

7-
with lib;
8-
97
# defined in trivial-builders.nix
108
# imported as wrapLua in lua-packages.nix and passed to build-lua-derivation to be used as buildInput
119
makeSetupHook {
12-
deps = makeWrapper;
13-
substitutions.executable = lua.interpreter;
14-
substitutions.lua = lua;
15-
substitutions.LuaPathSearchPaths = lib.escapeShellArgs lua.LuaPathSearchPaths;
16-
substitutions.LuaCPathSearchPaths = lib.escapeShellArgs lua.LuaPathSearchPaths;
17-
10+
deps = makeWrapper;
11+
substitutions.executable = lua.interpreter;
12+
substitutions.lua = lua;
13+
substitutions.LuaPathSearchPaths = lib.escapeShellArgs lua.LuaPathSearchPaths;
14+
substitutions.LuaCPathSearchPaths = lib.escapeShellArgs lua.LuaPathSearchPaths;
1815
} ./wrap.sh
1916

pkgs/development/interpreters/lua-5/wrapper.nix

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
let
1212
env = let
1313
paths = requiredLuaModules (extraLibs ++ [ lua ] );
14-
in (buildEnv {
14+
in buildEnv {
1515
name = "${lua.name}-env";
1616

1717
inherit paths;
1818
inherit ignoreCollisions;
1919
extraOutputsToInstall = [ "out" ] ++ extraOutputsToInstall;
2020

21+
nativeBuildInputs = [
22+
makeWrapper
23+
(lua.pkgs.lua-setup-hook lua.LuaPathSearchPaths lua.LuaCPathSearchPaths)
24+
];
25+
2126
# we create wrapper for the binaries in the different packages
2227
postBuild = ''
2328
if [ -L "$out/bin" ]; then
@@ -37,7 +42,12 @@ let
3742
rm -f "$out/bin/$prg"
3843
if [ -x "$prg" ]; then
3944
nix_debug "Making wrapper $prg"
40-
makeWrapper "$path/bin/$prg" "$out/bin/$prg" --suffix LUA_PATH ';' "$LUA_PATH" --suffix LUA_CPATH ';' "$LUA_CPATH" ${lib.concatStringsSep " " makeWrapperArgs}
45+
makeWrapper "$path/bin/$prg" "$out/bin/$prg" \
46+
--set-default LUA_PATH ";;" \
47+
--suffix LUA_PATH ';' "$LUA_PATH" \
48+
--set-default LUA_CPATH ";;" \
49+
--suffix LUA_CPATH ';' "$LUA_CPATH" \
50+
${lib.concatStringsSep " " makeWrapperArgs}
4151
fi
4252
fi
4353
done
@@ -62,8 +72,5 @@ let
6272
'';
6373
};
6474
};
65-
}).overrideAttrs (_: {
66-
# Add extra deps needed for postBuild hook.
67-
nativeBuildInputs = [ makeWrapper lua ];
68-
});
75+
};
6976
in env

pkgs/development/interpreters/luajit/default.nix

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
, extraMeta ? { }
1111
, callPackage
1212
, self
13-
, packageOverrides ? (self: super: { })
13+
, packageOverrides ? (final: prev: {})
1414
, enableFFI ? true
1515
, enableJIT ? true
1616
, enableJITDebugModule ? enableJIT
@@ -62,6 +62,15 @@ stdenv.mkDerivation rec {
6262
# passed by nixpkgs CC wrapper is insufficient on its own
6363
substituteInPlace src/Makefile --replace "#CCDEBUG= -g" "CCDEBUG= -g"
6464
fi
65+
66+
{
67+
echo -e '
68+
#undef LUA_PATH_DEFAULT
69+
#define LUA_PATH_DEFAULT "./share/lua/${luaversion}/?.lua;./?.lua;./?/init.lua"
70+
#undef LUA_CPATH_DEFAULT
71+
#define LUA_CPATH_DEFAULT "./lib/lua/${luaversion}/?.so;./?.so;./lib/lua/${luaversion}/loadall.so"
72+
'
73+
} >> src/luaconf.h
6574
'';
6675

6776
configurePhase = false;
@@ -88,15 +97,10 @@ stdenv.mkDerivation rec {
8897
ln -s "$out"/bin/luajit-* "$out"/bin/luajit
8998
'';
9099

91-
LuaPathSearchPaths = [
92-
"lib/lua/${luaversion}/?.lua"
93-
"share/lua/${luaversion}/?.lua"
94-
"share/lua/${luaversion}/?/init.lua"
95-
"lib/lua/${luaversion}/?/init.lua"
96-
"share/${name}/?.lua"
97-
];
98-
LuaCPathSearchPaths = [ "lib/lua/${luaversion}/?.so" "share/lua/${luaversion}/?.so" ];
99-
setupHook = luaPackages.lua-setup-hook LuaPathSearchPaths LuaCPathSearchPaths;
100+
LuaPathSearchPaths = luaPackages.lib.luaPathList;
101+
LuaCPathSearchPaths = luaPackages.lib.luaCPathList;
102+
103+
setupHook = luaPackages.lua-setup-hook luaPackages.lib.luaPathList luaPackages.lib.luaCPathList;
100104

101105
passthru = rec {
102106
buildEnv = callPackage ../lua-5/wrapper.nix {

pkgs/development/lua-modules/default.nix

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# inspired by pkgs/development/haskell-modules/default.nix
22
{ pkgs, lib
33
, lua
4-
, overrides ? (self: super: {})
4+
, overrides ? (final: prev: {})
55
}:
66

77
let
@@ -15,7 +15,7 @@ let
1515
overridenPackages = import ./overrides.nix { inherit pkgs; };
1616

1717
generatedPackages = if (builtins.pathExists ./generated-packages.nix) then
18-
pkgs.callPackage ./generated-packages.nix { } else (self: super: {});
18+
pkgs.callPackage ./generated-packages.nix { } else (final: prev: {});
1919

2020
extensible-self = lib.makeExtensible
2121
(extends overrides
@@ -24,7 +24,6 @@ let
2424
initialPackages
2525
)
2626
)
27-
)
28-
;
27+
);
2928
in
3029
extensible-self
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{ pkgs, lib, lua }:
2+
let
3+
requiredLuaModules = drvs: with lib; let
4+
modules = filter hasLuaModule drvs;
5+
in unique ([lua] ++ modules ++ concatLists (catAttrs "requiredLuaModules" modules));
6+
# Check whether a derivation provides a lua module.
7+
hasLuaModule = drv: drv ? luaModule;
8+
in
9+
rec {
10+
inherit hasLuaModule requiredLuaModules;
11+
12+
luaPathList = [
13+
"share/lua/${lua.luaversion}/?.lua"
14+
"share/lua/${lua.luaversion}/?/init.lua"
15+
];
16+
luaCPathList = [
17+
"lib/lua/${lua.luaversion}/?.so"
18+
];
19+
20+
/* generate paths without a prefix
21+
*/
22+
luaPathRelStr = lib.concatStringsSep ";" luaPathList;
23+
luaCPathRelStr = lib.concatStringsSep ";" luaCPathList;
24+
25+
/* generate LUA_(C)PATH value for a specific derivation, i.e., with absolute paths
26+
*/
27+
genLuaPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaPathList;
28+
genLuaCPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaCPathList;
29+
30+
/* Generate a LUA_PATH with absolute paths
31+
*/
32+
# genLuaPathAbs = drv:
33+
# lib.concatStringsSep ";" (map (x: "${drv}/x") luaPathList);
34+
35+
luaAtLeast = lib.versionAtLeast lua.luaversion;
36+
luaOlder = lib.versionOlder lua.luaversion;
37+
isLua51 = (lib.versions.majorMinor lua.version) == "5.1";
38+
isLua52 = (lib.versions.majorMinor lua.version) == "5.2";
39+
isLua53 = lua.luaversion == "5.3";
40+
isLuaJIT = lib.getName lua == "luajit";
41+
42+
/* generates the relative path towards the folder where
43+
seems stable even when using lua_modules_path = ""
44+
45+
Example:
46+
getDataFolder luaPackages.stdlib
47+
=> stdlib-41.2.2-1-rocks/stdlib/41.2.2-1/doc
48+
*/
49+
getDataFolder = drv:
50+
"${drv.pname}-${drv.version}-rocks/${drv.pname}/${drv.version}";
51+
52+
/* Convert derivation to a lua module.
53+
so that luaRequireModules can be run later
54+
*/
55+
toLuaModule = drv:
56+
drv.overrideAttrs( oldAttrs: {
57+
# Use passthru in order to prevent rebuilds when possible.
58+
passthru = (oldAttrs.passthru or {})// {
59+
luaModule = lua;
60+
requiredLuaModules = requiredLuaModules drv.propagatedBuildInputs;
61+
};
62+
});
63+
}

pkgs/development/lua-modules/overrides.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ with super;
363363
'';
364364
});
365365

366+
# TODO just while testing, remove afterwards
367+
# toVimPlugin should do it instead
368+
gitsigns-nvim = super.gitsigns-nvim.overrideAttrs(oa: {
369+
nativeBuildInputs = oa.nativeBuildInputs or [] ++ [ pkgs.vimUtils.vimGenDocHook ];
370+
});
371+
366372
# aliases
367373
cjson = super.lua-cjson;
368374
}

0 commit comments

Comments
 (0)