zaphyra's git: haumea

fork of https://github.com/nix-community/haumea

commit 6f104dee0b519cba986efb2a9166b10ed1e33eae
parent b915b66b27da3a595d77b139e945bb0a2fcac926
Author: figsoda <figsoda@pm.me>
Date: Mon, 17 Apr 2023 12:28:50 -0400

loaders.scoped: init
10 files changed, 63 insertions(+), 24 deletions(-)
diff --git a/README.md b/README.md
@@ -161,6 +161,14 @@ Type: `{ ... } -> Path -> Path`
 
 This loader will simply return the path of the file without `import`ing it.
 
+### [`loaders.scoped`](src/loaders/scoped.nix)
+
+Type: `{ self, super, root, ... } -> Path -> a`
+
+This is like [`loaders.default`], except it uses `scoepdImport` instead of `import`.
+With this loader, you don't have to explicitly declare the inputs with a lambda,
+since `scopedImport` will take care of it as if the file being loaded is wrapped with `with inputs;`.
+
 ### [`loaders.verbatim`](src/loaders/verbatim.nix)
 
 Type: `{ ... } -> Path -> a`
diff --git a/default.nix b/default.nix
@@ -4,7 +4,9 @@ let
   load = import ./src/load.nix {
     inherit lib;
     root.loaders.default = import ./src/loaders {
-      inherit lib;
+      super.defaultWith = import ./src/loaders/__defaultWith.nix {
+        inherit lib;
+      };
     };
   };
 in
diff --git a/src/loaders/__defaultWith.nix b/src/loaders/__defaultWith.nix
@@ -0,0 +1,26 @@
+{ lib }:
+
+let
+  inherit (builtins)
+    mapAttrs
+    ;
+  inherit (lib)
+    functionArgs
+    pipe
+    toFunction
+    ;
+in
+
+importer:
+
+inputs: path:
+
+let
+  f = toFunction (importer path);
+in
+
+pipe f [
+  functionArgs
+  (mapAttrs (name: _: inputs.${name}))
+  f
+]
diff --git a/src/loaders/default.nix b/src/loaders/default.nix
@@ -1,24 +1,3 @@
-{ lib }:
+{ super }:
 
-let
-  inherit (builtins)
-    mapAttrs
-    ;
-  inherit (lib)
-    functionArgs
-    pipe
-    toFunction
-    ;
-in
-
-inputs: path:
-
-let
-  f = toFunction (import path);
-in
-
-pipe f [
-  functionArgs
-  (mapAttrs (name: _: inputs.${name}))
-  f
-]
+super.defaultWith import
diff --git a/src/loaders/scoped.nix b/src/loaders/scoped.nix
@@ -0,0 +1,5 @@
+{ super }:
+
+inputs:
+
+super.defaultWith (scopedImport inputs) inputs
diff --git a/tests/scoped/__fixture/answer.nix b/tests/scoped/__fixture/answer.nix
@@ -0,0 +1 @@
+answer
diff --git a/tests/scoped/__fixture/bar.nix b/tests/scoped/__fixture/bar.nix
@@ -0,0 +1 @@
+"${super.foo}bar"
diff --git a/tests/scoped/__fixture/foo.nix b/tests/scoped/__fixture/foo.nix
@@ -0,0 +1,3 @@
+_:
+
+"foo"
diff --git a/tests/scoped/expected.nix b/tests/scoped/expected.nix
@@ -0,0 +1,5 @@
+{
+  answer = 42;
+  foo = "foo";
+  bar = "foobar";
+}
diff --git a/tests/scoped/expr.nix b/tests/scoped/expr.nix
@@ -0,0 +1,9 @@
+{ haumea }:
+
+haumea.load {
+  src = ./__fixture;
+  loader = haumea.loaders.scoped;
+  inputs = {
+    answer = 42;
+  };
+}