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