diff --git a/overlay.nix b/overlay.nix index 1c05260..ae95f18 100644 --- a/overlay.nix +++ b/overlay.nix @@ -86,6 +86,9 @@ final: prev: { }; }; + netlink-lua = final.callPackage ./pkgs/netlink-lua {}; + waitup = final.callPackage ./pkgs/waitup {}; + # these are packages for the build system not the host/target tufted = final.callPackage ./pkgs/tufted {}; diff --git a/pkgs/netlink-lua/default.nix b/pkgs/netlink-lua/default.nix new file mode 100644 index 0000000..35bf601 --- /dev/null +++ b/pkgs/netlink-lua/default.nix @@ -0,0 +1,23 @@ +{ lua, lib, fetchpatch, fetchFromGitHub, libmnl }: +let pname = "netlink"; +in lua.pkgs.buildLuaPackage { + inherit pname; + version = "0.1.1-1"; + + buildInputs = [ libmnl ]; + + src = fetchFromGitHub { + repo = "lua-netlink"; + owner = "chris2511"; + rev = "v0.1.1"; + hash = "sha256:1833naskl4p7rz5kk0byfgngvw1mvf6cnz64sr3ny7i202wv7s52"; + }; + + buildPhase = "$CC -shared -l mnl -l lua -o netlink.so src/*.c"; + + installPhase = '' + mkdir -p "$out/lib/lua/${lua.luaversion}" + cp netlink.so "$out/lib/lua/${lua.luaversion}/" + ''; + +} diff --git a/pkgs/waitup/default.nix b/pkgs/waitup/default.nix new file mode 100644 index 0000000..0639c7c --- /dev/null +++ b/pkgs/waitup/default.nix @@ -0,0 +1,31 @@ +{ + lua5_3 +, netlink-lua +, stdenv +, makeWrapper +}: +let + lua = lua5_3; + netlink = netlink-lua.override {inherit lua;}; + fennel = lua.pkgs.fennel; +in stdenv.mkDerivation rec { + pname = "waitup"; + version = "1"; + + buildInputs = [ lua netlink-lua ]; + nativeBuildInputs = [ makeWrapper fennel ]; + + src = ./.; + + installPhase = '' + mkdir -p $out/bin $out/lib + fennel --compile ${./waitup.fnl} > $out/lib/waitup.lua + + makeWrapper ${lua}/bin/lua $out/bin/${pname} \ + --prefix LUA_CPATH ";" ${netlink}/lib/lua/${lua.luaversion}/\?.so \ + --add-flags $out/lib/waitup.lua + ''; +} + +# to use fennel.view, +# --prefix LUA_PATH ";" ${fennel}/share/lua/5.2/\?.lua \ diff --git a/pkgs/waitup/waitup.fnl b/pkgs/waitup/waitup.fnl new file mode 100644 index 0000000..e5675a8 --- /dev/null +++ b/pkgs/waitup/waitup.fnl @@ -0,0 +1,36 @@ +(local netlink (require :netlink)) +(local sock (netlink.socket)) + +(when (< (# arg) 2) + (print "usage: waitup ifname fd") + (os.exit 1)) + +(local ifname (. arg 1)) +(local fd (tonumber (. arg 2))) +(local stream (io.open (.. "/proc/self/fd/" fd) "w")) + +(fn notify-ready [] + (stream:write "\n") + (print (.. (. arg 0) ": received netlink operstate up for " ifname)) + (stream:close)) + +(fn run-events [evs] + (each [_ v (ipairs evs)] + (print :event v.event v.name) + + (match v + ;; - up: Reflects the administrative state of the interface (IFF_UP) + ;; - running: Reflects the operational state (IFF_RUNNING). + {:event "newlink" :name ifname :up :yes :running :yes} + (notify-ready) + + {:event "newlink" :name ifname :up :no} + (os.exit 0)))) + +(run-events (sock:query {:link true})) + +(print (.. (. arg 0) ": waiting for netlink NEWLINK " ifname)) + +(while (sock:poll) + (let [ev (sock:event)] + (run-events ev)))