write-fennel quote PATH properly

escapeShellArg only quotes if the string contains special
characters, but for a Lua string we must quote unconditionally
This commit is contained in:
Daniel Barlow 2024-09-07 22:31:44 +01:00
parent 9f58e7b926
commit 69bf6cb5fb
1 changed files with 7 additions and 6 deletions

View File

@ -13,7 +13,7 @@ name:
}: }:
source: source:
let let
inherit (builtins) concatStringsSep map; inherit (builtins) concatStringsSep replaceStrings map;
luapath = map ( luapath = map (
f: "${f}/share/lua/${lua.luaversion}/?.lua;" + "${f}/share/lua/${lua.luaversion}/?/init.lua;" f: "${f}/share/lua/${lua.luaversion}/?.lua;" + "${f}/share/lua/${lua.luaversion}/?/init.lua;"
) packages; ) packages;
@ -21,6 +21,7 @@ let
macropath = concatStringsSep ";" macropath = concatStringsSep ";"
(map (f: "${f}/share/lua/${lua.luaversion}/?.fnl") macros); (map (f: "${f}/share/lua/${lua.luaversion}/?.fnl") macros);
luaFlags = lib.optionalString (mainFunction != null) "-e dofile(arg[0]).${mainFunction}()"; luaFlags = lib.optionalString (mainFunction != null) "-e dofile(arg[0]).${mainFunction}()";
quoteString = string : "'${replaceStrings ["'"] ["'\\''"] string}'";
in in
stdenv.mkDerivation { stdenv.mkDerivation {
inherit name; inherit name;
@ -29,11 +30,11 @@ stdenv.mkDerivation {
buildPhase = '' buildPhase = ''
( (
echo "#!${lua}/bin/lua ${luaFlags}" echo "#!${lua}/bin/lua ${luaFlags}"
echo "package.path = ${lib.strings.escapeShellArg (concatStringsSep "" luapath)} .. package.path" echo "package.path = ${quoteString (concatStringsSep "" luapath)} .. package.path"
echo "package.cpath = ${lib.strings.escapeShellArg (concatStringsSep "" luacpath)} .. package.cpath" echo "package.cpath = ${quoteString (concatStringsSep "" luacpath)} .. package.cpath"
echo "local ok, stdlib = pcall(require,'posix.stdlib'); if ok then stdlib.setenv('PATH',${lib.escapeShellArg (lib.makeBinPath packages)} .. \":\" .. os.getenv('PATH')) end" echo "local ok, stdlib = pcall(require,'posix.stdlib'); if ok then stdlib.setenv('PATH',${quoteString (lib.makeBinPath packages)} .. \":\" .. os.getenv('PATH')) end"
echo "local ok, ll = pcall(require,'lualinux'); if ok then ll.setenv('PATH',${lib.escapeShellArg (lib.makeBinPath packages)} .. \":\" .. os.getenv('PATH')) end" echo "local ok, ll = pcall(require,'lualinux'); if ok then ll.setenv('PATH',${quoteString (lib.makeBinPath packages)} .. \":\" .. os.getenv('PATH')) end"
fennel ${if macropath != "" then "--add-macro-path ${lib.strings.escapeShellArg macropath}" else ""} ${if correlate then "--correlate" else ""} --compile ${source} fennel ${if macropath != "" then "--add-macro-path ${quoteString macropath}" else ""} ${if correlate then "--correlate" else ""} --compile ${source}
) > ${name}.lua ) > ${name}.lua
''; '';