Skip to content

Commit e9fe4fa

Browse files
authored
nixos-rebuild-ng: fix --option parse, make --quiet a count (NixOS#374063)
2 parents 3833178 + 8963922 commit e9fe4fa

File tree

6 files changed

+87
-20
lines changed

6 files changed

+87
-20
lines changed

pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ def get_parser() -> tuple[argparse.ArgumentParser, dict[str, argparse.ArgumentPa
2727
default=0,
2828
help="Enable verbose logging (includes nix)",
2929
)
30+
common_flags.add_argument("--quiet", action="count", default=0)
3031
common_flags.add_argument("--max-jobs", "-j")
3132
common_flags.add_argument("--cores")
3233
common_flags.add_argument("--log-format")
3334
common_flags.add_argument("--keep-going", "-k", action="store_true")
3435
common_flags.add_argument("--keep-failed", "-K", action="store_true")
3536
common_flags.add_argument("--fallback", action="store_true")
3637
common_flags.add_argument("--repair", action="store_true")
37-
common_flags.add_argument("--option", nargs=2)
38+
common_flags.add_argument("--option", nargs=2, action="append")
3839

3940
common_build_flags = argparse.ArgumentParser(add_help=False)
4041
common_build_flags.add_argument("--builders")
4142
common_build_flags.add_argument("--include", "-I", action="append")
42-
common_build_flags.add_argument("--quiet", action="store_true")
4343
common_build_flags.add_argument("--print-build-logs", "-L", action="store_true")
4444
common_build_flags.add_argument("--show-trace", action="store_true")
4545

@@ -54,8 +54,8 @@ def get_parser() -> tuple[argparse.ArgumentParser, dict[str, argparse.ArgumentPa
5454
flake_common_flags.add_argument("--no-write-lock-file", action="store_true")
5555
flake_common_flags.add_argument("--no-registries", action="store_true")
5656
flake_common_flags.add_argument("--commit-lock-file", action="store_true")
57-
flake_common_flags.add_argument("--update-input")
58-
flake_common_flags.add_argument("--override-input", nargs=2)
57+
flake_common_flags.add_argument("--update-input", action="append")
58+
flake_common_flags.add_argument("--override-input", nargs=2, action="append")
5959

6060
classic_build_flags = argparse.ArgumentParser(add_help=False)
6161
classic_build_flags.add_argument("--no-build-output", "-Q", action="store_true")

pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class BuildAttr:
5252
attr: str | None
5353

5454
def to_attr(self, *attrs: str) -> str:
55-
return f"{self.attr + '.' if self.attr else ''}{".".join(attrs)}"
55+
return f"{self.attr + '.' if self.attr else ''}{'.'.join(attrs)}"
5656

5757
@classmethod
5858
def from_arg(cls, attr: str | None, file: str | None) -> Self:
@@ -68,7 +68,7 @@ class Flake:
6868
_re: ClassVar = re.compile(r"^(?P<path>[^\#]*)\#?(?P<attr>[^\#\"]*)$")
6969

7070
def to_attr(self, *attrs: str) -> str:
71-
return f"{self}.{".".join(attrs)}"
71+
return f"{self}.{'.'.join(attrs)}"
7272

7373
@override
7474
def __str__(self) -> str:
@@ -83,7 +83,7 @@ def parse(
8383
m = cls._re.match(flake_str)
8484
assert m is not None, f"got no matches for {flake_str}"
8585
attr = m.group("attr")
86-
nixos_attr = f"nixosConfigurations.{attr or hostname_fn() or "default"}"
86+
nixos_attr = f"nixosConfigurations.{attr or hostname_fn() or 'default'}"
8787
path = m.group("path")
8888
if ":" in path:
8989
return cls(path, nixos_attr)

pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"-o",
1616
"ControlMaster=auto",
1717
"-o",
18-
f"ControlPath={tmpdir.TMPDIR_PATH / "ssh-%n"}",
18+
f"ControlPath={tmpdir.TMPDIR_PATH / 'ssh-%n'}",
1919
"-o",
2020
"ControlPersist=60",
2121
]

pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections.abc import Mapping, Sequence
33
from typing import Any, assert_never, override
44

5-
type Arg = bool | str | list[str] | int | None
5+
type Arg = bool | str | list[str] | list[list[str]] | int | None
66
type Args = dict[str, Arg]
77

88

@@ -43,9 +43,13 @@ def dict_to_flags(d: Args | None) -> list[str]:
4343
flags.append(flag)
4444
flags.append(value)
4545
case list():
46-
for v in value:
46+
for vs in value:
4747
flags.append(flag)
48-
flags.append(v)
48+
if isinstance(vs, list):
49+
for v in vs:
50+
flags.append(v)
51+
else:
52+
flags.append(vs)
4953
case _:
5054
assert_never(value)
5155
return flags

pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,21 @@ def test_parse_args() -> None:
4040
"--flake",
4141
"/etc/nixos",
4242
"--option",
43-
"foo",
44-
"bar",
43+
"foo1",
44+
"bar1",
45+
"--option",
46+
"foo2",
47+
"bar2",
48+
"--override-input",
49+
"override1",
50+
"input1",
51+
"--override-input",
52+
"override2",
53+
"input2",
54+
"--update-input",
55+
"input1",
56+
"--update-input",
57+
"input2",
4558
]
4659
)
4760
assert nr.logger.level == logging.INFO
@@ -50,8 +63,27 @@ def test_parse_args() -> None:
5063
assert r1.install_grub is True
5164
assert r1.profile_name == "system"
5265
assert r1.action == "switch"
53-
assert r1.option == ["foo", "bar"]
54-
assert g1["common_flags"].option == ["foo", "bar"]
66+
# round-trip test (ensure that we have the same flags as parsed)
67+
assert nr.utils.dict_to_flags(vars(g1["common_flags"])) == [
68+
"--option",
69+
"foo1",
70+
"bar1",
71+
"--option",
72+
"foo2",
73+
"bar2",
74+
]
75+
assert nr.utils.dict_to_flags(vars(g1["flake_common_flags"])) == [
76+
"--update-input",
77+
"input1",
78+
"--update-input",
79+
"input2",
80+
"--override-input",
81+
"override1",
82+
"input1",
83+
"--override-input",
84+
"override2",
85+
"input2",
86+
]
5587

5688
r2, g2 = nr.parse_args(
5789
[
@@ -63,7 +95,13 @@ def test_parse_args() -> None:
6395
"foo",
6496
"--attr",
6597
"bar",
98+
"-I",
99+
"include1",
100+
"-I",
101+
"include2",
66102
"-vvv",
103+
"--quiet",
104+
"--quiet",
67105
]
68106
)
69107
assert nr.logger.level == logging.DEBUG
@@ -72,7 +110,18 @@ def test_parse_args() -> None:
72110
assert r2.action == "dry-build"
73111
assert r2.file == "foo"
74112
assert r2.attr == "bar"
75-
assert g2["common_flags"].v == 3
113+
# round-trip test (ensure that we have the same flags as parsed)
114+
assert nr.utils.dict_to_flags(vars(g2["common_flags"])) == [
115+
"-vvv",
116+
"--quiet",
117+
"--quiet",
118+
]
119+
assert nr.utils.dict_to_flags(vars(g2["common_build_flags"])) == [
120+
"--include",
121+
"include1",
122+
"--include",
123+
"include2",
124+
]
76125

77126

78127
@patch.dict(nr.process.os.environ, {}, clear=True)
@@ -292,6 +341,10 @@ def run_side_effect(args: list[str], **kwargs: Any) -> CompletedProcess[str]:
292341
"--sudo",
293342
"--verbose",
294343
"--fast",
344+
# https://github.com/NixOS/nixpkgs/issues/374050
345+
"--option",
346+
"narinfo-cache-negative-ttl",
347+
"1200",
295348
]
296349
)
297350

