diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 77f72fa9427675..0c30b2874a86f9 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -2520,4 +2520,38 @@ runTests { }; expected = "c"; }; + + # Check that `packagesFromDirectoryRecursive` can be used to create scopes + # for sub-directories + testPackagesFromDirectoryNestedScopes = let + inherit (lib) makeScope recurseIntoAttrs; + emptyScope = makeScope lib.callPackageWith (_: {}); + in { + expr = lib.filterAttrsRecursive (name: value: !lib.elem name [ "callPackage" "newScope" "overrideScope" "packages" ]) (packagesFromDirectoryRecursive { + inherit (emptyScope) callPackage newScope; + recurseIntoDirectory = f: { newScope, ... }@args: + recurseIntoAttrs (makeScope newScope (self: + f (args // { + inherit (self) callPackage newScope; + }) + )); + directory = ./packages-from-directory/scope; + }); + expected = lib.recurseIntoAttrs { + a = "a"; + b = "b"; + # Note: Other files/directories in `./test-data/c/` are ignored and can be + # used by `package.nix`. + c = "c"; + my-namespace = lib.recurseIntoAttrs { + d = "d"; + e = "e"; + f = "f"; + my-sub-namespace = lib.recurseIntoAttrs { + g = "g"; + h = "h"; + }; + }; + }; + }; } diff --git a/lib/tests/packages-from-directory/scope/a.nix b/lib/tests/packages-from-directory/scope/a.nix new file mode 100644 index 00000000000000..54f9eafd8e8782 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/a.nix @@ -0,0 +1,2 @@ +{ }: +"a" diff --git a/lib/tests/packages-from-directory/scope/b.nix b/lib/tests/packages-from-directory/scope/b.nix new file mode 100644 index 00000000000000..4201bac5693ebc --- /dev/null +++ b/lib/tests/packages-from-directory/scope/b.nix @@ -0,0 +1,3 @@ +{ a }: +assert a == "a"; +"b" diff --git a/lib/tests/packages-from-directory/scope/c/my-extra-feature.patch b/lib/tests/packages-from-directory/scope/c/my-extra-feature.patch new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/lib/tests/packages-from-directory/scope/c/not-a-namespace/not-a-package.nix b/lib/tests/packages-from-directory/scope/c/not-a-namespace/not-a-package.nix new file mode 100644 index 00000000000000..ffcd4415b08f85 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/c/not-a-namespace/not-a-package.nix @@ -0,0 +1 @@ +{ } diff --git a/lib/tests/packages-from-directory/scope/c/package.nix b/lib/tests/packages-from-directory/scope/c/package.nix new file mode 100644 index 00000000000000..c1203cdde96023 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/c/package.nix @@ -0,0 +1,2 @@ +{ }: +"c" diff --git a/lib/tests/packages-from-directory/scope/c/support-definitions.nix b/lib/tests/packages-from-directory/scope/c/support-definitions.nix new file mode 100644 index 00000000000000..ffcd4415b08f85 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/c/support-definitions.nix @@ -0,0 +1 @@ +{ } diff --git a/lib/tests/packages-from-directory/scope/my-namespace/d.nix b/lib/tests/packages-from-directory/scope/my-namespace/d.nix new file mode 100644 index 00000000000000..9496d021a94c1b --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/d.nix @@ -0,0 +1,5 @@ +{ a, e }: +# Check we can get parameter from the parent scope(s) as well as the current one +assert a == "a"; +assert e == "e"; +"d" diff --git a/lib/tests/packages-from-directory/scope/my-namespace/e.nix b/lib/tests/packages-from-directory/scope/my-namespace/e.nix new file mode 100644 index 00000000000000..276b80deab4063 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/e.nix @@ -0,0 +1,3 @@ +{ d }: +# Check that mutual recursion is possible +"e" diff --git a/lib/tests/packages-from-directory/scope/my-namespace/f/package.nix b/lib/tests/packages-from-directory/scope/my-namespace/f/package.nix new file mode 100644 index 00000000000000..c9a66c2eb1208f --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/f/package.nix @@ -0,0 +1,2 @@ +{ }: +"f" diff --git a/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/g.nix b/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/g.nix new file mode 100644 index 00000000000000..3f8f3a072a6ade --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/g.nix @@ -0,0 +1,3 @@ +{ a, d, h }: +# Check we can get parameters from ancestral scopes (e.g. the scope's grandparent) +"g" diff --git a/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/h.nix b/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/h.nix new file mode 100644 index 00000000000000..3756275ba75249 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/h.nix @@ -0,0 +1,2 @@ +{ }: +"h"