From 2de4d7a8f9cccb43875d8b378db2dbd9f70bbdb0 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Wed, 5 Jul 2023 20:23:27 +0100 Subject: [PATCH] fennel: extract some common functions into a shareable module --- examples/acquire-delegated-prefix.fnl | 22 +++------------------- examples/acquire-delegated-prefix.nix | 3 ++- pkgs/anoia/README | 14 ++++++++++++++ pkgs/anoia/default.nix | 19 +++++++++++++++++++ pkgs/anoia/init.fnl | 16 ++++++++++++++++ pkgs/default.nix | 1 + pkgs/write-fennel-script/default.nix | 6 +++++- 7 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 pkgs/anoia/README create mode 100644 pkgs/anoia/default.nix create mode 100644 pkgs/anoia/init.fnl diff --git a/examples/acquire-delegated-prefix.fnl b/examples/acquire-delegated-prefix.fnl index a4e4562c..b66e0ef7 100644 --- a/examples/acquire-delegated-prefix.fnl +++ b/examples/acquire-delegated-prefix.fnl @@ -1,12 +1,5 @@ (local inotify (require :inotify)) - -(fn merge [table1 table2] - (collect [k v (pairs table2) &into table1] - k v)) - -(fn split [sep string] - (icollect [v (string.gmatch string (.. "([^" sep "]+)"))] - v)) +(local { : merge : split : file-exists? : system } (require :anoia)) (fn parse-prefix [str] (fn parse-extra [s] @@ -24,12 +17,6 @@ ;;(parse-prefix "2001:8b0:de3a:40dc::/64,7198,7198") ;;(parse-prefix "2001:8b0:de3a:1001::/64,7198,7188,excluded=1/2,thi=10") - -(fn file-exists? [name] - (match (io.open name :r) - f (do (f:close) true) - _ false)) - (fn read-line [name] (with-open [f (assert (io.open name :r) (.. "can't open file " name))] (f:read "*l"))) @@ -80,9 +67,6 @@ (table.insert deleted (parse-prefix prefix)))) (values added deleted))) -;;(fn execute [s] (do (print s) true)) -(fn execute [s] (assert (os.execute s))) - (let [[state-directory lan-device] arg dir (watch-directory state-directory)] (var prefixes []) @@ -92,10 +76,10 @@ (let [new-prefixes (split " " (dir:read-line "/prefixes")) (added deleted) (changes prefixes new-prefixes)] (each [_ p (ipairs added)] - (execute + (system (.. "ip address add " p.prefix "::1/" p.len " dev " lan-device))) (each [_ p (ipairs deleted)] - (execute + (system (.. "ip address del " p.prefix "::1/" p.len " dev " lan-device))) (set prefixes new-prefixes))) (dir:wait-events))) diff --git a/examples/acquire-delegated-prefix.nix b/examples/acquire-delegated-prefix.nix index 90d7d601..a6ea2216 100644 --- a/examples/acquire-delegated-prefix.nix +++ b/examples/acquire-delegated-prefix.nix @@ -1,7 +1,8 @@ { writeFennelScript , linotify +, anoia }: writeFennelScript "acquire-delegated-prefix" - [ linotify ] + [ linotify anoia ] ./acquire-delegated-prefix.fnl diff --git a/pkgs/anoia/README b/pkgs/anoia/README new file mode 100644 index 00000000..bf21df18 --- /dev/null +++ b/pkgs/anoia/README @@ -0,0 +1,14 @@ + +In Terry Pratchett's Discworld novels, Anoi is a minor goddess of Things That Stick In Drawers + +> Often, but not uniquely, a ladle, but sometimes a metal spatula or, +> rarely, a mechanical egg-whisk that nobody in the house admits to +> ever buying. The desperate mad rattling and cries of ‘How can it +> close on the damn thing but not open with it? Who bought this? Do we +> ever use it?’ is as praise unto Anoia. She also eats corkscrews. + +This is a library of miscellaneous Fennel code used in Liminix that is +shared between various scripts but doesn't really fit together. It is +not a public stable interface - while any Liminix code is welcome to +use it, it's suject to reshuffle, rearrangement, refactor or rejection +without notice. diff --git a/pkgs/anoia/default.nix b/pkgs/anoia/default.nix new file mode 100644 index 00000000..1f527320 --- /dev/null +++ b/pkgs/anoia/default.nix @@ -0,0 +1,19 @@ +{ + fennel +, stdenv +, lua +}: +let pname = "anoia"; +in stdenv.mkDerivation { + inherit pname; + version = "0.1"; + src = ./.; + nativeBuildInputs = [ fennel ]; + buildPhase = '' + fennel --compile init.fnl > init.lua + ''; + installPhase = '' + mkdir -p "$out/share/lua/${lua.luaversion}/${pname}" + cp *.lua "$out/share/lua/${lua.luaversion}/${pname}" + ''; +} diff --git a/pkgs/anoia/init.fnl b/pkgs/anoia/init.fnl new file mode 100644 index 00000000..06695ba7 --- /dev/null +++ b/pkgs/anoia/init.fnl @@ -0,0 +1,16 @@ +(fn merge [table1 table2] + (collect [k v (pairs table2) &into table1] + k v)) + +(fn split [sep string] + (icollect [v (string.gmatch string (.. "([^" sep "]+)"))] + v)) + +(fn file-exists? [name] + (match (io.open name :r) + f (do (f:close) true) + _ false)) + +(fn system [s] (assert (os.execute s))) + +{ : merge : split : file-exists? : system } diff --git a/pkgs/default.nix b/pkgs/default.nix index e04ae515..c9565afb 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -58,4 +58,5 @@ odhcp-script = callPackage ./odhcp-script {}; fennel = callPackage ./fennel {}; fennelrepl = callPackage ./fennelrepl {}; + anoia = callPackage ./anoia {}; } diff --git a/pkgs/write-fennel-script/default.nix b/pkgs/write-fennel-script/default.nix index bac4874d..e4cf6b1b 100644 --- a/pkgs/write-fennel-script/default.nix +++ b/pkgs/write-fennel-script/default.nix @@ -6,7 +6,11 @@ }: name : packages : source : let - luapath = builtins.map (f: "${f}/share/lua/${lua.luaversion}/?.lua;") packages; + 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;