Skip to content

Commit ffaa811

Browse files
committed
Add --extra-nixpkgs-args
1 parent b201a2c commit ffaa811

File tree

11 files changed

+89
-65
lines changed

11 files changed

+89
-65
lines changed

nixpkgs_review/builddir.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
if TYPE_CHECKING:
1414
import types
1515

16+
from .buildenv import Buildenv
17+
1618

1719
class DisableKeyboardInterrupt:
1820
def __enter__(self) -> None:
@@ -60,7 +62,7 @@ def create_cache_directory(name: str) -> Path | TemporaryDirectory[str]:
6062

6163

6264
class Builddir:
63-
def __init__(self, name: str) -> None:
65+
def __init__(self, name: str, buildenv: Buildenv) -> None:
6466
self.environ = os.environ.copy()
6567
self.directory = create_cache_directory(name)
6668
self._temp_directory: TemporaryDirectory[str] | None = None
@@ -75,6 +77,7 @@ def __init__(self, name: str) -> None:
7577
self.worktree_dir = self.path / "nixpkgs"
7678
self.worktree_dir.mkdir()
7779
self._nix_path_parts = [
80+
f"nixpkgs-wrapper={buildenv.nixpkgs_wrapper}",
7881
f"nixpkgs={self.worktree_dir}",
7982
f"nixpkgs-overlays={self.overlay.path}",
8083
]

nixpkgs_review/buildenv.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
from pathlib import Path
66
from tempfile import NamedTemporaryFile
7-
from typing import TYPE_CHECKING
7+
from typing import TYPE_CHECKING, Self
88

99
from .utils import die
1010

@@ -23,39 +23,51 @@ def find_nixpkgs_root() -> Path | None:
2323

2424

2525
class Buildenv:
26-
def __init__(self, allow_aliases: bool, extra_nixpkgs_config: str) -> None:
26+
def __init__(
27+
self, allow_aliases: bool, extra_nixpkgs_config: str, extra_nixpkgs_args: str
28+
) -> None:
2729
if not (
2830
extra_nixpkgs_config.startswith("{") and extra_nixpkgs_config.endswith("}")
2931
):
3032
msg = "--extra-nixpkgs-config must start with `{` and end with `}`"
3133
raise RuntimeError(msg)
34+
if not (
35+
extra_nixpkgs_args.startswith("{") and extra_nixpkgs_args.endswith("}")
36+
):
37+
msg = "--extra-nixpkgs-args must start with `{` and end with `}`"
38+
raise RuntimeError(msg)
3239

