writeFennel is writeFennelScript with knobs on

The second parameter is now an options attrset, wherein we will pile
all kinds of cool stuff.

Right now the only cool bit is `mainFunction`, which allows you to
compile a fennel module into a lua script and name the function that
should be executed when the script runs. This makes it easier to
write testable Fennel code, because the test script can require the
module and call stuff in it.
This commit is contained in:
Daniel Barlow 2023-09-12 17:45:18 +01:00
parent 96e19767e9
commit 343d3b6508
3 changed files with 40 additions and 24 deletions

View file

@ -52,6 +52,7 @@ in {
};
};
writeFennelScript = callPackage ./write-fennel-script {};
writeFennel = callPackage ./write-fennel {};
writeAshScript = callPackage ./write-ash-script {};
systemconfig = callPackage ./systemconfig {};
s6-init-bin = callPackage ./s6-init-bin {};

View file

@ -2,30 +2,8 @@
lua
, lib
, fennel
, writeFennel
, stdenv
}:
name : packages : 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;
in stdenv.mkDerivation {
inherit name;
src = ./.;
nativeBuildInputs = [ fennel ];
buildPhase = ''
(
echo "#!${lua}/bin/lua"
echo "package.path = ${lib.strings.escapeShellArg (builtins.concatStringsSep "" luapath)} .. package.path"
echo "package.cpath = ${lib.strings.escapeShellArg (builtins.concatStringsSep "" luacpath)} .. package.cpath"
fennel --correlate --compile ${source}
) > ${name}.lua
'';
installPhase = ''
cp ${name}.lua $out
chmod +x $out
'';
}
writeFennel name { inherit packages; } source

View file

@ -0,0 +1,37 @@
{
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"
fennel ${if correlate then "--correlate" else ""} --compile ${source}
) > ${name}.lua
'';
installPhase = ''
cp ${name}.lua $out
chmod +x $out
'';
}