nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 buildPackages,
5 version,
6 src,
7 replaceVars,
8 extraMeta ? { },
9 self,
10 packageOverrides ? (final: prev: { }),
11 pkgsBuildBuild,
12 pkgsBuildHost,
13 pkgsBuildTarget,
14 pkgsHostHost,
15 pkgsTargetTarget,
16 passthruFun,
17 enableFFI ? true,
18 enableJIT ? true,
19 enableJITDebugModule ? enableJIT,
20 enableGC64 ? true,
21 enable52Compat ? false,
22 enableValgrindSupport ? false,
23 valgrind ? null,
24 enableGDBJITSupport ? false,
25 enableAPICheck ? false,
26 enableVMAssertions ? false,
27 enableRegisterAllocationRandomization ? false,
28 useSystemMalloc ? false,
29 # Upstream generates randomized string id's by default for security reasons
30 # https://github.com/LuaJIT/LuaJIT/issues/626. Deterministic string id's should
31 # never be needed for correctness (that should be fixed in the lua code),
32 # but may be helpful when you want to embed jit-compiled raw lua blobs in
33 # binaries that you want to be reproducible.
34 deterministicStringIds ? false,
35 luaAttr ? "luajit_${lib.versions.major version}_${lib.versions.minor version}",
36}@inputs:
37assert enableJITDebugModule -> enableJIT;
38assert enableGDBJITSupport -> enableJIT;
39assert enableValgrindSupport -> valgrind != null;
40let
41
42 luaPackages = self.pkgs;
43
44 XCFLAGS =
45 lib.optional (!enableFFI) "-DLUAJIT_DISABLE_FFI"
46 ++ lib.optional (!enableJIT) "-DLUAJIT_DISABLE_JIT"
47 ++ lib.optional enable52Compat "-DLUAJIT_ENABLE_LUA52COMPAT"
48 ++ lib.optional (!enableGC64) "-DLUAJIT_DISABLE_GC64"
49 ++ lib.optional useSystemMalloc "-DLUAJIT_USE_SYSMALLOC"
50 ++ lib.optional enableValgrindSupport "-DLUAJIT_USE_VALGRIND"
51 ++ lib.optional enableGDBJITSupport "-DLUAJIT_USE_GDBJIT"
52 ++ lib.optional enableAPICheck "-DLUAJIT_USE_APICHECK"
53 ++ lib.optional enableVMAssertions "-DLUAJIT_USE_ASSERT"
54 ++ lib.optional enableRegisterAllocationRandomization "-DLUAJIT_RANDOM_RA"
55 ++ lib.optional deterministicStringIds "-DLUAJIT_SECURITY_STRID=0";
56
57 # LuaJIT requires build for 32bit architectures to be build on x86 not x86_64
58 # TODO support also other build architectures. The ideal way would be to use
59 # stdenv_32bit but that doesn't work due to host platform mismatch:
60 # https://github.com/NixOS/nixpkgs/issues/212494
61 buildStdenv =
62 if buildPackages.stdenv.hostPlatform.isx86_64 && stdenv.hostPlatform.is32bit then
63 buildPackages.pkgsi686Linux.buildPackages.stdenv
64 else
65 buildPackages.stdenv;
66
67in
68stdenv.mkDerivation (finalAttrs: {
69 pname = "luajit";
70 inherit version src;
71
72 luaversion = "5.1";
73
74 postPatch = ''
75 substituteInPlace Makefile --replace ldconfig :
76 if test -n "''${dontStrip-}"; then
77 # CCDEBUG must be non-empty or everything will be stripped, -g being
78 # passed by nixpkgs CC wrapper is insufficient on its own
79 substituteInPlace src/Makefile --replace-fail "#CCDEBUG= -g" "CCDEBUG= -g"
80 fi
81 '';
82
83 dontConfigure = true;
84
85 buildInputs = lib.optional enableValgrindSupport valgrind;
86
87 buildFlags = [
88 "amalg" # Build highly optimized version
89 ];
90 makeFlags = [
91 "PREFIX=$(out)"
92 "DEFAULT_CC=cc"
93 "CROSS=${stdenv.cc.targetPrefix}"
94 "HOST_CC=${buildStdenv.cc}/bin/cc"
95 ]
96 # LuaJIT's build system needs an explicit target on MinGW, otherwise it can
97 # emit ELF-specific assembler directives (e.g. .hidden/.type/.size) that the
98 # PE/COFF toolchain doesn't accept.
99 ++ lib.optionals stdenv.hostPlatform.isMinGW [
100 "TARGET_SYS=Windows"
101 ]
102 ++ lib.optional enableJITDebugModule "INSTALL_LJLIBD=$(INSTALL_LMOD)"
103 ++ lib.optional stdenv.hostPlatform.isStatic "BUILDMODE=static";
104 enableParallelBuilding = true;
105 env.NIX_CFLAGS_COMPILE = toString XCFLAGS;
106
107 # The LuaJIT build produces `src/luajit.exe` on Windows targets, but the
108 # upstream install rule expects `src/luajit`. Provide a compatibility copy.
109 preInstall = lib.optionalString stdenv.hostPlatform.isMinGW ''
110 if [[ -e src/luajit.exe && ! -e src/luajit ]]; then
111 cp -p src/luajit.exe src/luajit
112 fi
113 '';
114
115 postInstall = ''
116 mkdir -p $out/nix-support
117 cp ${
118 replaceVars ../lua-5/utils.sh {
119 luapathsearchpaths = lib.escapeShellArgs finalAttrs.LuaPathSearchPaths;
120 luacpathsearchpaths = lib.escapeShellArgs finalAttrs.LuaCPathSearchPaths;
121 }
122 } $out/nix-support/utils.sh
123 ( cd "$out/include"; ln -s luajit-*/* . )
124 ln -s "$out"/bin/luajit-* "$out"/bin/lua
125 if [[ ! -e "$out"/bin/luajit ]]; then
126 ln -s "$out"/bin/luajit* "$out"/bin/luajit
127 fi
128 '';
129
130 LuaPathSearchPaths = luaPackages.luaLib.luaPathList;
131 LuaCPathSearchPaths = luaPackages.luaLib.luaCPathList;
132
133 setupHook = builtins.toFile "lua-setup-hook" ''
134 source @out@/nix-support/utils.sh
135 addEnvHooks "$hostOffset" luaEnvHook
136 '';
137
138 # copied from python
139 passthru =
140 let
141 # When we override the interpreter we also need to override the spliced versions of the interpreter
142 inputs' = lib.filterAttrs (n: v: !lib.isDerivation v && n != "passthruFun") inputs;
143 override =
144 attr:
145 let
146 lua = attr.override (inputs' // { self = lua; });
147 in
148 lua;
149 in
150 passthruFun rec {
151 inherit self packageOverrides luaAttr;
152 inherit (finalAttrs) luaversion;
153 executable = "lua";
154 luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr};
155 luaOnBuildForHost = override pkgsBuildHost.${luaAttr};
156 luaOnBuildForTarget = override pkgsBuildTarget.${luaAttr};
157 luaOnHostForHost = override pkgsHostHost.${luaAttr};
158 luaOnTargetForTarget = lib.optionalAttrs (lib.hasAttr luaAttr pkgsTargetTarget) (
159 override pkgsTargetTarget.${luaAttr}
160 );
161 };
162
163 meta =
164
165 {
166 description = "High-performance JIT compiler for Lua 5.1";
167 homepage = "https://luajit.org/";
168 license = lib.licenses.mit;
169 # MSYS2 ships LuaJIT for mingw-w64, and nixpkgs consumers (like phosphor)
170 # need it in Windows cross builds.
171 platforms = lib.platforms.linux ++ lib.platforms.darwin ++ lib.platforms.windows;
172 badPlatforms = [
173 "loongarch64-linux" # See https://github.com/LuaJIT/LuaJIT/issues/1278
174 "riscv64-linux" # See https://github.com/LuaJIT/LuaJIT/issues/628
175 "powerpc64le-linux" # `#error "No support for PPC64"`
176 ];
177 mainProgram = "lua";
178 maintainers = with lib.maintainers; [
179 thoughtpolice
180 vcunat
181 lblasc
182 ];
183 }
184 // extraMeta;
185})