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 sed -i "s@#define LUA_ROOT[[:space:]]*\"/usr/local/\"@#define LUA_ROOT \"$out/\"@g" src/luaconf.h
89
90 # abort if patching didn't work
91 grep $out src/luaconf.h
92 ''
93 + lib.optionalString (!stdenv.hostPlatform.isDarwin && !staticOnly) ''
94 # Add a target for a shared library to the Makefile.
95 sed -e '1s/^/LUA_SO = liblua.so/' \
96 -e 's/ALL_T *= */&$(LUA_SO) /' \
97 -i src/Makefile
98 cat ${./lua-dso.make} >> src/Makefile
99 '';
100
101 env = {
102 inherit luaversion;
103 pkgversion = version;
104 };
105
106 # see configurePhase for additional flags (with space)
107 makeFlags = [
108 "INSTALL_TOP=${placeholder "out"}"
109 "INSTALL_MAN=${placeholder "out"}/share/man/man1"
110 "R=${version}"
111 "LDFLAGS=-fPIC"
112 "V=${luaversion}"
113 "PLAT=${plat}"
114 "CC=${stdenv.cc.targetPrefix}cc"
115 "RANLIB=${stdenv.cc.targetPrefix}ranlib"
116 # Lua links with readline which depends on ncurses. For some reason when
117 # building pkgsStatic.lua it fails because symbols from ncurses are not
118 # found. Adding ncurses here fixes the problem.
119 "MYLIBS=-lncurses"
120 ];
121
122 configurePhase = ''
123 runHook preConfigure
124
125 makeFlagsArray+=(CFLAGS='-O2 -fPIC${lib.optionalString compat compatFlags} $(${
126 if lib.versionAtLeast luaversion "5.2" then "SYSCFLAGS" else "MYCFLAGS"
127 })' )
128 makeFlagsArray+=(${lib.optionalString stdenv.hostPlatform.isDarwin "CC=\"$CC\""}${
129 lib.optionalString (
130 stdenv.buildPlatform != stdenv.hostPlatform
131 ) " 'AR=${stdenv.cc.targetPrefix}ar rcu'"
132 })
133
134 installFlagsArray=( TO_BIN="lua luac" INSTALL_DATA='cp -d' \
135 TO_LIB="${
136 if stdenv.hostPlatform.isDarwin then
137 "liblua.${finalAttrs.version}.dylib"
138 else
139 (
140 "liblua.a"
141 + lib.optionalString (
142 !staticOnly
143 ) " liblua.so liblua.so.${luaversion} liblua.so.${finalAttrs.version}"
144 )
145 }" )
146
147 runHook postConfigure
148 '';
149 inherit postConfigure;
150
151 inherit postBuild;
152
153 postInstall = ''
154 mkdir -p "$out/nix-support" "$out/share/doc/lua" "$out/lib/pkgconfig"
155 cp ${
156 replaceVars ./utils.sh {
157 luapathsearchpaths = lib.escapeShellArgs finalAttrs.LuaPathSearchPaths;
158 luacpathsearchpaths = lib.escapeShellArgs finalAttrs.LuaCPathSearchPaths;
159 }
160 } $out/nix-support/utils.sh
161 mv "doc/"*.{gif,png,css,html} "$out/share/doc/lua/"
162 rmdir $out/{share,lib}/lua/${luaversion} $out/{share,lib}/lua
163 mkdir -p "$out/lib/pkgconfig"
164
165 cat >"$out/lib/pkgconfig/lua.pc" <<EOF
166 prefix=$out
167 libdir=$out/lib
168 includedir=$out/include
169 INSTALL_BIN=$out/bin
170 INSTALL_INC=$out/include
171 INSTALL_LIB=$out/lib
172 INSTALL_MAN=$out/man/man1
173
174 Name: Lua
175 Description: An Extensible Extension Language
176 Version: ${finalAttrs.version}
177 Requires:
178 Libs: -L$out/lib -llua
179 Cflags: -I$out/include
180 EOF
181 ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua-${luaversion}.pc"
182 ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua${luaversion}.pc"
183 ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua${
184 lib.replaceStrings [ "." ] [ "" ] luaversion
185 }.pc"
186
187 # Make documentation outputs of different versions co-installable.
188 mv $out/share/doc/lua $out/share/doc/lua-${finalAttrs.version}
189 '';
190
191 # copied from python
192 passthru =
193 let
194 # When we override the interpreter we also need to override the spliced versions of the interpreter
195 inputs' = lib.filterAttrs (n: v: !lib.isDerivation v && n != "passthruFun") inputs;
196 override =
197 attr:
198 let
199 lua = attr.override (inputs' // { self = lua; });
200 in
201 lua;
202 in
203 passthruFun rec {
204 inherit
205 self
206 luaversion
207 packageOverrides
208 luaAttr
209 ;
210 executable = "lua";
211 luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr};
212 luaOnBuildForHost = override pkgsBuildHost.${luaAttr};
213 luaOnBuildForTarget = override pkgsBuildTarget.${luaAttr};
214 luaOnHostForHost = override pkgsHostHost.${luaAttr};
215 luaOnTargetForTarget = lib.optionalAttrs (lib.hasAttr luaAttr pkgsTargetTarget) (
216 override pkgsTargetTarget.${luaAttr}
217 );
218 };
219
220 meta = {
221 homepage = "https://www.lua.org";
222 description = "Powerful, fast, lightweight, embeddable scripting language";
223 longDescription = ''
224 Lua combines simple procedural syntax with powerful data
225 description constructs based on associative arrays and extensible
226 semantics. Lua is dynamically typed, runs by interpreting bytecode
227 for a register-based virtual machine, and has automatic memory
228 management with incremental garbage collection, making it ideal
229 for configuration, scripting, and rapid prototyping.
230 '';
231 mainProgram = "lua";
232 license = lib.licenses.mit;
233 platforms = lib.platforms.unix;
234 };
235 }
236)