From 933ee30441209e77c307cd601075101449545097 Mon Sep 17 00:00:00 2001 From: Timothy DeHerrera Date: Mon, 5 Aug 2024 14:55:43 -0600 Subject: [PATCH] fix: distinguish std & composer features --- compose.toml | 1 - default.nix | 74 ++++++++++++++++++++++------------ src/default.nix | 25 +++++++++--- std.toml | 8 ++++ std/mod.nix | 1 + test/std-import/import.nix | 6 +-- test/std-import/import.sh | 18 ++++----- test/std-import/import/mod.nix | 5 ++- 8 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 std.toml diff --git a/compose.toml b/compose.toml index e2f6a2a..34e5728 100644 --- a/compose.toml +++ b/compose.toml @@ -5,5 +5,4 @@ description = "A purpose built, unopinionated, and performant module system for [features] std = [] -pkg_lib = ["std"] default = ["std"] diff --git a/default.nix b/default.nix index f4880a7..43191f8 100644 --- a/default.nix +++ b/default.nix @@ -1,23 +1,35 @@ let l = builtins; src = import ./src; - pins = import ./npins; - toml = l.fromTOML (l.readFile ./compose.toml); in { extern ? { }, + features ? [ ], # internal features of the composer function - __features ? toml.features.default or [ ], + stdFeatures ? src.stdToml.features.default or [ ], + composeFeatures ? src.composeToml.features.default, # enable testing code paths __internal__test ? false, + __isStd__ ? false, }: dir': let dir = src.prepDir dir'; - std = src.composeStd ./std; + std = src.composeStd { + features = stdFeatures; + inherit __internal__test; + } ./std; - __features' = src.features.parse toml.features __features; + composeFeatures' = src.features.parse src.composeToml.features composeFeatures; + + meta = { + features = { + mod = features; + compose = composeFeatures'; + std = stdFeatures; + }; + }; f = f: pre: dir: @@ -32,7 +44,6 @@ let scope = let scope' = with src; { - atom = atom'; mod = self'; builtins = errors.builtins; import = errors.import; @@ -46,30 +57,37 @@ let __getFlake = errors.import; }; - scope'' = src.injectOptionals scope' [ + scope'' = src.set.inject scope' [ preOpt { - _if = l.elem "std" __features'; - std = - std - // src.set.cond { - _if = l.elem "pkg_lib" __features'; - lib = import "${pins."nixpkgs.lib"}/lib"; - }; + _if = !__isStd__ && l.elem "std" composeFeatures'; + inherit std; + } + { + _if = !__isStd__; + atom = atom' // { + inherit meta; + }; + } + { + _if = __isStd__; + std = atom // { + inherit meta; + }; + } + { + _if = __internal__test; + # information about the internal module system itself + # available to tests + __internal = { + # a copy of the global scope, for testing if values exist + # mostly for our internal testing functions + scope = scope''; + }; } ]; in - scope'' - // src.set.cond { - _if = __internal__test; - # information about the internal module system itself - __internal = { - features = __features'; - # a copy of the global scope, for testing if values exist - # mostly for our internal testing functions - scope = scope''; - }; - }; + scope''; Import = scopedImport scope; @@ -108,7 +126,11 @@ let (baseNameOf dir) ]; - atom = src.fix f null dir; + atom = + let + fixed = src.fix f null dir; + in + src.set.inject fixed [ ({ _if = __isStd__; } // src.pureBuiltins) ]; in assert !__internal__test diff --git a/src/default.nix b/src/default.nix index 825f61c..b4c9aef 100644 --- a/src/default.nix +++ b/src/default.nix @@ -15,9 +15,18 @@ let std = builtins; mod = scopedImport { inherit std mod; } ../std/string/mod.nix; } ../std/string/toLowerCase.nix; + stdToml = l.fromTOML (l.readFile ../std.toml); + composeToml = l.fromTOML (l.readFile ../compose.toml); in -{ - inherit fix filterMap strToPath; +rec { + inherit + fix + filterMap + strToPath + stdFilter + stdToml + composeToml + ; file = { inherit parse; @@ -48,8 +57,12 @@ in ); composeStd = - path: - compose { } path // filterMap (k: v: if stdFilter k != null then null else { ${k} = v; }) builtins; + opts: path: + compose { + inherit (opts) __internal__test; + features = features.parse stdToml.features opts.features; + __isStd__ = true; + } path; modIsValid = mod: dir: @@ -59,7 +72,9 @@ in ${toString dir}/mod.nix ''; - injectOptionals = l.foldl' (acc: x: acc // cond x); + set.inject = l.foldl' (acc: x: acc // cond x); + + pureBuiltins = filterMap (k: v: if stdFilter k != null then null else { ${k} = v; }) builtins; hasMod = contents: contents."mod.nix" or null == "regular"; diff --git a/std.toml b/std.toml new file mode 100644 index 0000000..16bb8ee --- /dev/null +++ b/std.toml @@ -0,0 +1,8 @@ +[project] +name = "std" +version = "0.2.0" +description = "Nix Standard Library" + +[features] +pkg_lib = [] +default = [] diff --git a/std/mod.nix b/std/mod.nix index 23c06c1..061e1ac 100644 --- a/std/mod.nix +++ b/std/mod.nix @@ -4,4 +4,5 @@ Set = mod.set; Path = mod.path; String = mod.string; + Std = std; } diff --git a/test/std-import/import.nix b/test/std-import/import.nix index 10afbd6..88679a1 100644 --- a/test/std-import/import.nix +++ b/test/std-import/import.nix @@ -3,7 +3,7 @@ let in { default = compose { }; - noStd = compose { __features = [ ]; }; - explicit = compose { __features = [ "std" ]; }; - withNixpkgsLib = compose { __features = [ "pkg_lib" ]; }; + noStd = compose { composeFeatures = [ ]; }; + explicit = compose { stdFeatures = [ ]; }; + # withNixpkgsLib = compose { stdFeatures = [ "pkg_lib" ]; }; } diff --git a/test/std-import/import.sh b/test/std-import/import.sh index dc0805b..59e798e 100755 --- a/test/std-import/import.sh +++ b/test/std-import/import.sh @@ -7,7 +7,7 @@ f="$(nix eval -f import.nix default.std)" [[ "$f" == true ]] f="$(nix eval -f import.nix default.lib)" [[ "$f" == false ]] -f="$(nix eval -f import.nix default.core)" +f="$(nix eval -f import.nix default.compose)" [[ "$f" == '[ "std" ]' ]] # explicit @@ -15,7 +15,7 @@ f="$(nix eval -f import.nix explicit.std)" [[ "$f" == true ]] f="$(nix eval -f import.nix explicit.lib)" [[ "$f" == false ]] -f="$(nix eval -f import.nix explicit.core)" +f="$(nix eval -f import.nix explicit.compose)" [[ "$f" == '[ "std" ]' ]] # no std set @@ -23,13 +23,13 @@ f="$(nix eval -f import.nix noStd.std)" [[ "$f" == false ]] f="$(nix eval -f import.nix noStd.lib)" [[ "$f" == false ]] -f="$(nix eval -f import.nix noStd.core)" +f="$(nix eval -f import.nix noStd.compose)" [[ "$f" == '[ ]' ]] # no std set -f="$(nix eval -f import.nix withNixpkgsLib.std)" -[[ "$f" == true ]] -f="$(nix eval -f import.nix withNixpkgsLib.lib)" -[[ "$f" == true ]] -f="$(nix eval -f import.nix withNixpkgsLib.core)" -[[ "$f" == '[ "pkg_lib" "std" ]' ]] +# f="$(nix eval -f import.nix withNixpkgsLib.std)" +# [[ "$f" == true ]] +# f="$(nix eval -f import.nix withNixpkgsLib.lib)" +# [[ "$f" == true ]] +# f="$(nix eval -f import.nix withNixpkgsLib.core)" +# [[ "$f" == '[ "pkg_lib" "std" ]' ]] diff --git a/test/std-import/import/mod.nix b/test/std-import/import/mod.nix index 1168d67..0ac922a 100644 --- a/test/std-import/import/mod.nix +++ b/test/std-import/import/mod.nix @@ -1,8 +1,9 @@ let - inherit (__internal) scope features; + inherit (__internal) scope; in { Std = scope ? std; Lib = scope ? std && scope.std ? lib; - Core = features; + Compose = atom.meta.features.compose; + StdF = atom.meta.features.std; }