default.nix (1592B)
1 { 2 lua, 3 lib, 4 fennel, 5 stdenv, 6 }: 7 name: 8 { 9 packages ? [ ], 10 macros ? [ ], 11 correlate ? false, 12 mainFunction ? null, 13 }: 14 source: 15 let 16 inherit (builtins) concatStringsSep replaceStrings map; 17 luapath = map ( 18 f: "${f}/share/lua/${lua.luaversion}/?.lua;" + "${f}/share/lua/${lua.luaversion}/?/init.lua;" 19 ) packages; 20 luacpath = map (f: "${f}/lib/lua/${lua.luaversion}/?.so;") packages; 21 macropath = concatStringsSep ";" (map (f: "${f}/share/lua/${lua.luaversion}/?.fnl") macros); 22 luaFlags = lib.optionalString (mainFunction != null) "-e dofile(arg[0]).${mainFunction}()"; 23 quoteString = string: "'${replaceStrings [ "'" ] [ "'\\''" ] string}'"; 24 in 25 stdenv.mkDerivation { 26 inherit name; 27 src = ./.; 28 nativeBuildInputs = [ fennel ]; 29 buildPhase = '' 30 ( 31 echo "#!${lua}/bin/lua ${luaFlags}" 32 echo "package.path = ${quoteString (concatStringsSep "" luapath)} .. package.path" 33 echo "package.cpath = ${quoteString (concatStringsSep "" luacpath)} .. package.cpath" 34 echo "local ok, stdlib = pcall(require,'posix.stdlib'); if ok then stdlib.setenv('PATH',${quoteString (lib.makeBinPath packages)} .. \":\" .. os.getenv('PATH')) end" 35 echo "local ok, ll = pcall(require,'lualinux'); if ok then ll.setenv('PATH',${quoteString (lib.makeBinPath packages)} .. \":\" .. os.getenv('PATH')) end" 36 fennel ${if macropath != "" then "--add-macro-path ${quoteString macropath}" else ""} ${ 37 if correlate then "--correlate" else "" 38 } --compile ${source} 39 ) > ${name}.lua 40 41 ''; 42 installPhase = '' 43 cp ${name}.lua $out 44 chmod +x $out 45 ''; 46 }