-
Notifications
You must be signed in to change notification settings - Fork 85
Switch to nix-eval-jobs for local evaluation
#473
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
Draft
B4dM4n
wants to merge
7
commits into
Mic92:master
Choose a base branch
from
B4dM4n:nix-eval-jobs
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d8cfea
Fix Review.build_commit merge behavior
B4dM4n c235719
Fix typo in console report
B4dM4n f4d26b9
Report failed tests as failed build
B4dM4n e02c148
Switch to nix-eval-jobs for local evaluation
B4dM4n 5d0b0dc
Use nix-eval-jobs in multi_system_eval
B4dM4n 72cc6d5
Perform nix_build without review shell
B4dM4n 58b0c55
Add flag to build package passthru.tests
B4dM4n 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 | ||
|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||
|
|
||||
| inputs = { | ||||
| nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; | ||||
|
|
||||
|
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.
Suggested change
|
||||
| flake-parts.url = "github:hercules-ci/flake-parts"; | ||||
| flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; | ||||
|
|
||||
|
|
||||
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,4 +1,3 @@ | ||
| import concurrent.futures | ||
| import json | ||
| import os | ||
| import shlex | ||
|
|
@@ -21,13 +20,13 @@ class Attr: | |
| exists: bool | ||
| broken: bool | ||
| blacklisted: bool | ||
| path: Path | None | ||
| drv_path: str | None | ||
| outputs: dict[str, Path] | None | ||
| drv_path: Path | None | ||
| aliases: list[str] = field(default_factory=list) | ||
| _path_verified: bool | None = field(init=False, default=None) | ||
|
|
||
| def was_build(self) -> bool: | ||
| if self.path is None: | ||
| if self.outputs is None or len(self.outputs) == 0: | ||
| return False | ||
|
|
||
| if self._path_verified is not None: | ||
|
|
@@ -42,7 +41,7 @@ def was_build(self) -> bool: | |
| "verify", | ||
| "--no-contents", | ||
| "--no-trust", | ||
| self.path, | ||
| *self.outputs.values(), | ||
| ], | ||
| stderr=subprocess.DEVNULL, | ||
| check=False, | ||
|
|
@@ -51,7 +50,15 @@ def was_build(self) -> bool: | |
| return self._path_verified | ||
|
|
||
| def is_test(self) -> bool: | ||
| return self.name.startswith("nixosTests") | ||
| return self.name.startswith("nixosTests") or ".passthru.tests." in self.name | ||
|
|
||
| def outputs_with_name(self) -> dict[str, Path]: | ||
| def with_output(output: str) -> str: | ||
| if output == "out": | ||
| return self.name | ||
| return f"{self.name}.{output}" | ||
|
|
||
| return {with_output(output): path for output, path in self.outputs.items()} | ||
|
|
||
|
|
||
| REVIEW_SHELL: Final[str] = str(ROOT.joinpath("nix/review-shell.nix")) | ||
|
|
@@ -187,7 +194,7 @@ def tmpfs(path: Path | str, is_dir: bool = True) -> list[str]: | |
| ] | ||
|
|
||
|
|
||
| def _nix_eval_filter(json: dict[str, Any]) -> list[Attr]: | ||
| def _nix_eval_filter(json: list[Any]) -> list[Attr]: | ||
| # workaround https://github.com/NixOS/ofborg/issues/269 | ||
| blacklist = { | ||
| "appimage-run-tests", | ||
|
|
@@ -200,27 +207,37 @@ def _nix_eval_filter(json: dict[str, Any]) -> list[Attr]: | |
| "tests.trivial", | ||
| "tests.writers", | ||
| } | ||
|
|
||
| def is_blacklisted(name: str) -> bool: | ||
| return name in blacklist or any( | ||
| name.startswith(f"{entry}.") for entry in blacklist | ||
| ) | ||
|
|
||
| attr_by_path: dict[Path, Attr] = {} | ||
| broken = [] | ||
| for name, props in json.items(): | ||
| path = props.get("path", None) | ||
| if path is not None: | ||
| path = Path(path) | ||
|
|
||
| for props in json: | ||
| drv_path = None | ||
| outputs = None | ||
| if not props["broken"]: | ||
| drv_path = Path(props["drvPath"]) | ||
| outputs = {output: Path(path) for output, path in props["outputs"].items()} | ||
|
|
||
| # the 'name' field might be quoted, so get the unqoted one from 'attrPath' | ||
| name = props["attrPath"][1] | ||
| attr = Attr( | ||
| name=name, | ||
| exists=props["exists"], | ||
| broken=props["broken"], | ||
| blacklisted=name in blacklist, | ||
| path=path, | ||
| drv_path=props["drvPath"], | ||
| blacklisted=is_blacklisted(name), | ||
| outputs=outputs, | ||
| drv_path=drv_path, | ||
| ) | ||
| if attr.path is not None: | ||
| other = attr_by_path.get(attr.path, None) | ||
| if attr.drv_path is not None: | ||
| other = attr_by_path.get(attr.drv_path, None) | ||
| if other is None: | ||
| attr_by_path[attr.path] = attr | ||
| attr_by_path[attr.drv_path] = attr | ||
| elif len(other.name) > len(attr.name): | ||
| attr_by_path[attr.path] = attr | ||
| attr_by_path[attr.drv_path] = attr | ||
| attr.aliases.append(other.name) | ||
| else: | ||
| other.aliases.append(attr.name) | ||
|
|
@@ -234,31 +251,60 @@ def nix_eval( | |
| system: str, | ||
| allow: AllowedFeatures, | ||
| nix_path: str, | ||
| num_parallel_evals: int, | ||
| max_memory_size: int, | ||
| include_passthru_tests: bool = False, | ||
| ) -> list[Attr]: | ||
| return multi_system_eval( | ||
| {system: attrs}, | ||
| allow=allow, | ||
| nix_path=nix_path, | ||
| num_parallel_evals=num_parallel_evals, | ||
| max_memory_size=max_memory_size, | ||
| include_passthru_tests=include_passthru_tests, | ||
| ).get(system, []) | ||
|
|
||
|
|
||
| def multi_system_eval( | ||
| attr_names_per_system: dict[System, set[str]], | ||
| allow: AllowedFeatures, | ||
| nix_path: str, | ||
| num_parallel_evals: int, | ||
| max_memory_size: int, | ||
| include_passthru_tests: bool = False, | ||
| ) -> dict[System, list[Attr]]: | ||
| attr_json = NamedTemporaryFile(mode="w+", delete=False) # noqa: SIM115 | ||
| delete = True | ||
| try: | ||
| json.dump(list(attrs), attr_json) | ||
| json.dump( | ||
| {system: list(attrs) for system, attrs in attr_names_per_system.items()}, | ||
| attr_json, | ||
| ) | ||
| eval_script = str(ROOT.joinpath("nix/evalAttrs.nix")) | ||
| attr_json.flush() | ||
| cmd = [ | ||
| "nix", | ||
| "nix-eval-jobs", | ||
| "--workers", | ||
| str(num_parallel_evals), | ||
| "--max-memory-size", | ||
| str(max_memory_size), | ||
| "--extra-experimental-features", | ||
| "nix-command" if allow.url_literals else "nix-command no-url-literals", | ||
| "--system", | ||
| system, | ||
| "eval", | ||
| "" if allow.url_literals else "no-url-literals", | ||
| "--expr", | ||
| f"""(import {eval_script} {{ | ||
| attr-json = {attr_json.name}; | ||
| include-passthru-tests = {str(include_passthru_tests).lower()}; | ||
| }})""", | ||
| "--nix-path", | ||
| nix_path, | ||
| "--json", | ||
| "--impure", | ||
| "--allow-import-from-derivation" | ||
| if allow.ifd | ||
| else "--no-allow-import-from-derivation", | ||
| "--expr", | ||
| f"(import {eval_script} {{ attr-json = {attr_json.name}; }})", | ||
| "--apply", | ||
| "d: { inherit (d) exists broken; }", | ||
| ] | ||
|
|
||
| info("$ " + shlex.join(cmd)) | ||
| nix_eval = subprocess.run(cmd, stdout=subprocess.PIPE, text=True, check=False) | ||
| if nix_eval.returncode != 0: | ||
| delete = False | ||
|
|
@@ -267,48 +313,30 @@ def nix_eval( | |
| ) | ||
| raise NixpkgsReviewError(msg) | ||
|
|
||
| return _nix_eval_filter(json.loads(nix_eval.stdout)) | ||
| systems_packages = {} | ||
| for line in nix_eval.stdout.splitlines(): | ||
| attrs = json.loads(line) | ||
| system = attrs["attrPath"][0] | ||
| systems_packages.setdefault(system, list()).append(attrs) | ||
|
|
||
| return { | ||
| system: _nix_eval_filter(packages) | ||
| for system, packages in systems_packages.items() | ||
| } | ||
| finally: | ||
| attr_json.close() | ||
| if delete: | ||
| Path(attr_json.name).unlink() | ||
|
|
||
|
|
||
| def multi_system_eval( | ||
| attr_names_per_system: dict[System, set[str]], | ||
| allow: AllowedFeatures, | ||
| nix_path: str, | ||
| n_threads: int, | ||
| ) -> dict[System, list[Attr]]: | ||
| results: dict[System, list[Attr]] = {} | ||
| with concurrent.futures.ThreadPoolExecutor(max_workers=n_threads) as executor: | ||
| future_to_system = { | ||
| executor.submit( | ||
| nix_eval, | ||
| attrs=attrs, | ||
| system=system, | ||
| allow=allow, | ||
| nix_path=nix_path, | ||
| ): system | ||
| for system, attrs in attr_names_per_system.items() | ||
| } | ||
| for future in concurrent.futures.as_completed(future_to_system): | ||
| system = future_to_system[future] | ||
| results[system] = future.result() | ||
|
|
||
| return results | ||
|
|
||
|
|
||
| def nix_build( | ||
| attr_names_per_system: dict[System, set[str]], | ||
| args: str, | ||
| cache_directory: Path, | ||
| local_system: System, | ||
| allow: AllowedFeatures, | ||
| build_graph: str, | ||
| nix_path: str, | ||
| nixpkgs_config: Path, | ||
| n_threads: int, | ||
| num_parallel_evals: int, | ||
| max_memory_size: int, | ||
| ) -> dict[System, list[Attr]]: | ||
| if not attr_names_per_system: | ||
| info("Nothing to be built.") | ||
|
|
@@ -318,33 +346,29 @@ def nix_build( | |
| attr_names_per_system, | ||
| allow, | ||
| nix_path, | ||
| n_threads=n_threads, | ||
| num_parallel_evals=num_parallel_evals, | ||
| max_memory_size=max_memory_size, | ||
| ) | ||
|
|
||
| filtered_per_system: dict[System, list[str]] = {} | ||
| for system, attrs in attrs_per_system.items(): | ||
| filtered_per_system[system] = [] | ||
| for attr in attrs: | ||
| if not (attr.broken or attr.blacklisted): | ||
| filtered_per_system[system].append(attr.name) | ||
| paths = [] | ||
| for attrs in attrs_per_system.values(): | ||
| paths.extend( | ||
| f"{attr.drv_path}^*" | ||
| for attr in attrs | ||
| if not (attr.broken or attr.blacklisted) | ||
| ) | ||
|
|
||
| if all(len(filtered) == 0 for filtered in filtered_per_system.values()): | ||
| if len(paths) == 0: | ||
| return attrs_per_system | ||
|
|
||
| command = [ | ||
| build_graph, | ||
| "build", | ||
| "--file", | ||
| REVIEW_SHELL, | ||
| "--nix-path", | ||
| nix_path, | ||
| "--extra-experimental-features", | ||
| "nix-command" if allow.url_literals else "nix-command no-url-literals", | ||
|
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. Do we need to remove allow.url_literals now completely? |
||
| "nix-command", | ||
| "--no-link", | ||
| "--keep-going", | ||
| "--allow-import-from-derivation" | ||
| if allow.ifd | ||
| else "--no-allow-import-from-derivation", | ||
| "--stdin", | ||
| ] | ||
|
|
||
| if platform == "linux": | ||
|
|
@@ -355,14 +379,9 @@ def nix_build( | |
| "relaxed", | ||
| ] | ||
|
|
||
| command += build_shell_file_args( | ||
| cache_dir=cache_directory, | ||
| attrs_per_system=filtered_per_system, | ||
| local_system=local_system, | ||
| nixpkgs_config=nixpkgs_config, | ||
| ) + shlex.split(args) | ||
| command += shlex.split(args) | ||
|
|
||
| sh(command) | ||
| sh(command, input="\n".join(str(p) for p in paths)) | ||
| return attrs_per_system | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
I don't think that is necessary anymore