Skip to content

Commit

Permalink
chore: builtin features
Browse files Browse the repository at this point in the history
Use and reserve `std` and `core` features as
always-on builtin features.
  • Loading branch information
blaggacao committed Aug 9, 2024
1 parent 61adaf4 commit 4ed851f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 44 deletions.
8 changes: 6 additions & 2 deletions src/atom.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "atom"
version = "0.3.0"
description = "A purpose built, unopinionated, and performant module system for Nix code."

[atom.meta]
# special attribute for the core
__root_features__ = ["core"]

# all features of atom.toml are builtin features
[features]
std = []
default = ["std"]
core = ["std"]
21 changes: 4 additions & 17 deletions src/atom/compose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ in
config,
extern ? { },
features ? [ ],
# internal features of the composer function
stdFeatures ? src.stdToml.features.default or [ ],
composeFeatures ? src.atomToml.features.default,
# enable testing code paths
__internal__test ? false,
__isStd__ ? false,
Expand All @@ -17,21 +14,11 @@ dir':
let
dir = src.prepDir dir';

std = src.readStd {
features = stdFeatures;
inherit __internal__test;
} ../std.toml;

composeFeatures' = src.features.parse src.atomToml.features composeFeatures;
stdFeatures' = src.features.parse src.stdToml.features stdFeatures;
std = src.fromManifest { inherit features __internal__test; } ../std.toml;

__atom = config // {
features = config.features or { } // {
parsed = {
atom = features;
compose = composeFeatures';
std = stdFeatures';
};
__parsed = features;
};
};

Expand Down Expand Up @@ -65,7 +52,7 @@ let
scope'' = src.set.inject scope' [
preOpt
{
_if = !__isStd__ && l.elem "std" composeFeatures';
_if = !__isStd__ && l.elem "std" features;
inherit std;
}
{
Expand Down Expand Up @@ -145,7 +132,7 @@ let
}
)
{
_if = __isStd__ && l.elem "pkg_lib" __atom.features.parsed.atom;
_if = __isStd__ && l.elem "pkg_lib" __atom.features.__parsed;
inherit (extern) lib;
}
{
Expand Down
17 changes: 2 additions & 15 deletions src/atom/fromManifest.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ let

features' =
let
featSet = config.features or { };
featIn = if features == null then featSet.default or [ ] else features;
featSet = mod.features.addBuiltins (config.features or { });
featIn = mod.features.addRootFeatures (if features == null then featSet.default or [ ] else features);
in
mod.features.parse featSet featIn;

backend = config.backend or { };
nix = backend.nix or { };
compose = config.compose or { };

root = atom.path or name;
extern =
Expand Down Expand Up @@ -57,21 +56,9 @@ let

meta = atom.meta or { };

composeFeatures = compose.features or { };
in
(mod.compose) {
inherit extern __internal__test config;
features = features';
composeFeatures =
let
feat = composeFeatures.atom or mod.atomToml.features.default;
in
mod.features.parse mod.atomToml.features feat;
stdFeatures =
let
feat = composeFeatures.std or mod.stdToml.features.default;
in
mod.features.parse mod.stdToml.features feat;

__isStd__ = meta.__is_std__ or false;
} (dirOf path + "/${root}")
6 changes: 3 additions & 3 deletions src/atom/mod.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# to keep the core impelementation clean
let
l = builtins;
fromManifest = import ./fromManifest.nix;
fix = import ../std/fix.nix;
when = scopedImport { std = builtins; } ../std/set/when.nix;
filterMap = scopedImport { std = builtins; } ../std/set/filterMap.nix;
Expand Down Expand Up @@ -58,8 +57,6 @@ rec {
|| (type == "directory" && !l.pathExists "${path}/mod.nix")
);

readStd = opts: fromManifest { inherit (opts) __internal__test features; };

modIsValid =
mod: dir:
l.isAttrs mod
Expand All @@ -77,6 +74,9 @@ rec {
features.parse =
featureSet: l.foldl' (xs: x: if l.elem x xs then xs else [ x ] ++ featureSet.${x} ++ xs) [ ];

features.addBuiltins = featureSet: atomToml.features // stdToml.features // featureSet;
features.addRootFeatures = features: features ++ atomToml.meta.__root_features__;

# It is crucial that the directory is a path literal, not a string
# since the implicit copy to the /nix/store, which provides isolation,
# only happens for path literals.
Expand Down
3 changes: 1 addition & 2 deletions src/dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ name = "nixpkgs"
import = true
args = [{}]

[compose.features]
atom = ["std"]
[features]
std = ["pkg_lib"]

[backend.nix]
Expand Down
4 changes: 2 additions & 2 deletions src/std.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ fetcher = "npins"
# for the Nix std library
__is_std__ = true

# all features of std.toml are builtin features
[features]
pkg_lib = ["lib"]
default = []
std = [] # can have lib
4 changes: 2 additions & 2 deletions test/std-import/no-std.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
name = "test"
path = "import"

[compose.features]
atom = [] # defualt is ["std"]
[features]
core = [] # defualt is ["std"]
2 changes: 1 addition & 1 deletion test/std-import/pkglib.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
name = "test"
path = "import"

[compose.features]
[features]
std = ["pkg_lib"]

0 comments on commit 4ed851f

Please sign in to comment.