Skip to content

Commit

Permalink
fix: match eka import semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
nrdxp committed Oct 1, 2024
1 parent 4fa1b59 commit ac14df3
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 52 deletions.
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let
dev = (import ./src/core/importAtom.nix) { } ./src/dev.toml;
dev = (import ./src/core/importAtom.nix) { } ./src/dev.atom;
in
dev.shell
File renamed without changes.
86 changes: 49 additions & 37 deletions src/core/compose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,31 @@
*/
let
l = builtins;
src = import ./mod.nix;
core = import ./mod.nix;
in
{
src,
root,
config,
extern ? { },
features ? [ ],
# internal features of the composer function
stdFeatures ? src.stdToml.features.default or [ ],
coreFeatures ? src.coreToml.features.default,
stdFeatures ? core.stdToml.features.default or [ ],
coreFeatures ? core.coreToml.features.default,
# enable testing code paths
__internal__test ? false,
__isStd__ ? false,
}:
dir':
let
par = src.prepDir dir';
par = (root + "/${src}");

std = src.importStd {
std = core.importStd {
features = stdFeatures;
inherit __internal__test;
} ../std.toml;
} ../std.atom;

coreFeatures' = src.features.resolve src.coreToml.features coreFeatures;
stdFeatures' = src.features.resolve src.stdToml.features stdFeatures;
coreFeatures' = core.features.resolve core.coreToml.features coreFeatures;
stdFeatures' = core.features.resolve core.stdToml.features stdFeatures;

__atom = config // {
features = config.features or { } // {
Expand All @@ -90,7 +91,7 @@ let
};
};

msg = src.errors.debugMsg config;
msg = core.errors.debugMsg config;

f =
f: pre: dir:
Expand All @@ -104,9 +105,9 @@ let

scope =
let
scope' = with src; {
scope' = with core; {
inherit __atom;
mod = self';
mod = modScope;
builtins = std;
import = errors.import;
scopedImport = errors.import;
Expand All @@ -119,15 +120,15 @@ let
__getFlake = errors.import;
};