33-
self.nixpkgs_config = NamedTemporaryFile(suffix=".nix") # noqa: SIM115
40+
self._nixpkgs_wrapper_file = NamedTemporaryFile(suffix=".nix") # noqa: SIM115
3441
self.old_cwd: Path | None = None
3542
self.environ: dict[str, str] | None = None
3643
aliases_config = "allowAliases = false;" if not allow_aliases else ""
37-
config_content = f"""{{
38-
allowUnfree = true;
39-
allowBroken = true;
40-
{aliases_config}
41-
checkMeta = true;
42-
## TODO: also build packages marked as insecure
43-
# allowInsecurePredicate = x: true;
44-
}} // {extra_nixpkgs_config}
44+
config_content = f"""{{...}}@args:
45+
let extraArgs = {extra_nixpkgs_args};
46+
in import <nixpkgs> ({{
47+
config = {{
48+
allowUnfree = true;
49+
allowBroken = true;
50+
{aliases_config}
51+
checkMeta = true;
52+
## TODO: also build packages marked as insecure
53+
# allowInsecurePredicate = x: true;
54+
}} // {extra_nixpkgs_config} // extraArgs.config or {{}} // args.config or {{}};
55+
}} // (removeAttrs extraArgs [ "config" ]) // (removeAttrs args [ "config" ]))
4556
"""
46-
self.nixpkgs_config.write(config_content.encode())
47-
self.nixpkgs_config.flush()
57+
self._nixpkgs_wrapper_file.write(config_content.encode())
58+
self._nixpkgs_wrapper_file.flush()
4859

49-
def __enter__(self) -> Path:
60+
def __enter__(self) -> Self:
5061
self.environ = os.environ.copy()
5162
self.old_cwd = Path.cwd()
63+
self.nixpkgs_wrapper = Path(self._nixpkgs_wrapper_file.name)
5264

5365
if (root := find_nixpkgs_root()) is None:
5466
die("Has to be executed from nixpkgs repository")
5567
os.chdir(root)
5668

57-
os.environ["NIXPKGS_CONFIG"] = self.nixpkgs_config.name
58-
return Path(self.nixpkgs_config.name)
69+
os.environ["NIXPKGS_CONFIG"] = ""
70+
return self
5971

6072
def __exit__(
6173
self,
@@ -71,5 +83,5 @@ def __exit__(
7183
os.environ.clear()
7284
os.environ.update(self.environ)
7385

74-
if self.nixpkgs_config is not None:
75-
self.nixpkgs_config.close()
86+
if self._nixpkgs_wrapper_file is not None:
87+
self._nixpkgs_wrapper_file.close()

nixpkgs_review/cli/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ def common_flags() -> list[CommonFlag]:
303303
default="{ }",
304304
help="Extra nixpkgs config to pass to `import <nixpkgs>`",
305305
),
306+
CommonFlag(
307+
"--extra-nixpkgs-args",
308+
type=str,
309+
default="{ }",
310+
help="Extra nixpkgs args to pass to `import <nixpkgs>`",
311+
),
306312
CommonFlag(
307313
"--num-parallel-evals",
308314
type=int,

nixpkgs_review/cli/pr.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ def pr_command(args: argparse.Namespace) -> str:
9494

9595
builddir = None
9696
with (
97-
Buildenv(allow.aliases, args.extra_nixpkgs_config) as nixpkgs_config,
97+
Buildenv(
98+
allow.aliases, args.extra_nixpkgs_config, args.extra_nixpkgs_args
99+
) as buildenv,
98100
ExitStack() as stack,
99101
):
100102
review = None
101103
for pr in prs:
102-
builddir = stack.enter_context(Builddir(f"pr-{pr}"))
104+
builddir = stack.enter_context(Builddir(f"pr-{pr}", buildenv))
103105
try:
104106
review = Review(
105107
builddir=builddir,
@@ -119,8 +121,9 @@ def pr_command(args: argparse.Namespace) -> str:
119121
checkout=checkout_option,
120122
sandbox=args.sandbox,
121123
build_graph=args.build_graph,
122-
nixpkgs_config=nixpkgs_config,
124+
nixpkgs_wrapper=buildenv.nixpkgs_wrapper,
123125
extra_nixpkgs_config=args.extra_nixpkgs_config,
126+
extra_nixpkgs_args=args.extra_nixpkgs_args,
124127
num_parallel_evals=args.num_parallel_evals,
125128
show_header=not args.no_headers,
126129
show_logs=not args.no_logs,

nixpkgs_review/cli/rev.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
def rev_command(args: argparse.Namespace) -> Path:
1616
allow = AllowedFeatures(args.allow)
17-
with Buildenv(allow.aliases, args.extra_nixpkgs_config) as nixpkgs_config:
17+
with Buildenv(
18+
allow.aliases, args.extra_nixpkgs_config, args.extra_nixpkgs_args
19+
) as buildenv:
1820
commit = git.verify_commit_hash(args.commit)
1921
return review_local_revision(
2022
f"rev-{commit}",
2123
args,
2224
allow,
23-
nixpkgs_config,
25+
buildenv,
2426
commit,
2527
print_result=args.print_result,
2628
)

nixpkgs_review/cli/wip.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
def wip_command(args: argparse.Namespace) -> Path:
1616
allow = AllowedFeatures(args.allow)
17-
with Buildenv(allow.aliases, args.extra_nixpkgs_config) as nixpkgs_config:
17+
with Buildenv(
18+
allow.aliases, args.extra_nixpkgs_config, args.extra_nixpkgs_args
19+
) as buildenv:
1820
return review_local_revision(
1921
f"rev-{git.verify_commit_hash('HEAD')}-dirty",
2022
args,
2123
allow,
22-
nixpkgs_config,
24+
buildenv,
2325
None,
2426
staged=args.staged,
2527
print_result=args.print_result,

nixpkgs_review/nix.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def nix_shell(
7979
local_system: str,
8080
build_graph: str,
8181
nix_path: str,
82-
nixpkgs_config: Path,
82+
nixpkgs_wrapper: Path,
8383
nixpkgs_overlay: Path,
8484
run: str | None = None,
8585
*,
@@ -94,15 +94,14 @@ def nix_shell(
9494
cache_dir=cache_directory,
9595
attrs_per_system=attrs_per_system,
9696
local_system=local_system,
97-
nixpkgs_config=nixpkgs_config,
9897
)
9998
if sandbox:
10099
args = _nix_shell_sandbox(
101100
nix_shell,
102101
shell_file_args,
103102
cache_directory,
104103
nix_path,
105-
nixpkgs_config,
104+
nixpkgs_wrapper,
106105
nixpkgs_overlay,
107106
)
108107
else:
@@ -117,7 +116,7 @@ def _nix_shell_sandbox(
117116
shell_file_args: list[str],
118117
cache_directory: Path,
119118
nix_path: str,
120-
nixpkgs_config: Path,
119+
nixpkgs_wrapper: Path,
121120
nixpkgs_overlay: Path,
122121
) -> list[str]:
123122
if platform != "linux":
@@ -177,7 +176,7 @@ def tmpfs(path: Path | str, *, is_dir: bool = True) -> list[str]:
177176
*bind("/dev", dev=True),
178177
*tmpfs("/tmp"), # noqa: S108
179178
# Required for evaluation
180-
*bind(nixpkgs_config),
179+
*bind(nixpkgs_wrapper),
181180
*bind(nixpkgs_overlay),
182181
# /run (also cover sockets for wayland/pulseaudio and pipewires)
183182
*bind(Path("/run/user").joinpath(uid), dev=True, try_=True),
@@ -332,7 +331,6 @@ def nix_build(
332331
allow: AllowedFeatures,
333332
build_graph: str,
334333
nix_path: str,
335-
nixpkgs_config: Path,
336334
n_threads: int,
337335
) -> dict[System, list[Attr]]:
338336
if not attr_names_per_system:
@@ -376,7 +374,6 @@ def nix_build(
376374
cache_dir=cache_directory,
377375
attrs_per_system=filtered_per_system,
378376
local_system=local_system,
379-
nixpkgs_config=nixpkgs_config,
380377
)
381378
_write_review_shell_drv(
382379
cache_directory=cache_directory,
@@ -395,7 +392,6 @@ def build_shell_file_args(
395392
cache_dir: Path,
396393
attrs_per_system: dict[System, list[str]],
397394
local_system: str,
398-
nixpkgs_config: Path,
399395
) -> list[str]:
400396
attrs_file = cache_dir.joinpath("attrs.nix")
401397
with attrs_file.open("w+") as f:
@@ -412,12 +408,6 @@ def build_shell_file_args(
412408
"local-system",
413409
local_system,
414410
"--argstr",
415-
"nixpkgs-path",
416-
str(cache_dir.joinpath("nixpkgs/")),
417-
"--argstr",
418-
"nixpkgs-config-path",
419-
str(nixpkgs_config),
420-
"--argstr",
421411
"attrs-path",
422412
str(attrs_file),
423413
]

nixpkgs_review/nix/evalAttrs.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
with builtins;
44
let
5-
pkgs = import <nixpkgs> {
6-
config = import (getEnv "NIXPKGS_CONFIG") // {
5+
pkgs = import <nixpkgs-wrapper> {
6+
config = {
77
allowBroken = false;
88
};
99
};
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
{
22
local-system,
3-
nixpkgs-config-path,
3+
nixpkgs ? <nixpkgs-wrapper>,
44
# Path to Nix file containing the Nixpkgs config
55
attrs-path,
6-
# Path to Nix file containing a list of attributes to build
7-
nixpkgs-path,
86
# Path to this review's nixpkgs
9-
local-pkgs ? import nixpkgs-path {
7+
local-pkgs ? import <nixpkgs> {
108
system = local-system;
11-
config = import nixpkgs-config-path;
129
},
1310
lib ? local-pkgs.lib,
1411
}:
1512

1613
let
1714

18-
nixpkgs-config = import nixpkgs-config-path;
1915
extractPackagesForSystem =
2016
system: system-attrs:
2117
let
22-
system-pkg = import nixpkgs-path {
18+
system-pkg = import nixpkgs {
2319
inherit system;
24-
config = nixpkgs-config;
2520
};
2621
in
2722
map (attrString: lib.attrByPath (lib.splitString "." attrString) null system-pkg) system-attrs;
@@ -38,11 +33,11 @@ let
3833
}
3934
);
4035
in
41-
(import nixpkgs-path { }).mkShell {
36+
(import nixpkgs { }).mkShell {
4237
name = "review-shell";
4338
preferLocalBuild = true;
4439
allowSubstitutes = false;
4540
dontWrapQtApps = true;
46-
# see test_rev_command_with_pkg_count
47-
packages = if builtins.length attrs > 50 then [ env ] else attrs;
41+
packages = [ env ];
4842
}
43+

nixpkgs_review/report.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ def __init__(
311311
commit: str | None,
312312
attrs_per_system: dict[str, list[Attr]],
313313
extra_nixpkgs_config: str,
314+
extra_nixpkgs_args: str,
314315
only_packages: set[str],
315316
additional_packages: set[str],
316317
package_regex: list[Pattern[str]],
@@ -337,6 +338,9 @@ def __init__(
337338
self.extra_nixpkgs_config = (
338339
extra_nixpkgs_config if extra_nixpkgs_config != "{ }" else None
339340
)
341+
self.extra_nixpkgs_args = (
342+
extra_nixpkgs_args if extra_nixpkgs_args != "{ }" else None
343+
)
340344

341345
reports: dict[System, SystemReport] = {}
342346
for system, attrs in attrs_per_system.items():
@@ -367,6 +371,7 @@ def json(self, pr: int | None) -> str:
367371
"commit": self.commit,
368372
"checkout": self.checkout,
369373
"extra-nixpkgs-config": self.extra_nixpkgs_config,
374+
"extra-nixpkgs-args": self.extra_nixpkgs_args,
370375
"only_packages": list(self.only_packages),
371376
"additional_packages": list(self.additional_packages),
372377
"package_regex": list(self.package_regex),
@@ -387,6 +392,8 @@ def _generate_command_string(self, pr: int | None) -> str:
387392
cmd += f" pr {pr}"
388393
if self.extra_nixpkgs_config:
389394
cmd += f" --extra-nixpkgs-config '{self.extra_nixpkgs_config}'"
395+
if self.extra_nixpkgs_args:
396+
cmd += f" --extra-nixpkgs-args '{self.extra_nixpkgs_args}'"
390397
if self.checkout != "merge":
391398
cmd += f" --checkout {self.checkout}"
392399

0 commit comments

Comments
 (0)