From fdffdbb22a2fe19bfc6bd4b4b1bf5cb25a393ee1 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Mon, 29 May 2023 20:20:12 +0100 Subject: [PATCH] add writeFennelScript function, make ifwait use it --- pkgs/default.nix | 1 + pkgs/ifwait/default.nix | 33 +++++----------------------- pkgs/write-fennel-script/default.nix | 21 ++++++++++++++++++ tests/ci.nix | 1 + tests/fennel/hello.fnl | 1 + tests/fennel/test.nix | 21 ++++++++++++++++++ 6 files changed, 51 insertions(+), 27 deletions(-) create mode 100644 pkgs/write-fennel-script/default.nix create mode 100644 tests/fennel/hello.fnl create mode 100644 tests/fennel/test.nix diff --git a/pkgs/default.nix b/pkgs/default.nix index df3985d..fd9753e 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -11,6 +11,7 @@ kernel = callPackage ./kernel {}; }; }; + writeFennelScript = callPackage ./write-fennel-script {}; writeAshScript = callPackage ./write-ash-script {}; systemconfig = callPackage ./systemconfig {}; s6-init-bin = callPackage ./s6-init-bin {}; diff --git a/pkgs/ifwait/default.nix b/pkgs/ifwait/default.nix index 0f1b2db..27691b4 100644 --- a/pkgs/ifwait/default.nix +++ b/pkgs/ifwait/default.nix @@ -1,34 +1,13 @@ { luaSmall , netlink-lua -, stdenv -, makeWrapper +, writeFennelScript +, runCommand }: let lua = luaSmall; netlink = netlink-lua.override {inherit lua;}; - fennel = lua.pkgs.fennel; -in stdenv.mkDerivation rec { - pname = "ifwait"; - version = "1"; - phases = [ "installPhase" ]; - - buildInputs = [ lua netlink ]; - nativeBuildInputs = [ makeWrapper fennel ]; - - LUA_CPATH = "${netlink}/lib/lua/${lua.luaversion}/\?.so"; # for nix-shell - - installPhase = '' - mkdir -p $out/bin $out/lib - fennel --compile ${./ifwait.fnl} > $out/lib/${pname}.lua - - makeWrapper ${lua}/bin/lua $out/bin/${pname} \ - --prefix LUA_CPATH ";" ${netlink}/lib/lua/${lua.luaversion}/\?.so \ - --add-flags $out/lib/${pname}.lua - # makeWrapper adds a shebang for bash - sed -i -e '1c#!/bin/sh' $out/bin/${pname} - ''; -} - -# to use fennel.view, -# --prefix LUA_PATH ";" ${fennel}/share/lua/5.2/\?.lua \ +in runCommand "ifwait" {} '' + mkdir -p $out/bin + cp -p ${writeFennelScript "ifwait" [netlink] ./ifwait.fnl} $out/bin/ifwait +'' diff --git a/pkgs/write-fennel-script/default.nix b/pkgs/write-fennel-script/default.nix new file mode 100644 index 0000000..bdff627 --- /dev/null +++ b/pkgs/write-fennel-script/default.nix @@ -0,0 +1,21 @@ +{ + runCommand +, luaSmall +, runtimeShell +, lib +}: +let lua = luaSmall; +in name : packages : source : + let + luapath = builtins.map (f: "${f}/share/lua/${lua.luaversion}/?.lua;") packages; + luacpath = builtins.map (f: "${f}/lib/lua/${lua.luaversion}/?.so;") packages; + in runCommand name {} '' + #!${runtimeShell} + ( + echo "#!${lua}/bin/lua" + echo "package.path = ${lib.strings.escapeShellArg luapath} .. package.path" + echo "package.cpath = ${lib.strings.escapeShellArg luacpath} .. package.cpath" + ${lua.pkgs.fennel}/bin/fennel --compile ${source} + ) > $out + chmod a+x $out + '' diff --git a/tests/ci.nix b/tests/ci.nix index 2f785d2..315112c 100644 --- a/tests/ci.nix +++ b/tests/ci.nix @@ -5,4 +5,5 @@ pppoe = import ./pppoe/test.nix; jffs2 = import ./jffs2/test.nix; min-copy-closure = import ./min-copy-closure/test.nix; + fennel = import ./fennel/test.nix; } diff --git a/tests/fennel/hello.fnl b/tests/fennel/hello.fnl new file mode 100644 index 0000000..2ed187c --- /dev/null +++ b/tests/fennel/hello.fnl @@ -0,0 +1 @@ +(print "hello") diff --git a/tests/fennel/test.nix b/tests/fennel/test.nix new file mode 100644 index 0000000..5f874c3 --- /dev/null +++ b/tests/fennel/test.nix @@ -0,0 +1,21 @@ +{ + liminix +, nixpkgs +}: +let + overlay = import "${liminix}/overlay.nix"; + pkgs = import { overlays = [overlay]; }; + script = pkgs.writeFennelScript "foo" [] ./hello.fnl; + inherit (pkgs.luaSmall.pkgs) fifo; + netlink = pkgs.netlink-lua.override { lua = pkgs.luaSmall; }; + script2 = pkgs.writeFennelScript "foo2" [fifo netlink] ./hello.fnl; +in pkgs.runCommand "check" { + } '' +set -e +# test that it works +test $(${script}) = "hello" +# test that lua path, cpath are set +grep -q ${fifo}/share/lua/5.3 ${script2} +grep -q ${netlink}/lib/lua/5.3 ${script2} +date > $out +''