Skip to content

Commit

Permalink
feat: implement with std lib
Browse files Browse the repository at this point in the history
Basic implementation with core features implemented, including isolation
of Nix expressions to enforce module boundaries.

One core feature still missing is private member semantics.
  • Loading branch information
nrdxp committed Aug 2, 2024
1 parent 68b63bd commit 3f699df
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
58 changes: 58 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
let
fix = import ./std/fix.nix;
filterMap = scopedImport { std = builtins; } ./std/set/filterMap.nix;
parse = scopedImport { std = builtins; } ./std/file/parse.nix;
compose = import ./.;

filterMod = builtins.filterSource (
path: type:
let
file = parse (baseNameOf path);
in
type == "regular" && file.ext or null != "nix"
);

in
dir:
let
atom = fix (
f: super: dir:
let
contents = builtins.readDir dir;
self =
let
import' = scopedImport (
{
inherit atom;
std = compose ./std // builtins;
self = self // {
outPath = filterMod dir;
};
}
// (if super != { } then { inherit super; } else { })
);
mod =
if contents ? "mod.nix" && contents."mod.nix" == "regular" then
import' "${dir + "/mod.nix"}"
else
{ };
in
filterMap (
name: type:
let
path = dir + "/${name}";
file = parse name;
in
if type == "directory" then
{ ${name} = f self path; }
else if type == "regular" && file.ext or null == "nix" && name != "mod.nix" then
{ ${file.name} = import' "${path}"; }
else
null # Ignore other file types
) contents
// mod;
in
self
) { } dir;
in
atom
1 change: 1 addition & 0 deletions std/file/mod.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
11 changes: 11 additions & 0 deletions std/file/parse.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
s:
let
r = std.match "([^.]+)\\.(.+)" s;
in
if r == null || std.length r < 2 then
null
else
{
name = std.head r;
ext = std.elemAt r 1;
}
5 changes: 5 additions & 0 deletions std/fix.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
f:
let
x = f x;
in
x
1 change: 1 addition & 0 deletions std/mod.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
8 changes: 8 additions & 0 deletions std/set/filterMap.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
f: set:
std.foldl' (
acc: key:
let
val = f key set.${key};
in
if val == null || !std.isAttrs val then acc else acc // val
) { } (std.attrNames set)
1 change: 1 addition & 0 deletions std/set/mod.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }

0 comments on commit 3f699df

Please sign in to comment.