-
Notifications
You must be signed in to change notification settings - Fork 48
refactor(flake): extract evalModules to lib/eval-modules.nix #143
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
Open
sshine
wants to merge
7
commits into
hall:main
Choose a base branch
from
sshine:expose-eval-modules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8cb1850
tests(eval-modules): pin the evaluator and overlay interfaces
sshine d430138
refactor(flake): resolve kubenix.modules without going through self
sshine ddbee52
refactor(flake): make evalModules self-recursive
sshine dfd9b3f
refactor(flake): evaluate modules against the caller's pkgs
sshine f507ff9
refactor(flake): parameterise evalModules on pkgs rather than a system
sshine 7b86b64
refactor(flake): extract evalModules to lib/eval-modules.nix
sshine 904b681
fix(flake): bind the overlay's evalModules to the pkgs it extends
sshine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,16 @@ | ||
| { inputs, self, config, ... }: | ||
| { inputs, config, ... }: | ||
| let | ||
| # evalModules with same interface as lib.evalModules and kubenix as special argument | ||
| mkEvalModules = system: | ||
| let | ||
| pkgs = inputs.nixpkgs.legacyPackages.${system}; | ||
| in | ||
| attrs @ { module ? null, modules ? [ module ], ... }: | ||
| let | ||
| lib' = pkgs.lib.extend (lib: _self: import ../lib/upstreamables.nix { inherit lib pkgs; }); | ||
| attrs' = builtins.removeAttrs attrs [ "module" ]; | ||
| in | ||
| lib'.evalModules (pkgs.lib.recursiveUpdate | ||
| { | ||
| modules = modules ++ [{ | ||
| config._module.args = { | ||
| inherit pkgs; | ||
| name = "default"; | ||
| }; | ||
| }]; | ||
| specialArgs = { | ||
| pkgs = import inputs.nixpkgs { | ||
| inherit (pkgs.stdenv.hostPlatform) system; | ||
| overlays = [ self.overlays.default ]; | ||
| config.allowUnsupportedSystem = true; | ||
| }; | ||
|
|
||
| kubenix = { | ||
| lib = import ../lib { inherit pkgs; inherit (pkgs) lib; }; | ||
| evalModules = self.evalModules.${pkgs.stdenv.hostPlatform.system}; | ||
| modules = self.nixosModules.kubenix; | ||
| }; | ||
| }; | ||
| } | ||
| attrs'); | ||
| # Kept in lib/ so consumers without flake machinery, such as nixpkgs, can import the evaluator | ||
| # directly. The flake only binds it to a package set per system. | ||
| mkEvalModules = pkgs: import ../lib/eval-modules.nix { inherit pkgs; }; | ||
| in | ||
| { | ||
| flake.evalModules = inputs.nixpkgs.lib.genAttrs config.systems mkEvalModules; | ||
| flake.evalModules = inputs.nixpkgs.lib.genAttrs config.systems ( | ||
| system: mkEvalModules inputs.nixpkgs.legacyPackages.${system} | ||
| ); | ||
|
|
||
| # Share this system's evalModules with the other perSystem modules. | ||
| perSystem = { system, ... }: { | ||
| _module.args.evalModules = mkEvalModules system; | ||
| _module.args.evalModules = mkEvalModules inputs.nixpkgs.legacyPackages.${system}; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| { self, ... }: | ||
| { ... }: | ||
| { | ||
| flake.overlays.default = _final: prev: { | ||
| kubenix.evalModules = self.evalModules.${prev.stdenv.hostPlatform.system}; | ||
| # Bound to the package set being extended, so modules evaluated through it see the consumer's | ||
| # nixpkgs. Going via self.evalModules.<system> instead would pin them to kubenix's own. | ||
| flake.overlays.default = final: _prev: { | ||
| kubenix.evalModules = import ../lib/eval-modules.nix { pkgs = final; }; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # evalModules with same interface as lib.evalModules and kubenix as special argument | ||
| { pkgs }: | ||
| let | ||
| evalModules = attrs @ { module ? null, modules ? [ module ], ... }: | ||
| let | ||
| lib' = pkgs.lib.extend (lib: _self: import ./upstreamables.nix { inherit lib pkgs; }); | ||
| attrs' = builtins.removeAttrs attrs [ "module" ]; | ||
| in | ||
| lib'.evalModules (pkgs.lib.recursiveUpdate | ||
| { | ||
| modules = modules ++ [{ | ||
| config._module.args = { | ||
| inherit pkgs; | ||
| name = "default"; | ||
| }; | ||
| }]; | ||
| specialArgs = { | ||
| inherit pkgs; | ||
|
|
||
| kubenix = { | ||
| lib = import ./. { inherit pkgs; inherit (pkgs) lib; }; | ||
| inherit evalModules; | ||
| modules = import ../modules; | ||
| }; | ||
| }; | ||
| } | ||
| attrs'); | ||
| in | ||
| evalModules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| { pkgs | ||
| , evalModules | ||
| , ... | ||
| }: | ||
| let | ||
| inherit (pkgs) lib; | ||
|
|
||
| pod = evalModules { | ||
| module = { kubenix, ... }: { | ||
| imports = [ kubenix.modules.k8s ]; | ||
| kubernetes.resources.pods.example.spec.containers.example.image = "nginx"; | ||
| }; | ||
| }; | ||
|
|
||
| # Reaches every member of the `kubenix` special argument from inside a module: `modules` in | ||
| # `imports` position, `lib` in a value position, and `evalModules` for a nested evaluation. | ||
| probe = evalModules { | ||
| module = { kubenix, lib, ... }: { | ||
| imports = [ kubenix.modules.k8s ]; | ||
|
|
||
| options.probe = lib.mkOption { type = lib.types.attrs; }; | ||
|
|
||
| config.probe = { | ||
| libNamespaces = builtins.attrNames kubenix.lib; | ||
| nested = (kubenix.evalModules { | ||
| module = { kubenix, ... }: { | ||
| imports = [ kubenix.modules.k8s ]; | ||
| kubernetes.resources.configMaps.nested.data.key = "value"; | ||
| }; | ||
| }).config.kubernetes.api.resources.configMaps.nested.metadata.name; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| assertions = [ | ||
| { | ||
| message = "evalModules renders a Pod with apiVersion, kind and name"; | ||
| assertion = | ||
| let o = pod.config.kubernetes.api.resources.pods.example; | ||
| in o.apiVersion == "v1" && o.kind == "Pod" && o.metadata.name == "example"; | ||
| } | ||
| { | ||
| message = "kubenix.modules is reachable from a module's imports"; | ||
| assertion = pod.config.kubernetes.api.resources.pods ? example; | ||
| } | ||
| { | ||
| message = "kubenix.lib exposes the k8s, docker and helm namespaces"; | ||
| assertion = probe.config.probe.libNamespaces == [ "docker" "helm" "k8s" ]; | ||
| } | ||
| { | ||
| message = "kubenix.evalModules supports nested evaluation"; | ||
| assertion = probe.config.probe.nested == "nested"; | ||
| } | ||
| ]; | ||
|
|
||
| failures = map (a: a.message) (builtins.filter (a: !a.assertion) assertions); | ||
| in | ||
| pkgs.runCommand "kubenix-eval-modules-interface" | ||
| { | ||
| passthru = { inherit pod probe; }; | ||
| } | ||
| ( | ||
| if failures == [ ] | ||
| then "echo success > $out" | ||
| else "echo ${lib.escapeShellArg (builtins.concatStringsSep "\n" failures)} >&2; exit 1" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| { pkgs | ||
| , overlay | ||
| , ... | ||
| }: | ||
| let | ||
| inherit (pkgs) lib; | ||
|
|
||
| marker = "sees-extended-package-set"; | ||
|
|
||
| overlaid = pkgs.extend ( | ||
| lib.composeExtensions overlay (_final: _prev: { kubenixOverlayMarker = marker; }) | ||
| ); | ||
|
|
||
| assertions = [ | ||
| { | ||
| message = "the overlay provides pkgs.kubenix.evalModules"; | ||
| assertion = (overlaid ? kubenix) && builtins.isFunction overlaid.kubenix.evalModules; | ||
| } | ||
| { | ||
| message = "the overlay's evalModules evaluates modules against the extended package set"; | ||
| assertion = (overlaid.kubenix.evalModules { | ||
| module = { pkgs, lib, ... }: { | ||
| options.marker = lib.mkOption { default = pkgs.kubenixOverlayMarker or null; }; | ||
| }; | ||
| }).config.marker == marker; | ||
| } | ||
| ]; | ||
|
|
||
| failures = map (a: a.message) (builtins.filter (a: !a.assertion) assertions); | ||
| in | ||
| pkgs.runCommand "kubenix-overlay" { } ( | ||
| if failures == [ ] | ||
| then "echo success > $out" | ||
| else "echo ${lib.escapeShellArg (builtins.concatStringsSep "\n" failures)} >&2; exit 1" | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure why the reason for this was but it seems like we lost it
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 regression -- I was experimenting with agentic workflows and created a pull request before self-reviewing. Sorry for the delay; I'll re-request review when it's ready.