diff --git a/nixpkgs_review/builddir.py b/nixpkgs_review/builddir.py index 4bc9d50..010bce6 100644 --- a/nixpkgs_review/builddir.py +++ b/nixpkgs_review/builddir.py @@ -58,12 +58,10 @@ def __init__(self, name: str) -> None: self.worktree_dir = self.path.joinpath("nixpkgs") self.worktree_dir.mkdir() - self.worktree_dir = self.worktree_dir - os.environ["NIX_PATH"] = self.nixpkgs_path() - - def nixpkgs_path(self) -> str: - return f"nixpkgs={self.worktree_dir}:nixpkgs-overlays={self.overlay.path}" + self.nix_path = ( + f"nixpkgs={self.worktree_dir} nixpkgs-overlays={self.overlay.path}" + ) def __enter__(self) -> "Builddir": return self diff --git a/nixpkgs_review/cli/post_result.py b/nixpkgs_review/cli/post_result.py index 15afcbc..5e94f49 100644 --- a/nixpkgs_review/cli/post_result.py +++ b/nixpkgs_review/cli/post_result.py @@ -1,6 +1,5 @@ import argparse import os -import subprocess import sys from pathlib import Path @@ -17,14 +16,7 @@ def post_result_command(args: argparse.Namespace) -> None: sys.exit(1) pr = int(pr_env) - output = subprocess.run( - ["nix-instantiate", "--find-file", "nixpkgs"], - check=True, - stdout=subprocess.PIPE, - text=True, - ).stdout.strip() - nixpkgs_path = Path(output) - report = nixpkgs_path.parent.joinpath("report.md") + report = Path(os.environ["NIXPKGS_REVIEW_ROOT"]) / "report.md" if not report.exists(): warn(f"Report not found in {report}. Are you in a nixpkgs-review nix-shell?") sys.exit(1) diff --git a/nixpkgs_review/nix.py b/nixpkgs_review/nix.py index f69026b..2ee237e 100644 --- a/nixpkgs_review/nix.py +++ b/nixpkgs_review/nix.py @@ -46,6 +46,7 @@ def nix_shell( cache_directory: Path, system: str, build_graph: str, + nix_path: str, nixpkgs_config: Path, run: Optional[str] = None, sandbox: bool = False, @@ -57,15 +58,17 @@ def nix_shell( shell = cache_directory.joinpath("shell.nix") write_shell_expression(shell, attrs, system, nixpkgs_config) if sandbox: - args = _nix_shell_sandbox(nix_shell, shell, nixpkgs_config) + args = _nix_shell_sandbox(nix_shell, shell, nix_path, nixpkgs_config) else: - args = [nix_shell, str(shell)] + args = [nix_shell, str(shell), "--nix-path", nix_path] if run: args.extend(["--run", run]) sh(args, cwd=cache_directory, check=False) -def _nix_shell_sandbox(nix_shell: str, shell: Path, nixpkgs_config: Path) -> List[str]: +def _nix_shell_sandbox( + nix_shell: str, shell: Path, nix_path: str, nixpkgs_config: Path +) -> List[str]: if platform != "linux": raise RuntimeError("Sandbox mode is only available on Linux platforms.") @@ -137,7 +140,7 @@ def tmpfs(path: Union[Path, str], dir: bool = True) -> List[str]: *bind(hub_config, try_=True), *bind(gh_config, try_=True), ] - return [bwrap, *bwrap_args, "--", nix_shell, str(shell)] + return [bwrap, *bwrap_args, "--", nix_shell, str(shell), "--nix-path", nix_path] def _nix_eval_filter(json: Dict[str, Any]) -> List[Attr]: @@ -185,6 +188,7 @@ def nix_eval( attrs: Set[str], system: str, allow: AllowedFeatures, + nix_path: str, ) -> List[Attr]: attr_json = NamedTemporaryFile(mode="w+", delete=False) delete = True @@ -199,6 +203,8 @@ def nix_eval( "--system", system, "eval", + "--nix-path", + nix_path, "--json", "--impure", "--allow-import-from-derivation" @@ -233,13 +239,14 @@ def nix_build( system: str, allow: AllowedFeatures, build_graph: str, + nix_path: str, nixpkgs_config: Path, ) -> List[Attr]: if not attr_names: info("Nothing to be built.") return [] - attrs = nix_eval(attr_names, system, allow) + attrs = nix_eval(attr_names, system, allow, nix_path) filtered = [] for attr in attrs: if not (attr.broken or attr.blacklisted): @@ -254,6 +261,8 @@ def nix_build( command = [ build_graph, "build", + "--nix-path", + nix_path, "--extra-experimental-features", "nix-command" if allow.url_literals else "nix-command no-url-literals", "--no-link", diff --git a/nixpkgs_review/review.py b/nixpkgs_review/review.py index 4c38c24..7fde40f 100644 --- a/nixpkgs_review/review.py +++ b/nixpkgs_review/review.py @@ -151,7 +151,7 @@ def build_commit( self.git_worktree(base_commit) base_packages = list_packages( - str(self.worktree_dir()), + self.builddir.nix_path, self.system, self.allow, ) @@ -162,7 +162,7 @@ def build_commit( self.git_merge(reviewed_commit) merged_packages = list_packages( - str(self.worktree_dir()), + self.builddir.nix_path, self.system, self.allow, check_meta=True, @@ -192,6 +192,7 @@ def build(self, packages: Set[str], args: str) -> List[Attr]: self.skip_packages_regex, self.system, self.allow, + self.builddir.nix_path, ) return nix_build( packages, @@ -200,6 +201,7 @@ def build(self, packages: Set[str], args: str) -> List[Attr]: self.system, self.allow, self.build_graph, + self.builddir.nix_path, self.nixpkgs_config, ) @@ -252,7 +254,7 @@ def start_review( print_result: bool = False, ) -> None: os.environ.pop("NIXPKGS_CONFIG", None) - os.environ["NIX_PATH"] = path.as_posix() + os.environ["NIXPKGS_REVIEW_ROOT"] = str(path) if pr: os.environ["PR"] = str(pr) report = Report(self.system, attr, self.extra_nixpkgs_config) @@ -273,6 +275,7 @@ def start_review( path, self.system, self.build_graph, + self.builddir.nix_path, self.nixpkgs_config, self.run, self.sandbox, @@ -342,7 +345,7 @@ def parse_packages_xml(stdout: IO[str]) -> List[Package]: def list_packages( - path: str, + nix_path: str, system: str, allow: AllowedFeatures, check_meta: bool = False, @@ -355,7 +358,9 @@ def list_packages( "system", system, "-f", - path, + "", + "--nix-path", + nix_path, "-qaP", "--xml", "--out-path", @@ -378,13 +383,14 @@ def package_attrs( package_set: Set[str], system: str, allow: AllowedFeatures, + nix_path: str, ignore_nonexisting: bool = True, ) -> Dict[str, Attr]: attrs: Dict[str, Attr] = {} nonexisting = [] - for attr in nix_eval(package_set, system, allow): + for attr in nix_eval(package_set, system, allow, nix_path): if not attr.exists: nonexisting.append(attr.name) elif not attr.broken: @@ -403,12 +409,14 @@ def join_packages( specified_packages: Set[str], system: str, allow: AllowedFeatures, + nix_path: str, ) -> Set[str]: - changed_attrs = package_attrs(changed_packages, system, allow) + changed_attrs = package_attrs(changed_packages, system, allow, nix_path) specified_attrs = package_attrs( specified_packages, system, allow, + nix_path, ignore_nonexisting=False, ) @@ -439,6 +447,7 @@ def filter_packages( skip_package_regexes: List[Pattern[str]], system: str, allow: AllowedFeatures, + nix_path: str, ) -> Set[str]: packages: Set[str] = set() @@ -456,6 +465,7 @@ def filter_packages( specified_packages, system, allow, + nix_path, ) for attr in changed_packages: diff --git a/tests/test_github_actions.py b/tests/test_github_actions.py index baa68a6..7a33d8b 100644 --- a/tests/test_github_actions.py +++ b/tests/test_github_actions.py @@ -9,12 +9,14 @@ @patch("urllib.request.urlopen") def test_post_result(mock_urlopen: MagicMock, helpers: Helpers) -> None: with helpers.nixpkgs() as nixpkgs: + root = nixpkgs.path.parent + os.environ["PR"] = "1" - os.environ["NIX_PATH"] = f"nixpkgs={nixpkgs.path}" os.environ["GITHUB_TOKEN"] = "foo" + os.environ["NIXPKGS_REVIEW_ROOT"] = str(root) mock_urlopen.side_effect = [mock_open(read_data="{}")()] - report = nixpkgs.path.joinpath("..", "report.md") + report = root / "report.md" with open(report, "w") as f: f.write("")