[Backport release-25.05] nixos-rebuild-ng: silence reexec messages (#416248)

This commit is contained in:
Thiago Kenji Okada
2025-06-12 23:33:42 +01:00
committed by GitHub
4 changed files with 50 additions and 11 deletions

View File

@@ -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"})

View File

@@ -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())

View File

@@ -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(

View File

@@ -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,
)