From cdb23b147c7a1c0996125a38b9e468ce1a9df2ce Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 25 Apr 2024 21:14:37 +0100 Subject: [PATCH] convert anoia.fs to use lualinux --- modules/dhcp6c/acquire-delegated-prefix.nix | 3 +- modules/dhcp6c/acquire-wan-address.nix | 3 +- pkgs/anoia/Makefile | 2 ++ pkgs/anoia/default.nix | 3 +- pkgs/anoia/fs.fnl | 36 ++++++++++++++++----- pkgs/anoia/svc.fnl | 13 ++++---- pkgs/anoia/test-svc.fnl | 2 +- pkgs/devout/default.nix | 2 +- pkgs/fennelrepl/default.nix | 1 - 9 files changed, 44 insertions(+), 21 deletions(-) diff --git a/modules/dhcp6c/acquire-delegated-prefix.nix b/modules/dhcp6c/acquire-delegated-prefix.nix index 1b9f4bd..f48db31 100644 --- a/modules/dhcp6c/acquire-delegated-prefix.nix +++ b/modules/dhcp6c/acquire-delegated-prefix.nix @@ -3,8 +3,9 @@ , linotify , anoia , lua +, lualinux }: writeFennel "acquire-delegated-prefix" { - packages = [ linotify anoia lua.pkgs.luafilesystem ]; + packages = [ linotify anoia lualinux ]; mainFunction = "run"; } ./acquire-delegated-prefix.fnl diff --git a/modules/dhcp6c/acquire-wan-address.nix b/modules/dhcp6c/acquire-wan-address.nix index 3773d35..5d6b6c8 100644 --- a/modules/dhcp6c/acquire-wan-address.nix +++ b/modules/dhcp6c/acquire-wan-address.nix @@ -2,9 +2,10 @@ writeFennel , linotify , anoia +, lualinux , lua }: writeFennel "acquire-wan-address" { - packages = [ linotify anoia lua.pkgs.luafilesystem ]; + packages = [ linotify anoia lualinux ]; mainFunction = "run"; } ./acquire-wan-address.fnl diff --git a/pkgs/anoia/Makefile b/pkgs/anoia/Makefile index 2cf691a..9fdf7f8 100644 --- a/pkgs/anoia/Makefile +++ b/pkgs/anoia/Makefile @@ -2,7 +2,9 @@ default: fs.lua init.lua nl.lua svc.lua test: + ln -s . anoia fennel test.fnl + fennel test-svc.fnl %.lua: %.fnl fennel --compile $< > $@ diff --git a/pkgs/anoia/default.nix b/pkgs/anoia/default.nix index 9882c57..7876303 100644 --- a/pkgs/anoia/default.nix +++ b/pkgs/anoia/default.nix @@ -1,6 +1,7 @@ { fennel , stdenv +, linotify , lua , lualinux }: @@ -10,7 +11,7 @@ in stdenv.mkDerivation { version = "0.1"; src = ./.; nativeBuildInputs = [ fennel ]; - buildInputs = with lua.pkgs; [ luafilesystem lualinux ]; + buildInputs = with lua.pkgs; [ linotify lualinux ]; outputs = [ "out" "dev" ]; doCheck = true; diff --git a/pkgs/anoia/fs.fnl b/pkgs/anoia/fs.fnl index e3645cc..71693e8 100644 --- a/pkgs/anoia/fs.fnl +++ b/pkgs/anoia/fs.fnl @@ -11,9 +11,20 @@ (fn ifmt-bits [mode] (and mode (band mode 0xf000))) +(fn file-type [pathname] + (. { + S_IFDIR :directory + S_IFSOCK :socket + S_IFLNK :link + S_IFREG :file + S_IFBLK :block-device + S_IFCHR :character-device + S_IFIFO :fifo + } + (ifmt-bits (ll.lstat3 pathname)))) + (fn directory? [pathname] - (let [(mode size mtime) (ll.lstat3 pathname)] - (= (ifmt-bits mode) S_IFDIR))) + (= (file-type pathname) :directory)) (fn mktree [pathname] (if (or (= pathname "") (= pathname "/")) @@ -24,18 +35,25 @@ (or (directory? parent) (mktree parent)) (assert (ll.mkdir pathname))))) +(fn dir [name] + (let [dp (assert (ll.opendir name) name)] + (fn [] + (match (ll.readdir dp) + (name type) (values name type) + (nil err) (do (if err (print err)) (ll.closedir dp) nil))))) + (fn rmtree [pathname] - (case (ifmt-bits (ll.lstat3 pathname)) + (case (file-type pathname) nil true - S_IFDIR + :directory (do - (each [f (lfs.dir pathname)] + (each [f (dir pathname)] (when (not (or (= f ".") (= f ".."))) (rmtree ( .. pathname "/" f))) - (lfs.rmdir pathname))) - S_IFREG + (ll.rmdir pathname))) + :file (os.remove pathname) - S_IFLNK + :link (os.remove pathname) unknown (error (.. "can't remove " pathname " of mode \"" unknown "\"")))) @@ -45,5 +63,7 @@ : mktree : rmtree : directory? + : dir + : file-type :symlink (fn [from to] (ll.symlink from to)) } diff --git a/pkgs/anoia/svc.fnl b/pkgs/anoia/svc.fnl index d980e7f..c473dc7 100644 --- a/pkgs/anoia/svc.fnl +++ b/pkgs/anoia/svc.fnl @@ -1,7 +1,6 @@ (local inotify (require :inotify)) (local { : file-exists? } (require :anoia)) -(local { : directory? } (require :anoia.fs)) -(local lfs (require :lfs)) +(local { : file-type : dir &as fs } (require :anoia.fs)) (fn read-line [name] (with-open [f (assert (io.open name :r) (.. "can't open file " name))] @@ -20,15 +19,15 @@ handle)) (fn read-value [pathname] - (case (lfs.symlinkattributes pathname) + (case (file-type pathname) nil nil - {:mode "directory"} - (collect [f (lfs.dir pathname)] + :directory + (collect [f (fs.dir pathname)] (when (not (or (= f ".") (= f ".."))) (values f (read-value ( .. pathname "/" f))))) - {:mode "file"} + :file (read-line pathname) - {:mode "link"} + :link (read-line pathname) unknown (error (.. "can't read " pathname " of kind \"" unknown.mode "\"")))) diff --git a/pkgs/anoia/test-svc.fnl b/pkgs/anoia/test-svc.fnl index 89830ee..61729d4 100644 --- a/pkgs/anoia/test-svc.fnl +++ b/pkgs/anoia/test-svc.fnl @@ -1,4 +1,4 @@ -(local svc (require :anoia.svc)) +(local svc (require :svc)) (local { : view } (require :fennel)) (local ex (svc.open "./example-output")) diff --git a/pkgs/devout/default.nix b/pkgs/devout/default.nix index 522a1ca..e675ed5 100644 --- a/pkgs/devout/default.nix +++ b/pkgs/devout/default.nix @@ -15,7 +15,7 @@ stdenv.mkDerivation { installPhase = '' mkdir -p $out/bin cp -p ${writeFennel "devout" { - packages = [fennel anoia nellie lua.pkgs.luafilesystem lualinux]; + packages = [fennel anoia nellie lualinux]; mainFunction = "run"; } ./devout.fnl} $out/bin/devout ''; diff --git a/pkgs/fennelrepl/default.nix b/pkgs/fennelrepl/default.nix index e1c4604..28f47ca 100644 --- a/pkgs/fennelrepl/default.nix +++ b/pkgs/fennelrepl/default.nix @@ -19,7 +19,6 @@ let packages = [ lualinux netlink-lua lua.pkgs.readline - lua.pkgs.luafilesystem ]; join = ps: builtins.concatStringsSep ";" ps; luapath = join (builtins.map (f: