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 [
92 "PREFIX=$(out)"
93 "DEFAULT_CC=cc"
94 "CROSS=${stdenv.cc.targetPrefix}"
95 "HOST_CC=${buildStdenv.cc}/bin/cc"
96 ]
97 ++ lib.optional enableJITDebugModule "INSTALL_LJLIBD=$(INSTALL_LMOD)"
98 ++ lib.optional stdenv.hostPlatform.isStatic "BUILDMODE=static";
99 enableParallelBuilding = true;
100 env.NIX_CFLAGS_COMPILE = toString XCFLAGS;
101
102 postInstall = ''
103 mkdir -p $out/nix-support
104 cp ${
105 replaceVars ../lua-5/utils.sh {
106 luapathsearchpaths = lib.escapeShellArgs finalAttrs.LuaPathSearchPaths;
107 luacpathsearchpaths = lib.escapeShellArgs finalAttrs.LuaCPathSearchPaths;
108 }
109 } $out/nix-support/utils.sh
110 ( cd "$out/include"; ln -s luajit-*/* . )
111 ln -s "$out"/bin/luajit-* "$out"/bin/lua
112 if [[ ! -e "$out"/bin/luajit ]]; then
113 ln -s "$out"/bin/luajit* "$out"/bin/luajit
114 fi
115 '';
116
117 LuaPathSearchPaths = luaPackages.luaLib.luaPathList;
118 LuaCPathSearchPaths = luaPackages.luaLib.luaCPathList;
119
120 setupHook = builtins.toFile "lua-setup-hook" ''
121 source @out@/nix-support/utils.sh
122 addEnvHooks "$hostOffset" luaEnvHook
123 '';
124
125 # copied from python
126 passthru =
127 let
128 # When we override the interpreter we also need to override the spliced versions of the interpreter
129 inputs' = lib.filterAttrs (n: v: !lib.isDerivation v && n != "passthruFun") inputs;
130 override =
131 attr:
132 let
133 lua = attr.override (inputs' // { self = lua; });
134 in
135 lua;
136 in
137 passthruFun rec {
138 inherit self packageOverrides luaAttr;
139 inherit (finalAttrs) luaversion;
140 executable = "lua";
141 luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr};
142 luaOnBuildForHost = override pkgsBuildHost.${luaAttr};
143 luaOnBuildForTarget = override pkgsBuildTarget.${luaAttr};
144 luaOnHostForHost = override pkgsHostHost.${luaAttr};
145 luaOnTargetForTarget = lib.optionalAttrs (lib.hasAttr luaAttr pkgsTargetTarget) (
146 override pkgsTargetTarget.${luaAttr}
147 );
148 };
149
150 meta =
151 with lib;
152 {
153 description = "High-performance JIT compiler for Lua 5.1";
154 homepage = "https://luajit.org/";
155 license = licenses.mit;
156 platforms = platforms.linux ++ platforms.darwin;
157 badPlatforms = [
158 "loongarch64-linux" # See https://github.com/LuaJIT/LuaJIT/issues/1278
159 "riscv64-linux" # See https://github.com/LuaJIT/LuaJIT/issues/628
160 "powerpc64le-linux" # `#error "No support for PPC64"`
161 ];
162 mainProgram = "lua";
163 maintainers = with maintainers; [
164 thoughtpolice
165 smironov
166 vcunat
167 lblasc
168 ];
169 }
170 // extraMeta;
171})