scope'' = src.set.inject scope' [
scope'' = core.set.inject scope' [
preOpt
{
_if = !__isStd__ && l.elem "std" coreFeatures';
inherit std;
}
{
_if = !__isStd__;
atom = atom';
atom = atomScope;
}
{
_if = __isStd__;
Expand All @@ -141,7 +142,8 @@ let
# a copy of the global scope, for testing if values exist
# for our internal testing functions
scope = scope'';
inherit src __isStd__ __internal__test;
inherit __isStd__ __internal__test;
src = core;
};
}
];
Expand All @@ -153,59 +155,69 @@ let
g =
name: type:
let
path = src.path.make dir name;
file = src.file.parse name;
path = core.path.make dir name;
file = core.file.parse name;
member = Import (l.path { inherit path name; });
module = src.path.make path "mod.nix";
module = core.path.make path "mod.nix";
in
if type == "directory" && l.pathExists module then
{ ${name} = f ((src.lowerKeys self) // src.set.when preOpt) path; }
{ ${name} = f ((core.lowerKeys mod) // core.set.when preOpt) path; }
else if type == "regular" && file.ext or null == "nix" && name != "mod.nix" then
{
${file.name} =
let
trace = src.errors.modPath par dir;
trace = core.errors.modPath par dir;
in
src.errors.context (msg "${trace}.${file.name}") member;
core.errors.context (msg "${trace}.${file.name}") member;
}
else
null # Ignore other file types
;

self' = src.lowerKeys (l.removeAttrs self [ "mod" ] // { outPath = src.rmNixSrcs dir; });
modScope = core.lowerKeys (l.removeAttrs mod [ "mod" ] // { outPath = core.rmNixSrcs dir; });

self =
mod =
let
path = src.path.make dir "mod.nix";
path = core.path.make dir "mod.nix";
module = Import (
l.path {
inherit path;
name = baseNameOf path;
}
);
trace = src.errors.modPath par dir;
trace = core.errors.modPath par dir;
in
assert src.modIsValid module dir;
src.filterMap g contents // (src.errors.context (msg trace) module);
assert core.modIsValid module dir;
core.filterMap g contents // (core.errors.context (msg trace) module);

in
if src.hasMod contents then
src.collectPublic self
if core.hasMod contents then
core.collectPublic mod
else
# Base case: no module
{ };

atom' = l.removeAttrs (extern // atom // { inherit extern; }) [
"atom"
(baseNameOf par)
];
atomScope =
l.removeAttrs
(
extern
// atom
// {
inherit extern;
outPath = core.rmAtomSrc root config.atom.id src;
}
)
[
"atom"
(baseNameOf par)
];

atom =
let
fixed = src.fix f null par;
fixed = core.fix f null par;
in
src.set.inject fixed [
({ _if = __isStd__; } // src.pureBuiltinsForStd fixed)
core.set.inject fixed [
({ _if = __isStd__; } // core.pureBuiltinsForStd fixed)
{
_if = __isStd__ && l.elem "lib" __atom.features.resolved.atom;
inherit (extern) lib;
Expand All @@ -221,7 +233,7 @@ in
assert
!__internal__test
# older versions of Nix don't have the `warn` builtin
|| src.errors.warn ''
|| core.errors.warn ''
in ${toString ./default.nix}:
Internal testing functionality is enabled via the `__test` boolean.
This should never be `true` except in internal test runs.
Expand Down
18 changes: 15 additions & 3 deletions src/core/importAtom.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ let
backend = config.backend or { };
nix = backend.nix or { };

root = builtins.seq id (atom.path or id);
root = mod.prepDir (dirOf path);
src = builtins.seq id (
let
file = mod.parse (baseNameOf path);
in
file.name
);
extern =
let
fetcher = nix.fetcher or "native"; # native doesn't exist yet
Expand Down Expand Up @@ -88,7 +94,13 @@ let

in
mod.compose {
inherit extern __internal__test config;
inherit
extern
__internal__test
config
root
src
;
features = features';
coreFeatures =
let
Expand All @@ -102,4 +114,4 @@ mod.compose {
mod.features.resolve mod.stdToml.features feat;

__isStd__ = meta.__is_std__ or false;
} (dirOf path + "/${root}")
}
17 changes: 15 additions & 2 deletions src/core/mod.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ let
std = builtins;
mod = scopedImport { inherit std mod; } ../std/string/mod.nix;
} ../std/string/toLowerCase.nix;
stdToml = l.fromTOML (l.readFile ../std.toml);
coreToml = l.fromTOML (l.readFile ../core.toml);
stdToml = l.fromTOML (l.readFile ../std.atom);
coreToml = l.fromTOML (l.readFile ../core.atom);
in
rec {
inherit
fix
parse
filterMap
stdFilter
stdToml
Expand Down Expand Up @@ -71,6 +72,18 @@ rec {
);
};

rmAtomSrc =
path: name: src:
l.path {
inherit name path;
filter =
path: type:
let
file = parse path;
in
file.name != src;
};

importStd = opts: importAtom { inherit (opts) __internal__test features; };

modIsValid =
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions test/features/resolve.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ let
f = import ../../src/core/importAtom.nix { __internal__test = true; };
in
{
recursive-features = f ./recursive-features.toml;
recursive-features-loop = f ./recursive-features-loop.toml;
recursive-features = f ./recursive-features.atom;
recursive-features-loop = f ./recursive-features-loop.atom;
}
File renamed without changes.
2 changes: 1 addition & 1 deletion test/integrity/bld.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let
f = import ../../src/core/importAtom.nix;
atom = f { } (
# added to test implicit path conversion when path is a string
builtins.toPath ./bld.toml
builtins.toPath ./bld.atom
);
in
builtins.deepSeq atom atom
File renamed without changes.
2 changes: 1 addition & 1 deletion test/pre.nix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(import ../src/core/importAtom.nix { }) ./pre.toml
(import ../src/core/importAtom.nix { }) ./pre.atom
2 changes: 1 addition & 1 deletion test/purity/purity.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let
f = import ../../src/core/importAtom.nix;
atom = f { __internal__test = true; } (
# added to test implicit path conversion when path is a string
builtins.toPath ./test.toml
builtins.toPath ./test.atom
);
in
atom
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions test/std-import/import.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ let
f = import ../../src/core/importAtom.nix { __internal__test = true; };
in
{
default = f ./default.toml;
noStd = f ./no-std.toml;
explicit = f ./explicit.toml;
withLib = f ./with-lib.toml;
default = f ./default.atom;
noStd = f ./no-std.atom;
explicit = f ./explicit.atom;
withLib = f ./with-lib.atom;
}
File renamed without changes.
File renamed without changes.

0 comments on commit ac14df3

Please sign in to comment.