commit 6f559e370079e3d786ae1c2d6e01713c43a9536d
parent 6478c56ed1db0321cb9bc89872b24d975296c640
Author: Daniel Barlow <dan@telent.net>
Date: Sun, 26 Oct 2025 20:32:52 +0000
add fennelc, a script to do what writeFennel does
it's a bit simpler because no need to do quite as much string surgery
to compute package.{c,}path
also it can be used in Makefiles
Diffstat:
3 files changed, 69 insertions(+), 0 deletions(-)
diff --git a/pkgs/default.nix b/pkgs/default.nix
@@ -85,6 +85,7 @@ in
devout = callPackage ./devout { };
fetch-freebsd = callPackage ./fetch-freebsd { };
fennel = callPackage ./fennel { };
+ fennelc = callPackage ./fennelc { };
fennelrepl = callPackage ./fennelrepl { };
firewallgen = callPackage ./firewallgen { };
firmware-utils = callPackage ./firmware-utils { };
diff --git a/pkgs/fennelc/default.nix b/pkgs/fennelc/default.nix
@@ -0,0 +1,19 @@
+{
+ lua,
+ runCommand
+}:
+let fennel = lua.pkgs.fennel; in
+runCommand "build-fennelc"
+{
+ nativeBuildInputs = [ fennel ] ;
+} ''
+LUAPATH=$(ls -d ${fennel}/share/lua/*)
+mkdir -p $out/bin
+(
+exec > $out/bin/fennelc
+echo '#! ${lua}/bin/lua'
+echo "package.path = \"''${LUAPATH}/?.lua;''${LLPATH}/?.lua;\" .. package.path"
+fennel --compile ${./fennelc.fnl}
+)
+chmod +x $out/bin/fennelc
+''
diff --git a/pkgs/fennelc/fennelc.fnl b/pkgs/fennelc/fennelc.fnl
@@ -0,0 +1,49 @@
+(local { : view &as fennel } ( require :fennel))
+
+(fn assoc [tbl k v & more]
+ (tset tbl k v)
+ (case more
+ [k v] (assoc tbl k v)
+ _ tbl))
+
+(fn parse-args [args]
+ (case args
+ ["--correlate" & rest]
+ (assoc (parse-args rest)
+ :correlate true)
+
+ ["--main" main-fn & rest]
+ (assoc (parse-args rest)
+ :main main-fn)
+
+ ["-o" output-file & rest]
+ (assoc (parse-args rest)
+ :output-file output-file)
+
+ [source] { :source-file source }))
+
+(fn output-name [opts]
+ (or opts.output-file
+ (string.gsub opts.source-file "%.fnl$" "")))
+
+(let [luapath (os.getenv "LUA_PATH")
+ luacpath (os.getenv "LUA_CPATH")
+ path (os.getenv "PATH")
+ opts (parse-args arg)
+ output-file (output-name opts)]
+ (with-open [o (io.open output-file :w)]
+ (o:write "#!/usr/bin/env lua\n")
+ (and luapath
+ (o:write (string.format "package.path = %q .. \";\" .. package.path\n" luapath)))
+ (and luacpath
+ (o:write (string.format "package.cpath = %q .. \";\" .. package.cpath\n" luacpath)))
+ (let [(ok? msg)
+ (pcall
+ fennel.compile-string
+ (: (io.open opts.source-file :r) :read "*a")
+ {:filename opts.source-file
+ :correlate opts.correlate})]
+ (when (not ok?)
+ (error (.. "error: " msg)))
+ (o:write msg)
+ (os.execute (string.format "chmod +x %q" output-file)))))