liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 343d3b6508f73440ae205329a2e069f7a465d4fd
parent 96e19767e935decaef7ea9108bc82c6e0c34b407
Author: Daniel Barlow <dan@telent.net>
Date:   Tue, 12 Sep 2023 17:45:18 +0100

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.

Diffstat:
Mpkgs/default.nix | 1+
Mpkgs/write-fennel-script/default.nix | 26++------------------------
Apkgs/write-fennel/default.nix | 37+++++++++++++++++++++++++++++++++++++
3 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/pkgs/default.nix b/pkgs/default.nix @@ -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 {}; diff --git a/pkgs/write-fennel-script/default.nix b/pkgs/write-fennel-script/default.nix @@ -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 diff --git a/pkgs/write-fennel/default.nix b/pkgs/write-fennel/default.nix @@ -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 + ''; + }