From 343d3b6508f73440ae205329a2e069f7a465d4fd Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Tue, 12 Sep 2023 17:45:18 +0100 Subject: [PATCH] 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. --- pkgs/default.nix | 1 + pkgs/write-fennel-script/default.nix | 26 ++----------------- pkgs/write-fennel/default.nix | 37 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 pkgs/write-fennel/default.nix diff --git a/pkgs/default.nix b/pkgs/default.nix index 1c52a63..e1fa46c 100644 --- 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 index dfe36f4..09e46c4 100644 --- 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 new file mode 100644 index 0000000..c9be23e --- /dev/null +++ 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 + ''; + }