Skip to content

Commit

Permalink
fix: distinguish std & composer features
Browse files Browse the repository at this point in the history
  • Loading branch information
nrdxp committed Aug 5, 2024
1 parent 9490965 commit 933ee30
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 46 deletions.
1 change: 0 additions & 1 deletion compose.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ description = "A purpose built, unopinionated, and performant module system for

[features]
std = []
pkg_lib = ["std"]
default = ["std"]
74 changes: 48 additions & 26 deletions default.nix
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -32,7 +44,6 @@ let
scope =
let
scope' = with src; {
atom = atom';
mod = self';
builtins = errors.builtins;
import = errors.import;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions src/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand All @@ -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";

Expand Down
8 changes: 8 additions & 0 deletions std.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = "std"
version = "0.2.0"
description = "Nix Standard Library"

[features]
pkg_lib = []
default = []
1 change: 1 addition & 0 deletions std/mod.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
Set = mod.set;
Path = mod.path;
String = mod.string;
Std = std;
}
6 changes: 3 additions & 3 deletions test/std-import/import.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]; };
}
18 changes: 9 additions & 9 deletions test/std-import/import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ 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
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
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" ]' ]]
5 changes: 3 additions & 2 deletions test/std-import/import/mod.nix
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 933ee30

Please sign in to comment.