Skip to content

Commit 6f104de

Browse files
committed
loaders.scoped: init
1 parent b915b66 commit 6f104de

File tree

10 files changed

+63
-24
lines changed

10 files changed

+63
-24
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ Type: `{ ... } -> Path -> Path`
161161

162162
This loader will simply return the path of the file without `import`ing it.
163163

164+
### [`loaders.scoped`](src/loaders/scoped.nix)
165+
166+
Type: `{ self, super, root, ... } -> Path -> a`
167+
168+
This is like [`loaders.default`], except it uses `scoepdImport` instead of `import`.
169+
With this loader, you don't have to explicitly declare the inputs with a lambda,
170+
since `scopedImport` will take care of it as if the file being loaded is wrapped with `with inputs;`.
171+
164172
### [`loaders.verbatim`](src/loaders/verbatim.nix)
165173

166174
Type: `{ ... } -> Path -> a`

default.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ let
44
load = import ./src/load.nix {
55
inherit lib;
66
root.loaders.default = import ./src/loaders {
7-
inherit lib;
7+
super.defaultWith = import ./src/loaders/__defaultWith.nix {
8+
inherit lib;
9+
};
810
};
911
};
1012
in

src/loaders/__defaultWith.nix

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{ lib }:
2+
3+
let
4+
inherit (builtins)
5+
mapAttrs
6+
;
7+
inherit (lib)
8+
functionArgs
9+
pipe
10+
toFunction
11+
;
12+
in
13+
14+
importer:
15+
16+
inputs: path:
17+
18+
let
19+
f = toFunction (importer path);
20+
in
21+
22+
pipe f [
23+
functionArgs
24+
(mapAttrs (name: _: inputs.${name}))
25+
f
26+
]

src/loaders/default.nix

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
{ lib }:
1+
{ super }:
22

3-
let
4-
inherit (builtins)
5-
mapAttrs
6-
;
7-
inherit (lib)
8-
functionArgs
9-
pipe
10-
toFunction
11-
;
12-
in
13-
14-
inputs: path:
15-
16-
let
17-
f = toFunction (import path);
18-
in
19-
20-
pipe f [
21-
functionArgs
22-
(mapAttrs (name: _: inputs.${name}))
23-
f
24-
]
3+
super.defaultWith import

src/loaders/scoped.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{ super }:
2+
3+
inputs:
4+
5+
super.defaultWith (scopedImport inputs) inputs

tests/scoped/__fixture/answer.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
answer

tests/scoped/__fixture/bar.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"${super.foo}bar"

tests/scoped/__fixture/foo.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_:
2+
3+
"foo"

tests/scoped/expected.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
answer = 42;
3+
foo = "foo";
4+
bar = "foobar";
5+
}

tests/scoped/expr.nix

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{ haumea }:
2+
3+
haumea.load {
4+
src = ./__fixture;
5+
loader = haumea.loaders.scoped;
6+
inputs = {
7+
answer = 42;
8+
};
9+
}

0 commit comments

Comments
 (0)