-
-
Notifications
You must be signed in to change notification settings - Fork 24
[WIP]: feat: add fixedTo.{atLeast,exactly,upTo} #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # "Just Give 'Em One of These" - Moe Szyslak | ||
| # A __functor that applies context to parametric includes (functions) and recurses into other included aspects | ||
| { lib, ... }: | ||
| let | ||
| recursiveApply = | ||
| apply: ctx: include: | ||
| if include ? includes then recursiveFunctor apply include ctx else apply ctx include; | ||
| recursiveFunctor = | ||
| apply: aspect: | ||
| aspect | ||
| // { | ||
| __functor = self: ctx: { | ||
| includes = | ||
| self.includes or [ ] | ||
| |> builtins.filter lib.isFunction | ||
| |> map (recursiveApply apply ctx) | ||
| |> builtins.filter (x: x != { }); | ||
| }; | ||
| }; | ||
| in | ||
| recursiveFunctor |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| { den, ... }: | ||
| { den, lib, ... }: | ||
| let | ||
| take.unused = _unused: used: used; | ||
| take.exactly = take den.lib.canTake.exactly; | ||
| take.atLeast = take den.lib.canTake.atLeast; | ||
| take.exactly = take (_fn: ctx: ctx) den.lib.canTake.exactly; | ||
| take.atLeast = take (_fn: ctx: ctx) den.lib.canTake.atLeast; | ||
| take.upTo = take (fn: fn |> lib.functionsArgs |> builtins.intersectAttrs) den.lib.canTake.upTo; | ||
| take.__functor = | ||
| _: takes: fn: ctx: | ||
| if takes ctx fn then fn ctx else { }; | ||
| _: takes: adapter: fn: ctx: | ||
| if takes ctx fn then fn (adapter fn ctx) else { }; | ||
| in | ||
| take |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| { denTest, ... }: | ||
| { | ||
| flake.tests.parametric = { | ||
|
|
||
| test-parametric-forwards-context = denTest ( | ||
| { den, igloo, ... }: | ||
| { | ||
| den, | ||
| igloo, | ||
| ... | ||
| }: | ||
| let | ||
| foo = den.lib.parametric { | ||
| includes = [ | ||
|
|
@@ -26,7 +29,11 @@ | |
| ); | ||
|
|
||
| test-parametric-owned-config = denTest ( | ||
| { den, igloo, ... }: | ||
| { | ||
| den, | ||
| igloo, | ||
| ... | ||
| }: | ||
| let | ||
| foo = den.lib.parametric { | ||
| nixos.networking.hostName = "from-parametric-owned"; | ||
|
|
@@ -43,7 +50,11 @@ | |
| ); | ||
|
|
||
| test-parametric-fixedTo = denTest ( | ||
| { den, igloo, ... }: | ||
| { | ||
| den, | ||
| igloo, | ||
| ... | ||
| }: | ||
| let | ||
| foo = | ||
| { host, ... }: | ||
|
|
@@ -68,12 +79,20 @@ | |
| ); | ||
|
|
||
| test-parametric-expands = denTest ( | ||
| { den, igloo, ... }: | ||
| { | ||
| den, | ||
| igloo, | ||
| ... | ||
| }: | ||
| let | ||
| foo = den.lib.parametric.expands { planet = "Earth"; } { | ||
| includes = [ | ||
| ( | ||
| { host, planet, ... }: | ||
| { | ||
| host, | ||
| planet, | ||
| ... | ||
| }: | ||
| { | ||
| nixos.users.users.tux.description = "${host.name}/${planet}"; | ||
| } | ||
|
|
@@ -91,7 +110,11 @@ | |
| ); | ||
|
|
||
| test-never-matches-aspect-skipped = denTest ( | ||
| { den, igloo, ... }: | ||
| { | ||
| den, | ||
| igloo, | ||
| ... | ||
| }: | ||
| let | ||
| never-matches = | ||
| { never-exists, ... }: | ||
|
|
@@ -113,5 +136,145 @@ | |
| } | ||
| ); | ||
|
|
||
| test-parametric-fixedTo-atLeast = denTest ( | ||
| { | ||
| den, | ||
| lib, | ||
| inputs, | ||
| ... | ||
| }: | ||
| let | ||
| inherit (den.lib.parametric) fixedTo; | ||
| testAspect = name: include: { | ||
| nixos.test = [ "excluded-owned-${name}" ]; | ||
|
|
||
| _.host = | ||
| { host }: | ||
| { | ||
| nixos.test = [ "${host}-${name}" ]; | ||
| }; | ||
|
|
||
| _.host-user = | ||
| { host, user }: | ||
| { | ||
| nixos.test = [ "${host}-${user}-${name}" ]; | ||
| }; | ||
|
|
||
| _.static = | ||
| { class, ... }: | ||
| { | ||
| ${class}.test = [ "excluded-static-${name}" ]; | ||
| }; | ||
|
|
||
| includes = include ++ [ | ||
| den.aspects.${name}._.host | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I belive there's a fixed point for aspects: { aspect,...} see lib/aspects/types.nix so you don't have to access by name, aspect can reference itself, just an option. |
||
| den.aspects.${name}._.host-user | ||
| den.aspects.${name}._.static | ||
| ]; | ||
| }; | ||
| testOptionProvider = args: aspect: { | ||
| includes = [ | ||
| aspect | ||
| { | ||
| __functor = self: _: { | ||
| nixos.options.test = lib.mkOption { type = lib.types.listOf lib.types.str; }; | ||
| }; | ||
| __functionArgs = | ||
| args | ||
| |> map (arg: { | ||
| name = arg; | ||
| value = false; | ||
| }) | ||
| |> builtins.listToAttrs; | ||
| } | ||
| ]; | ||
| }; | ||
| in | ||
| { | ||
| den.aspects.inner = testAspect "inner" [ ]; | ||
| den.aspects.outer = testAspect "outer" [ den.aspects.inner ]; | ||
|
|
||
| expr = | ||
| { | ||
| exactlyHost = { | ||
| ctx = { | ||
| host = "igloo"; | ||
| }; | ||
| functor = fixedTo.exactly; | ||
| }; | ||
| exactlyHostUser = { | ||
| ctx = { | ||
| host = "igloo"; | ||
| user = "tux"; | ||
| }; | ||
| functor = fixedTo.exactly; | ||
| }; | ||
| upToHost = { | ||
| ctx = { | ||
| host = "igloo"; | ||
| }; | ||
| functor = fixedTo.upTo; | ||
| }; | ||
| upToHostUser = { | ||
| ctx = { | ||
| host = "igloo"; | ||
| user = "tux"; | ||
| }; | ||
| functor = fixedTo.upTo; | ||
| }; | ||
| atLeastHost = { | ||
| ctx = { | ||
| host = "igloo"; | ||
| }; | ||
| functor = fixedTo.atLeast; | ||
| }; | ||
| # This test case errors because atLeast tries to call { host }: with { host, user } causing an error | ||
| # this is IMO incorrect behaviour, but would technically be a breaking change if people are using | ||
| # args@{ host, ... } which is why I introduced a new kind "upTo" which uses the canTake.atLeast | ||
| # predicate but only calls the function with the attributes it expects | ||
| # atLeastHostUser = { | ||
| # ctx = { | ||
| # host = "igloo"; | ||
| # user = "tux"; | ||
| # }; | ||
| # parametricFunctor = atLeast.fixed; | ||
| # }; | ||
| } | ||
| |> lib.mapAttrs ( | ||
| _: test: | ||
| den.aspects.outer | ||
| |> testOptionProvider (builtins.attrNames test.ctx) | ||
| |> (lib.flip test.functor) test.ctx | ||
| |> den.lib.aspects.resolve "nixos" [ ] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this api changed when we bundled flake aspects, no need for the list at the end |
||
| |> (nixos: inputs.nixpkgs.lib.evalModules { modules = [ nixos ]; }) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about defining options.test here, since it is used for checking values. instead of defining the option at an aspect. |
||
| |> (x: x.config.test) | ||
| ); | ||
|
Comment on lines
+243
to
+251
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I was working on this, I wanted to stay away from |
||
|
|
||
| expected = { | ||
| atLeastHost = [ | ||
| "igloo-inner" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to have these assertions separated. you can still use a factory function that wraps denTest, the reason is we better have independent tests that can give us clues of what to fix in the future. |
||
| "igloo-outer" | ||
| ]; | ||
| exactlyHost = [ | ||
| "igloo-inner" | ||
| "igloo-outer" | ||
| ]; | ||
| exactlyHostUser = [ | ||
| "igloo-tux-inner" | ||
| "igloo-tux-outer" | ||
| ]; | ||
| upToHost = [ | ||
| "igloo-inner" | ||
| "igloo-outer" | ||
| ]; | ||
| upToHostUser = [ | ||
| "igloo-tux-inner" | ||
| "igloo-inner" | ||
| "igloo-tux-outer" | ||
| "igloo-outer" | ||
| ]; | ||
| }; | ||
| } | ||
| ); | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a giant test of all the new functionality, I would need to split this apart.