@@ -307,6 +360,9 @@ def run_side_effect(args: list[str], **kwargs: Any) -> CompletedProcess[str]:
307360
"--print-out-paths",
308361
"/path/to/config#nixosConfigurations.hostname.config.system.build.toplevel",
309362
"-v",
363+
"--option",
364+
"narinfo-cache-negative-ttl",
365+
"1200",
310366
"--no-link",
311367
],
312368
check=True,

pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_utils.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ def test_dict_to_flags() -> None:
1111
"test_flag_2": False,
1212
"test_flag_3": "value",
1313
"test_flag_4": ["v1", "v2"],
14-
"test_flag_5": None,
14+
"test_flag_5": [["o1", "v1"], ["o2", "v2"]],
15+
"test_flag_6": None,
1516
"t": True,
1617
"v": 5,
17-
"verbose": 2,
18+
"quiet": 2,
1819
}
1920
)
2021
assert r1 == [
@@ -25,10 +26,16 @@ def test_dict_to_flags() -> None:
2526
"v1",
2627
"--test-flag-4",
2728
"v2",
29+
"--test-flag-5",
30+
"o1",
31+
"v1",
32+
"--test-flag-5",
33+
"o2",
34+
"v2",
2835
"-t",
2936
"-vvvvv",
30-
"--verbose",
31-
"--verbose",
37+
"--quiet",
38+
"--quiet",
3239
]
3340
r2 = u.dict_to_flags({"verbose": 0, "empty_list": []})
3441
assert r2 == []

0 commit comments

Comments
 (0)