Skip to content

Commit a376797

Browse files
committed
nixos-rebuild-ng: replace usage of kwargs
1 parent 58e948d commit a376797

File tree

6 files changed

+61
-46
lines changed

6 files changed

+61
-46
lines changed

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,27 @@ def parser_warn(msg: str) -> None:
262262
def reexec(
263263
argv: list[str],
264264
args: argparse.Namespace,
265-
build_flags: dict[str, Args],
266-
flake_build_flags: dict[str, Args],
265+
build_flags: Args,
266+
flake_build_flags: Args,
267267
) -> None:
268268
drv = None
269269
attr = "config.system.build.nixos-rebuild"
270270
try:
271271
# Parsing the args here but ignore ask_sudo_password since it is not
272272
# needed and we would end up asking sudo password twice
273273
if flake := Flake.from_arg(args.flake, Remote.from_arg(args.target_host, None)):
274-
drv = nix.build_flake(attr, flake, **flake_build_flags, no_link=True)
274+
drv = nix.build_flake(
275+
attr,
276+
flake,
277+
flake_build_flags | {"no_link": True},
278+
)
275279
else:
276280
build_attr = BuildAttr.from_arg(args.attr, args.file)
277-
drv = nix.build(attr, build_attr, **build_flags, no_out_link=True)
281+
drv = nix.build(
282+
attr,
283+
build_attr,
284+
build_flags | {"no_out_link": True},
285+
)
278286
except CalledProcessError:
279287
logger.warning(
280288
"could not build a newer version of nixos-rebuild, "
@@ -350,7 +358,7 @@ def execute(argv: list[str]) -> None:
350358
flake = Flake.from_arg(args.flake, target_host)
351359

352360
if can_run and not flake:
353-
nixpkgs_path = nix.find_file("nixpkgs", **build_flags)
361+
nixpkgs_path = nix.find_file("nixpkgs", build_flags)
354362
rev = nix.get_nixpkgs_rev(nixpkgs_path)
355363
if nixpkgs_path and rev:
356364
(nixpkgs_path / ".version-suffix").write_text(rev)
@@ -370,7 +378,10 @@ def execute(argv: list[str]) -> None:
370378

371379
dry_run = action == Action.DRY_BUILD
372380
no_link = action in (Action.SWITCH, Action.BOOT)
381+
build_flags |= {"no_out_link": no_link, "dry_run": dry_run}
382+
flake_build_flags |= {"no_link": no_link, "dry_run": dry_run}
373383
rollback = bool(args.rollback)
384+
374385
match action:
375386
case Action.BUILD_VM:
376387
attr = "config.system.build.vm"
@@ -407,9 +418,7 @@ def execute(argv: list[str]) -> None:
407418
path_to_config = nix.build_flake(
408419
attr,
409420
flake,
410-
no_link=no_link,
411-
dry_run=dry_run,
412-
**flake_build_flags,
421+
flake_build_flags=flake_build_flags,
413422
)
414423
case (_, False, Remote(_), None):
415424
path_to_config = nix.build_remote(
@@ -424,9 +433,7 @@ def execute(argv: list[str]) -> None:
424433
path_to_config = nix.build(
425434
attr,
426435
build_attr,
427-
no_out_link=no_link,
428-
dry_run=dry_run,
429-
**build_flags,
436+
build_flags=build_flags,
430437
)
431438
case never:
432439
# should never happen, but mypy is not smart enough to
@@ -442,7 +449,7 @@ def execute(argv: list[str]) -> None:
442449
path_to_config,
443450
to_host=target_host,
444451
from_host=build_host,
445-
**copy_flags,
452+
copy_flags=copy_flags,
446453
)
447454
if action in (Action.SWITCH, Action.BOOT):
448455
nix.set_profile(
@@ -468,7 +475,7 @@ def execute(argv: list[str]) -> None:
468475
f"Done. The virtual machine can be started by running '{vm_path}'"
469476
)
470477
case Action.EDIT:
471-
nix.edit(flake, **flake_build_flags)
478+
nix.edit(flake, flake_build_flags)
472479
case Action.DRY_RUN:
473480
assert False, "DRY_RUN should be a DRY_BUILD alias"
474481
case Action.LIST_GENERATIONS:
@@ -488,9 +495,9 @@ def execute(argv: list[str]) -> None:
488495
print(tabulate(generations, headers=headers))
489496
case Action.REPL:
490497
if flake:
491-
nix.repl_flake("toplevel", flake, **flake_build_flags)
498+
nix.repl_flake("toplevel", flake, flake_build_flags)
492499
else:
493-
nix.repl("system", build_attr, **build_flags)
500+
nix.repl("system", build_attr, build_flags)
494501
case _:
495502
assert_never(action)
496503

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
def build(
3333
attr: str,
3434
build_attr: BuildAttr,
35-
**build_flags: Args,
35+
build_flags: Args | None = None,
3636
) -> Path:
3737
"""Build NixOS attribute using classic Nix.
3838
@@ -52,7 +52,7 @@ def build(
5252
def build_flake(
5353
attr: str,
5454
flake: Flake,
55-
**flake_build_flags: Args,
55+
flake_build_flags: Args | None = None,
5656
) -> Path:
5757
"""Build NixOS attribute using Flakes.
5858
@@ -74,9 +74,9 @@ def build_remote(
7474
attr: str,
7575
build_attr: BuildAttr,
7676
build_host: Remote | None,
77-
build_flags: dict[str, Args] | None = None,
78-
instantiate_flags: dict[str, Args] | None = None,
79-
copy_flags: dict[str, Args] | None = None,
77+
build_flags: Args | None = None,
78+
instantiate_flags: Args | None = None,
79+
copy_flags: Args | None = None,
8080
) -> Path:
8181
# We need to use `--add-root` otherwise Nix will print this warning:
8282
# > warning: you did not specify '--add-root'; the result might be removed
@@ -89,12 +89,12 @@ def build_remote(
8989
build_attr.to_attr(attr),
9090
"--add-root",
9191
tmpdir.TMPDIR_PATH / uuid4().hex,
92-
*dict_to_flags(instantiate_flags or {}),
92+
*dict_to_flags(instantiate_flags),
9393
],
9494
stdout=PIPE,
9595
)
9696
drv = Path(r.stdout.strip()).resolve()
97-
copy_closure(drv, to_host=build_host, from_host=None, **(copy_flags or {}))
97+
copy_closure(drv, to_host=build_host, from_host=None, copy_flags=copy_flags)
9898

