From 4935f5f09bfecfc9d8fab1aeb1a2a9b2ca9487ed Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 11 Jun 2025 22:47:27 +0100 Subject: [PATCH] nixos-rebuild-ng: silence reexec messages Right now reexec is really verbose when it fails: let's say the configuration has a eval error, you will see the evaluation error twice, once in during the reexec and another one when the configuration is evaluated. This commit changes this behavior so the evaluation done during re-exec process is silent. It is still possible to check the error with `--debug` flag. --- .../src/nixos_rebuild/__init__.py | 18 ++++++++----- .../nixos-rebuild-ng/src/nixos_rebuild/nix.py | 6 +++-- .../nixos-rebuild-ng/src/tests/test_main.py | 11 +++++++- .../ni/nixos-rebuild-ng/src/tests/test_nix.py | 26 ++++++++++++++++++- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/__init__.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/__init__.py index fd03e5b69ef4..78b961b03e16 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/__init__.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/__init__.py @@ -13,7 +13,9 @@ from .models import Action, BuildAttr, Flake, ImageVariants, NRError, Profile from .process import Remote, cleanup_ssh from .utils import Args, LogFormatter, tabulate -logger: Final = logging.getLogger() +NIXOS_REBUILD_ATTR: Final = "config.system.build.nixos-rebuild" + +logger: Final = logging.getLogger(__name__) logger.setLevel(logging.INFO) @@ -276,26 +278,28 @@ def reexec( flake_build_flags: Args, ) -> None: drv = None - attr = "config.system.build.nixos-rebuild" try: # Parsing the args here but ignore ask_sudo_password since it is not # needed and we would end up asking sudo password twice if flake := Flake.from_arg(args.flake, Remote.from_arg(args.target_host, None)): drv = nix.build_flake( - attr, + NIXOS_REBUILD_ATTR, flake, flake_build_flags | {"no_link": True}, + quiet=True, ) else: build_attr = BuildAttr.from_arg(args.attr, args.file) drv = nix.build( - attr, + NIXOS_REBUILD_ATTR, build_attr, build_flags | {"no_out_link": True}, + quiet=True, ) except CalledProcessError: logger.warning( - "could not build a newer version of nixos-rebuild, using current version" + "could not build a newer version of nixos-rebuild, using current version", + exc_info=logger.isEnabledFor(logging.DEBUG), ) if drv: @@ -319,9 +323,9 @@ def reexec( # - Exec format error (e.g.: another OS/CPU arch) logger.warning( "could not re-exec in a newer version of nixos-rebuild, " - + "using current version" + + "using current version", + exc_info=logger.isEnabledFor(logging.DEBUG), ) - logger.debug("re-exec exception", exc_info=True) # We already run clean-up, let's re-exec in the current version # to avoid issues os.execve(current, argv, os.environ | {"_NIXOS_REBUILD_REEXEC": "1"}) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py index 3be610b5ddbd..dc95b914b360 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py @@ -51,6 +51,7 @@ def build( attr: str, build_attr: BuildAttr, build_flags: Args | None = None, + quiet: bool = False, ) -> Path: """Build NixOS attribute using classic Nix. @@ -63,7 +64,7 @@ def build( build_attr.to_attr(attr), *dict_to_flags(build_flags), ] - r = run_wrapper(run_args, stdout=PIPE) + r = run_wrapper(run_args, stdout=PIPE, stderr=PIPE if quiet else None) return Path(r.stdout.strip()) @@ -71,6 +72,7 @@ def build_flake( attr: str, flake: Flake, flake_build_flags: Args | None = None, + quiet: bool = False, ) -> Path: """Build NixOS attribute using Flakes. @@ -84,7 +86,7 @@ def build_flake( flake.to_attr(attr), *dict_to_flags(flake_build_flags), ] - r = run_wrapper(run_args, stdout=PIPE) + r = run_wrapper(run_args, stdout=PIPE, stderr=PIPE if quiet else None) return Path(r.stdout.strip()) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py index 59270b73ec38..22b7054000a8 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py @@ -140,9 +140,10 @@ def test_reexec(mock_build: Mock, mock_execve: Mock, monkeypatch: MonkeyPatch) - mock_build.assert_has_calls( [ call( - "config.system.build.nixos-rebuild", + nr.NIXOS_REBUILD_ATTR, nr.models.BuildAttr(ANY, ANY), {"build": True, "no_out_link": True}, + quiet=True, ) ] ) @@ -187,6 +188,7 @@ def test_reexec_flake( "config.system.build.nixos-rebuild", nr.models.Flake(ANY, ANY), {"flake": True, "no_link": True}, + quiet=True, ) # do not exec if there is no new version mock_execve.assert_not_called() @@ -266,6 +268,7 @@ def test_execute_nix_boot(mock_run: Mock, tmp_path: Path) -> None: ], check=True, stdout=PIPE, + stderr=None, **DEFAULT_RUN_KWARGS, ), call( @@ -340,6 +343,7 @@ def test_execute_nix_build_vm(mock_run: Mock, tmp_path: Path) -> None: ], check=True, stdout=PIPE, + stderr=None, **DEFAULT_RUN_KWARGS, ) ] @@ -404,6 +408,7 @@ def test_execute_nix_build_image_flake(mock_run: Mock, tmp_path: Path) -> None: ], check=True, stdout=PIPE, + stderr=None, **DEFAULT_RUN_KWARGS, ), call( @@ -471,6 +476,7 @@ def test_execute_nix_switch_flake(mock_run: Mock, tmp_path: Path) -> None: ], check=True, stdout=PIPE, + stderr=None, **DEFAULT_RUN_KWARGS, ), call( @@ -761,6 +767,7 @@ def test_execute_nix_switch_flake_target_host( ], check=True, stdout=PIPE, + stderr=None, **DEFAULT_RUN_KWARGS, ), call( @@ -1028,6 +1035,7 @@ def test_execute_build(mock_run: Mock, tmp_path: Path) -> None: ], check=True, stdout=PIPE, + stderr=None, **DEFAULT_RUN_KWARGS, ) ] @@ -1067,6 +1075,7 @@ def test_execute_test_flake(mock_run: Mock, tmp_path: Path) -> None: ], check=True, stdout=PIPE, + stderr=None, **DEFAULT_RUN_KWARGS, ), call( diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py index d87b6b62dca8..195f929aaf53 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py @@ -36,14 +36,18 @@ def test_build(mock_run: Mock) -> None: "foo", ], stdout=PIPE, + stderr=None, ) assert n.build( - "config.system.build.attr", m.BuildAttr(Path("file"), "preAttr") + "config.system.build.attr", + m.BuildAttr(Path("file"), "preAttr"), + quiet=True, ) == Path("/path/to/file") mock_run.assert_called_with( ["nix-build", Path("file"), "--attr", "preAttr.config.system.build.attr"], stdout=PIPE, + stderr=PIPE, ) @@ -74,6 +78,26 @@ def test_build_flake(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> "foo", ], stdout=PIPE, + stderr=None, + ) + + assert n.build_flake( + "config.system.build.toplevel", + flake, + None, + quiet=True, + ) == Path("/path/to/file") + mock_run.assert_called_with( + [ + "nix", + "--extra-experimental-features", + "nix-command flakes", + "build", + "--print-out-paths", + '.#nixosConfigurations."hostname".config.system.build.toplevel', + ], + stdout=PIPE, + stderr=PIPE, )