1{
2 lib,
3 stdenv,
4 fetchurl,
5 readline,
6 compat ? false,
7 makeWrapper,
8 self,
9 packageOverrides ? (final: prev: { }),
10 replaceVars,
11 pkgsBuildBuild,
12 pkgsBuildHost,
13 pkgsBuildTarget,
14 pkgsHostHost,
15 pkgsTargetTarget,
16 version,
17 hash,
18 passthruFun,
19 patches ? [ ],
20 postConfigure ? null,
21 postBuild ? null,
22 staticOnly ? stdenv.hostPlatform.isStatic,
23 luaAttr ? "lua${lib.versions.major version}_${lib.versions.minor version}",
24}@inputs:
25
26stdenv.mkDerivation (
27 finalAttrs:
28 let
29 luaPackages = self.pkgs;
30
31 luaversion = lib.versions.majorMinor finalAttrs.version;
32
33 plat =
34 if (stdenv.hostPlatform.isLinux && lib.versionOlder self.luaversion "5.4") then
35 "linux"
36 else if (stdenv.hostPlatform.isLinux && lib.versionAtLeast self.luaversion "5.4") then
37 "linux-readline"
38 else if stdenv.hostPlatform.isDarwin then
39 "macosx"
40 else if stdenv.hostPlatform.isMinGW then
41 "mingw"
42 else if stdenv.hostPlatform.isFreeBSD then
43 "freebsd"
44 else if stdenv.hostPlatform.isSunOS then
45 "solaris"
46 else if stdenv.hostPlatform.isBSD then
47 "bsd"
48 else if stdenv.hostPlatform.isUnix then
49 "posix"
50 else
51 "generic";
52
53 compatFlags =
54 if (lib.versionOlder self.luaversion "5.3") then
55 " -DLUA_COMPAT_ALL"
56 else if (lib.versionOlder self.luaversion "5.4") then
57 " -DLUA_COMPAT_5_1 -DLUA_COMPAT_5_2"
58 else
59 " -DLUA_COMPAT_5_3";
60 in
61
62 {
63 pname = "lua";
64 inherit version;
65 outputs = [
66 "out"
67 "doc"
68 ];
69
70 src = fetchurl {
71 url = "https://www.lua.org/ftp/lua-${finalAttrs.version}.tar.gz";
72 sha256 = hash;
73 };
74
75 LuaPathSearchPaths = luaPackages.luaLib.luaPathList;
76 LuaCPathSearchPaths = luaPackages.luaLib.luaCPathList;
77 setupHook = builtins.toFile "lua-setup-hook" ''
78 source @out@/nix-support/utils.sh
79 addEnvHooks "$hostOffset" luaEnvHook
80 '';
81
82 nativeBuildInputs = [ makeWrapper ];
83 buildInputs = [ readline ];
84
85 inherit patches;
86
87 postPatch =
88 ''
89 sed -i "s@#define LUA_ROOT[[:space:]]*\"/usr/local/\"@#define LUA_ROOT \"$out/\"@g" src/luaconf.h
90
91 # abort if patching didn't work
92 grep $out src/luaconf.h
93 ''
94 + lib.optionalString (!stdenv.hostPlatform.isDarwin && !staticOnly) ''
95 # Add a target for a shared library to the Makefile.
96 sed -e '1s/^/LUA_SO = liblua.so/' \
97 -e 's/ALL_T *= */&$(LUA_SO) /' \
98 -i src/Makefile
99 cat ${./lua-dso.make} >> src/Makefile
100 '';
101
102 # see configurePhase for additional flags (with space)
103 makeFlags = [
104 "INSTALL_TOP=${placeholder "out"}"
105 "INSTALL_MAN=${placeholder "out"}/share/man/man1"
106 "R=${version}"
107 "LDFLAGS=-fPIC"
108 "V=${luaversion}"
109 "PLAT=${plat}"
110 "CC=${stdenv.cc.targetPrefix}cc"
111 "RANLIB=${stdenv.cc.targetPrefix}ranlib"
112 # Lua links with readline which depends on ncurses. For some reason when
113 # building pkgsStatic.lua it fails because symbols from ncurses are not
114 # found. Adding ncurses here fixes the problem.
115 "MYLIBS=-lncurses"
116 ];
117
118 configurePhase = ''
119 runHook preConfigure
120
121 makeFlagsArray+=(CFLAGS='-O2 -fPIC${lib.optionalString compat compatFlags} $(${
122 if lib.versionAtLeast luaversion "5.2" then "SYSCFLAGS" else "MYCFLAGS"
123 })' )
124 makeFlagsArray+=(${lib.optionalString stdenv.hostPlatform.isDarwin "CC=\"$CC\""}${
125 lib.optionalString (
126 stdenv.buildPlatform != stdenv.hostPlatform
127 ) " 'AR=${stdenv.cc.targetPrefix}ar rcu'"
128 })
129
130 installFlagsArray=( TO_BIN="lua luac" INSTALL_DATA='cp -d' \
131 TO_LIB="${
132 if stdenv.hostPlatform.isDarwin then
133 "liblua.${finalAttrs.version}.dylib"
134 else
135 (
136 "liblua.a"
137 + lib.optionalString (
138 !staticOnly
139 ) " liblua.so liblua.so.${luaversion} liblua.so.${finalAttrs.version}"
140 )
141 }" )
142
143 runHook postConfigure
144 '';
145 inherit postConfigure;
146
147 inherit postBuild;
148
149 postInstall = ''
150 mkdir -p "$out/nix-support" "$out/share/doc/lua" "$out/lib/pkgconfig"
151 cp ${
152 replaceVars ./utils.sh {
153 luapathsearchpaths = lib.escapeShellArgs finalAttrs.LuaPathSearchPaths;
154 luacpathsearchpaths = lib.escapeShellArgs finalAttrs.LuaCPathSearchPaths;
155 }
156 } $out/nix-support/utils.sh
157 mv "doc/"*.{gif,png,css,html} "$out/share/doc/lua/"
158 rmdir $out/{share,lib}/lua/${luaversion} $out/{share,lib}/lua
159 mkdir -p "$out/lib/pkgconfig"
160
161 cat >"$out/lib/pkgconfig/lua.pc" <<EOF
162 prefix=$out
163 libdir=$out/lib
164 includedir=$out/include
165 INSTALL_BIN=$out/bin
166 INSTALL_INC=$out/include
167 INSTALL_LIB=$out/lib
168 INSTALL_MAN=$out/man/man1
169
170 Name: Lua
171 Description: An Extensible Extension Language
172 Version: ${finalAttrs.version}
173 Requires:
174 Libs: -L$out/lib -llua
175 Cflags: -I$out/include
176 EOF
177 ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua-${luaversion}.pc"
178 ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua${luaversion}.pc"
179 ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua${
180 lib.replaceStrings [ "." ] [ "" ] luaversion
181 }.pc"
182
183 # Make documentation outputs of different versions co-installable.
184 mv $out/share/doc/lua $out/share/doc/lua-${finalAttrs.version}
185 '';
186
187 # copied from python
188 passthru =
189 let
190 # When we override the interpreter we also need to override the spliced versions of the interpreter
191 inputs' = lib.filterAttrs (n: v: !lib.isDerivation v && n != "passthruFun") inputs;
192 override =
193 attr:
194 let
195 lua = attr.override (inputs' // { self = lua; });
196 in
197 lua;
198 in
199 passthruFun rec {
200 inherit
201 self
202 luaversion
203 packageOverrides
204 luaAttr
205 ;
206 executable = "lua";
207 luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr};
208 luaOnBuildForHost = override pkgsBuildHost.${luaAttr};
209 luaOnBuildForTarget = override pkgsBuildTarget.${luaAttr};
210 luaOnHostForHost = override pkgsHostHost.${luaAttr};
211 luaOnTargetForTarget = lib.optionalAttrs (lib.hasAttr luaAttr pkgsTargetTarget) (
212 override pkgsTargetTarget.${luaAttr}
213 );
214 };
215
216 meta = {
217 homepage = "https://www.lua.org";
218 description = "Powerful, fast, lightweight, embeddable scripting language";
219 longDescription = ''
220 Lua combines simple procedural syntax with powerful data
221 description constructs based on associative arrays and extensible
222 semantics. Lua is dynamically typed, runs by interpreting bytecode
223 for a register-based virtual machine, and has automatic memory
224 management with incremental garbage collection, making it ideal
225 for configuration, scripting, and rapid prototyping.
226 '';
227 mainProgram = "lua";
228 license = lib.licenses.mit;
229 platforms = lib.platforms.unix;
230 };
231 }
232)