9999
# Need a temporary directory in remote to use in `nix-store --add-root`
100100
r = run_wrapper(
@@ -109,7 +109,7 @@ def build_remote(
109109
drv,
110110
"--add-root",
111111
remote_tmpdir / uuid4().hex,
112-
*dict_to_flags(build_flags or {}),
112+
*dict_to_flags(build_flags),
113113
],
114114
remote=build_host,
115115
stdout=PIPE,
@@ -128,9 +128,9 @@ def build_remote_flake(
128128
attr: str,
129129
flake: Flake,
130130
build_host: Remote,
131-
flake_build_flags: dict[str, Args] | None = None,
132-
copy_flags: dict[str, Args] | None = None,
133-
build_flags: dict[str, Args] | None = None,
131+
flake_build_flags: Args | None = None,
132+
copy_flags: Args | None = None,
133+
build_flags: Args | None = None,
134134
) -> Path:
135135
r = run_wrapper(
136136
[
@@ -139,20 +139,20 @@ def build_remote_flake(
139139
"eval",
140140
"--raw",
141141
flake.to_attr(attr, "drvPath"),
142-
*dict_to_flags(flake_build_flags or {}),
142+
*dict_to_flags(flake_build_flags),
143143
],
144144
stdout=PIPE,
145145
)
146146
drv = Path(r.stdout.strip())
147-
copy_closure(drv, to_host=build_host, from_host=None, **(copy_flags or {}))
147+
copy_closure(drv, to_host=build_host, from_host=None, copy_flags=copy_flags)
148148
r = run_wrapper(
149149
[
150150
"nix",
151151
*FLAKE_FLAGS,
152152
"build",
153153
f"{drv}^*",
154154
"--print-out-paths",
155-
*dict_to_flags(build_flags or {}),
155+
*dict_to_flags(build_flags),
156156
],
157157
remote=build_host,
158158
stdout=PIPE,
@@ -164,7 +164,7 @@ def copy_closure(
164164
closure: Path,
165165
to_host: Remote | None,
166166
from_host: Remote | None = None,
167-
**copy_flags: Args,
167+
copy_flags: Args | None = None,
168168
) -> None:
169169
"""Copy a nix closure to or from host to localhost.
170170
@@ -222,7 +222,7 @@ def nix_copy(to_host: Remote, from_host: Remote) -> None:
222222
nix_copy_closure(to_host, to=True)
223223

224224

225-
def edit(flake: Flake | None, **flake_flags: Args) -> None:
225+
def edit(flake: Flake | None, flake_flags: Args | None = None) -> None:
226226
"Try to find and open NixOS configuration file in editor."
227227
if flake:
228228
run_wrapper(
@@ -251,7 +251,7 @@ def edit(flake: Flake | None, **flake_flags: Args) -> None:
251251
raise NRError("cannot find NixOS config file")
252252

253253

254-
def find_file(file: str, **nix_flags: Args) -> Path | None:
254+
def find_file(file: str, nix_flags: Args | None = None) -> Path | None:
255255
"Find classic Nix file location."
256256
r = run_wrapper(
257257
["nix-instantiate", "--find-file", file, *dict_to_flags(nix_flags)],
@@ -421,14 +421,14 @@ def get_generation_info(generation: Generation) -> GenerationJson:
421421
)
422422

423423

424-
def repl(attr: str, build_attr: BuildAttr, **nix_flags: Args) -> None:
424+
def repl(attr: str, build_attr: BuildAttr, nix_flags: Args | None = None) -> None:
425425
run_args = ["nix", "repl", "--file", build_attr.path]
426426
if build_attr.attr:
427427
run_args.append(build_attr.attr)
428428
run_wrapper([*run_args, *dict_to_flags(nix_flags)])
429429

430430

431-
def repl_flake(attr: str, flake: Flake, **flake_flags: Args) -> None:
431+
def repl_flake(attr: str, flake: Flake, flake_flags: Args | None = None) -> None:
432432
expr = Template(
433433
files(__package__).joinpath(FLAKE_REPL_TEMPLATE).read_text()
434434
).substitute(

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

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

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

78

89
class LogFormatter(logging.Formatter):
@@ -19,7 +20,10 @@ def format(self, record: logging.LogRecord) -> str:
1920
return formatter.format(record)
2021

2122

22-
def dict_to_flags(d: Mapping[str, Args]) -> list[str]:
23+
def dict_to_flags(d: Args | None) -> list[str]:
24+
if not d:
25+
return []
26+
2327
flags = []
2428
for key, value in d.items():
2529
flag = f"--{'-'.join(key.split('_'))}"

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ def run_side_effect(args: list[str], **kwargs: Any) -> CompletedProcess[str]:
123123
"<nixpkgs/nixos>",
124124
"--attr",
125125
"config.system.build.toplevel",
126-
"--no-out-link",
127126
"-vvv",
127+
"--no-out-link",
128128
],
129129
check=True,
130130
stdout=PIPE,
@@ -188,8 +188,8 @@ def run_side_effect(args: list[str], **kwargs: Any) -> CompletedProcess[str]:
188188
"build",
189189
"--print-out-paths",
190190
"/path/to/config#nixosConfigurations.hostname.config.system.build.toplevel",
191-
"--no-link",
192191
"-v",
192+
"--no-link",
193193
],
194194
check=True,
195195
stdout=PIPE,
@@ -349,6 +349,7 @@ def run_side_effect(args: list[str], **kwargs: Any) -> CompletedProcess[str]:
349349
"eval",
350350
"--raw",
351351
"/path/to/config#nixosConfigurations.hostname.config.system.build.toplevel.drvPath",
352+
"--no-link",
352353
],
353354
check=True,
354355
stdout=PIPE,
@@ -371,6 +372,7 @@ def run_side_effect(args: list[str], **kwargs: Any) -> CompletedProcess[str]:
371372
"build",
372373
f"'{config_path}^*'",
373374
"--print-out-paths",
375+
"--no-out-link",
374376
],
375377
check=True,
376378
stdout=PIPE,

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
)
2222
def test_build(mock_run: Any, monkeypatch: Any) -> None:
2323
assert n.build(
24-
"config.system.build.attr", m.BuildAttr("<nixpkgs/nixos>", None), nix_flag="foo"
24+
"config.system.build.attr",
25+
m.BuildAttr("<nixpkgs/nixos>", None),
26+
{"nix_flag": "foo"},
2527
) == Path("/path/to/file")
2628
mock_run.assert_called_with(
2729
[
@@ -55,8 +57,7 @@ def test_build_flake(mock_run: Any) -> None:
5557
assert n.build_flake(
5658
"config.system.build.toplevel",
5759
flake,
58-
no_link=True,
59-
nix_flag="foo",
60+
{"no_link": True, "nix_flag": "foo"},
6061
) == Path("/path/to/file")
6162
mock_run.assert_called_with(
6263
[
@@ -237,7 +238,7 @@ def test_copy_closure(monkeypatch: Any) -> None:
237238

238239
monkeypatch.setenv("NIX_SSHOPTS", "--ssh build-opt")
239240
with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run:
240-
n.copy_closure(closure, None, build_host, copy_flag=True)
241+
n.copy_closure(closure, None, build_host, {"copy_flag": True})
241242
mock_run.assert_called_with(
242243
["nix-copy-closure", "--copy-flag", "--from", "[email protected]", closure],
243244
extra_env={
@@ -251,7 +252,7 @@ def test_copy_closure(monkeypatch: Any) -> None:
251252
"NIX_SSHOPTS": " ".join(p.SSH_DEFAULT_OPTS + ["--ssh build-target-opt"])
252253
}
253254
with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run:
254-
n.copy_closure(closure, target_host, build_host, copy_flag=True)
255+
n.copy_closure(closure, target_host, build_host, {"copy_flag": True})
255256
mock_run.assert_called_with(
256257
[
257258
"nix",
@@ -287,7 +288,7 @@ def test_copy_closure(monkeypatch: Any) -> None:
287288
def test_edit(mock_run: Any, monkeypatch: Any, tmpdir: Any) -> None:
288289
# Flake
289290
flake = m.Flake.parse(".#attr")
290-
n.edit(flake, commit_lock_file=True)
291+
n.edit(flake, {"commit_lock_file": True})
291292
mock_run.assert_called_with(
292293
[
293294
"nix",
@@ -472,7 +473,7 @@ def test_list_generations(mock_get_generations: Any, tmp_path: Path) -> None:
472473

473474
@patch(get_qualified_name(n.run_wrapper, n), autospec=True)
474475
def test_repl(mock_run: Any) -> None:
475-
n.repl("attr", m.BuildAttr("<nixpkgs/nixos>", None), nix_flag=True)
476+
n.repl("attr", m.BuildAttr("<nixpkgs/nixos>", None), {"nix_flag": True})
476477
mock_run.assert_called_with(
477478
["nix", "repl", "--file", "<nixpkgs/nixos>", "--nix-flag"]
478479
)
@@ -483,7 +484,7 @@ def test_repl(mock_run: Any) -> None:
483484

484485
@patch(get_qualified_name(n.run_wrapper, n), autospec=True)
485486
def test_repl_flake(mock_run: Any) -> None:
486-
n.repl_flake("attr", m.Flake(Path("flake.nix"), "myAttr"), nix_flag=True)
487+
n.repl_flake("attr", m.Flake(Path("flake.nix"), "myAttr"), {"nix_flag": True})
487488
# See nixos-rebuild-ng.tests.repl for a better test,
488489
# this is mostly for sanity check
489490
assert mock_run.call_count == 1

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
def test_dict_to_flags() -> None:
7+
assert u.dict_to_flags(None) == []
78
r1 = u.dict_to_flags(
89
{
910
"test_flag_1": True,

0 commit comments

Comments
 (0)