liminix/pkgs/write-fennel/default.nix
Raito Bezarius c39bfc2bb5 fix(write-fennel): escape binary path for PATH
otherwise, escapeShellArg might just do nothing!

Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
2024-09-07 21:03:57 +02:00

38 lines
1.2 KiB
Nix

{
lua
, lib
, fennel
, stdenv
}:
name :
{
packages ? [],
correlate ? false,
mainFunction ? null
} @ options : source :
let
luapath = builtins.map
(f:
"${f}/share/lua/${lua.luaversion}/?.lua;" +
"${f}/share/lua/${lua.luaversion}/?/init.lua;")
packages;
luacpath = builtins.map (f: "${f}/lib/lua/${lua.luaversion}/?.so;") packages;
luaFlags = lib.optionalString (mainFunction !=null) "-e dofile(arg[0]).${mainFunction}()";
in stdenv.mkDerivation {
inherit name;
src = ./.;
nativeBuildInputs = [ fennel ];
buildPhase = ''
(
echo "#!${lua}/bin/lua ${luaFlags}"
echo "package.path = ${lib.strings.escapeShellArg (builtins.concatStringsSep "" luapath)} .. package.path"
echo "package.cpath = ${lib.strings.escapeShellArg (builtins.concatStringsSep "" luacpath)} .. package.cpath"
echo "local ok, stdlib = pcall(require,'posix.stdlib'); if ok then stdlib.setenv('PATH', \"${lib.makeBinPath packages}\" .. \":\" .. os.getenv('PATH')) end"
fennel ${if correlate then "--correlate" else ""} --compile ${source}
) > ${name}.lua
'';
installPhase = ''
cp ${name}.lua $out
chmod +x $out
'';
}