Skip to content

Commit

Permalink
Merge pull request #459 from PerchunPak/build-namespace
Browse files Browse the repository at this point in the history
Add support for building `namespace.*` without eval'ing nixpkgs
  • Loading branch information
GaetanLepage authored Feb 2, 2025
2 parents b3ccb2a + 568a10d commit e0c47a9
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 5 deletions.
9 changes: 5 additions & 4 deletions nixpkgs_review/cli/pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ def pr_command(args: argparse.Namespace) -> str:
if args.token:
args.eval = "github"
else:
warn(
"No GitHub token provided via GITHUB_TOKEN variable. Falling back to local evaluation.\n"
"Tip: Install the `gh` command line tool and run `gh auth login` to authenticate."
)
if not args.package:
warn(
"No GitHub token provided via GITHUB_TOKEN variable. Falling back to local evaluation.\n"
"Tip: Install the `gh` command line tool and run `gh auth login` to authenticate."
)
args.eval = "local"
case "github":
if not args.token:
Expand Down
6 changes: 6 additions & 0 deletions nixpkgs_review/nix/evalAttrs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ let
drvPath = null;
})
]
else if !lib.isDerivation pkg then
if builtins.typeOf pkg != "set" then
# if it is not a package, ignore it (it is probably something like overrideAttrs)
[ ]
else
lib.flatten (lib.mapAttrsToList (name': _: getProperties ("${name}.${name'}")) pkg)
else
lib.flip map pkg.outputs or [ "out" ] (
output:
Expand Down
5 changes: 5 additions & 0 deletions nixpkgs_review/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ def __init__(
self,
attrs_per_system: dict[str, list[Attr]],
extra_nixpkgs_config: str,
only_packages: set[str],
show_header: bool = True,
*,
checkout: Literal["merge", "commit"] = "merge",
) -> None:
self.show_header = show_header
self.attrs = attrs_per_system
self.checkout = checkout
self.only_packages = only_packages

if extra_nixpkgs_config != "{ }":
self.extra_nixpkgs_config: str | None = extra_nixpkgs_config
Expand Down Expand Up @@ -190,6 +192,7 @@ def json(self, pr: int | None) -> str:
"pr": pr,
"checkout": self.checkout,
"extra-nixpkgs-config": self.extra_nixpkgs_config,
"only_packages": list(self.only_packages),
"result": {
system: report.serialize()
for system, report in self.system_reports.items()
Expand All @@ -212,6 +215,8 @@ def markdown(self, pr: int | None) -> str:
cmd += f" --extra-nixpkgs-config '{self.extra_nixpkgs_config}'"
if self.checkout != "merge":
cmd += f" --checkout {self.checkout}"
if self.only_packages:
cmd += " --package " + " --package ".join(self.only_packages)
msg += f"Command: `{cmd}`\n"

for system, report in self.system_reports.items():
Expand Down
26 changes: 25 additions & 1 deletion nixpkgs_review/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def __init__(
self.run = run
self.remote = remote
self.github_client = GithubClient(api_token)
self.use_github_eval = use_github_eval
self.use_github_eval = use_github_eval and not only_packages
self.checkout = checkout
self.only_packages = only_packages
self.package_regex = package_regexes
Expand Down Expand Up @@ -216,6 +216,22 @@ def build_commit(
"""
self.git_worktree(base_commit)

if self.only_packages:
if reviewed_commit is None:
self.apply_unstaged(staged)
elif self.checkout == CheckoutOption.MERGE:
self.git_checkout(reviewed_commit)
else:
self.git_merge(reviewed_commit)

changed_attrs = {}
for system in self.systems:
changed_attrs[system] = set()
for package in self.only_packages:
changed_attrs[system].add(package)

return self.build(changed_attrs, self.build_args)

print("Local evaluation for computing rebuilds")

# TODO: nix-eval-jobs ?
Expand Down Expand Up @@ -330,6 +346,13 @@ def build_pr(self, pr_number: int) -> dict[System, list[Attr]]:
raise NixpkgsReviewError(msg)
base_rev = run.stdout.strip()

if self.only_packages:
packages_per_system = {}
for system in self.systems:
packages_per_system[system] = set()
for package in self.only_packages:
packages_per_system[system].add(package)

if packages_per_system is None:
return self.build_commit(base_rev, pr_rev)

Expand All @@ -356,6 +379,7 @@ def start_review(
attrs_per_system,
self.extra_nixpkgs_config,
checkout=self.checkout.name.lower(), # type: ignore[arg-type]
only_packages=self.only_packages,
show_header=self.show_header,
)
report.print_console(pr)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,34 @@ def test_pr_github_action_eval(
],
)
helpers.assert_built(pkg_name="pkg1", path=path)


@patch("nixpkgs_review.review._list_packages_system")
def test_pr_only_packages_does_not_trigger_an_eval(
mock_eval: MagicMock,
helpers: Helpers,
) -> None:
mock_eval.side_effect = RuntimeError
with helpers.nixpkgs() as nixpkgs:
nixpkgs.path.joinpath("pkg1.txt").write_text("foo")
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", "example-change"], check=True)
subprocess.run(["git", "checkout", "-b", "pull/363128/merge"], check=True)
subprocess.run(
["git", "push", str(nixpkgs.remote), "pull/363128/merge"], check=True
)

path = main(
"nixpkgs-review",
[
"pr",
"--remote",
str(nixpkgs.remote),
"--run",
"exit 0",
"--package",
"pkg1",
"363128",
],
)
helpers.assert_built(pkg_name="pkg1", path=path)
30 changes: 30 additions & 0 deletions tests/test_rev.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import shutil
import subprocess
from unittest.mock import MagicMock, patch

import pytest

Expand Down Expand Up @@ -40,3 +41,32 @@ def test_rev_command_without_nom(helpers: Helpers) -> None:
],
)
helpers.assert_built(pkg_name="pkg1", path=path)


@patch("nixpkgs_review.review._list_packages_system")
def test_rev_only_packages_does_not_trigger_an_eval(
mock_eval: MagicMock,
helpers: Helpers,
) -> None:
mock_eval.side_effect = RuntimeError
with helpers.nixpkgs() as nixpkgs:
nixpkgs.path.joinpath("pkg1.txt").write_text("foo")
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", "example-change"], check=True)

path = main(
"nixpkgs-review",
[
"rev",
"HEAD",
"--remote",
str(nixpkgs.remote),
"--run",
"exit 0",
"--build-graph",
"nix",
"--package",
"pkg1",
],
)
helpers.assert_built(pkg_name="pkg1", path=path)
29 changes: 29 additions & 0 deletions tests/test_wip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import shutil
from unittest.mock import MagicMock, patch

import pytest

Expand Down Expand Up @@ -40,3 +41,31 @@ def test_wip_command_without_nom(
helpers.assert_built(pkg_name="pkg1", path=path)
captured = capfd.readouterr()
assert "$ nix build" in captured.out


@patch("nixpkgs_review.review._list_packages_system")
def test_wip_only_packages_does_not_trigger_an_eval(
mock_eval: MagicMock,
helpers: Helpers,
capfd: pytest.CaptureFixture,
) -> None:
mock_eval.side_effect = RuntimeError
with helpers.nixpkgs() as nixpkgs:
nixpkgs.path.joinpath("pkg1.txt").write_text("foo")
path = main(
"nixpkgs-review",
[
"wip",
"--remote",
str(nixpkgs.remote),
"--run",
"exit 0",
"--build-graph",
"nix",
"--package",
"pkg1",
],
)
helpers.assert_built(pkg_name="pkg1", path=path)
captured = capfd.readouterr()
assert "$ nix build" in captured.out

0 comments on commit e0c47a9

Please sign in to comment.