Skip to content

Commit 0465e79

Browse files
committed
Add --extra-nixpkgs-args
1 parent 0c04828 commit 0465e79

File tree

11 files changed

+89
-64
lines changed

11 files changed

+89
-64
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+
}} // extraArgs // args)
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
@@ -297,6 +297,12 @@ def common_flags() -> list[CommonFlag]:
297297
default="{ }",
298298
help="Extra nixpkgs config to pass to `import <nixpkgs>`",
299299
),
300+
CommonFlag(
301+
"--extra-nixpkgs-args",
302+
type=str,
303+
default="{ }",
304+
help="Extra nixpkgs args to pass to `import <nixpkgs>`",
305+
),
300306
CommonFlag(
301307
"--num-parallel-evals",
302308
type=int,

nixpkgs_review/cli/pr.py

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

9393
builddir = None
9494
with (
95-
Buildenv(allow.aliases, args.extra_nixpkgs_config) as nixpkgs_config,
95+
Buildenv(
96+
allow.aliases, args.extra_nixpkgs_config, args.extra_nixpkgs_args
97+
) as buildenv,
9698
ExitStack() as stack,
9799
):
98100
review = None
99101
for pr in prs:
100-
builddir = stack.enter_context(Builddir(f"pr-{pr}"))
102+
builddir = stack.enter_context(Builddir(f"pr-{pr}", buildenv))
101103
try:
102104
review = Review(
103105
builddir=builddir,
@@ -117,8 +119,9 @@ def pr_command(args: argparse.Namespace) -> str:
117119
checkout=checkout_option,
118120
sandbox=args.sandbox,
119121
build_graph=args.build_graph,
120-
nixpkgs_config=nixpkgs_config,
122+
nixpkgs_wrapper=buildenv.nixpkgs_wrapper,
121123
extra_nixpkgs_config=args.extra_nixpkgs_config,
124+
extra_nixpkgs_args=args.extra_nixpkgs_args,
122125
num_parallel_evals=args.num_parallel_evals,
123126
show_header=not args.no_headers,
124127
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
@@ -67,7 +67,7 @@ def nix_shell(
6767
local_system: str,
6868
build_graph: str,
6969
nix_path: str,
70-
nixpkgs_config: Path,
70+
nixpkgs_wrapper: Path,
7171
nixpkgs_overlay: Path,
7272
run: str | None = None,
7373
*,
@@ -82,15 +82,14 @@ def nix_shell(
8282
cache_dir=cache_directory,
8383
attrs_per_system=attrs_per_system,
8484
local_system=local_system,
85-
nixpkgs_config=nixpkgs_config,
8685
)
8786
if sandbox:
8887
args = _nix_shell_sandbox(
8988
nix_shell,
9089
shell_file_args,
9190
cache_directory,
9291
nix_path,
93-
nixpkgs_config,
92+
nixpkgs_wrapper,
9493
nixpkgs_overlay,
9594
)
9695
else:
@@ -105,7 +104,7 @@ def _nix_shell_sandbox(
105104
shell_file_args: list[str],
106105
cache_directory: Path,
107106
nix_path: str,
108-
nixpkgs_config: Path,
107+
nixpkgs_wrapper: Path,
109108
nixpkgs_overlay: Path,
110109
) -> list[str]:
111110
if platform != "linux":
@@ -165,7 +164,7 @@ def tmpfs(path: Path | str, *, is_dir: bool = True) -> list[str]:
165164
*bind("/dev", dev=True),
166165
*tmpfs("/tmp"), # noqa: S108
167166
# Required for evaluation
168-
*bind(nixpkgs_config),
167+
*bind(nixpkgs_wrapper),
169168
*bind(nixpkgs_overlay),
170169
# /run (also cover sockets for wayland/pulseaudio and pipewires)
171170
*bind(Path("/run/user").joinpath(uid), dev=True, try_=True),
@@ -326,7 +325,6 @@ def nix_build(
326325
allow: AllowedFeatures,
327326
build_graph: str,
328327
nix_path: str,
329-
nixpkgs_config: Path,
330328
n_threads: int,
331329
) -> dict[System, list[Attr]]:
332330
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
) + shlex.split(args)
381378

382379
sh(command)
@@ -387,7 +384,6 @@ def build_shell_file_args(
387384
cache_dir: Path,
388385
attrs_per_system: dict[System, list[str]],
389386
local_system: str,
390-
nixpkgs_config: Path,
391387
) -> list[str]:
392388
attrs_file = cache_dir.joinpath("attrs.nix")
393389
with attrs_file.open("w+") as f:
@@ -404,12 +400,6 @@ def build_shell_file_args(
404400
"local-system",
405401
local_system,
406402
"--argstr",
407-
"nixpkgs-path",
408-
str(cache_dir.joinpath("nixpkgs/")),
409-
"--argstr",
410-
"nixpkgs-config-path",
411-
str(nixpkgs_config),
412-
"--argstr",
413403
"attrs-path",
414404
str(attrs_file),
415405
]

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 & 10 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,10 +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-
packages = if builtins.length attrs > 50 then [ env ] else attrs;
41+
packages = [ env ];
4742
}
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)