nixos-rebuild-ng: set process.run_wrapper check=True by default

This commit is contained in:
Thiago Kenji Okada 2024-11-26 22:08:27 +00:00
parent 3a080abf13
commit 3ef018f5e3
4 changed files with 10 additions and 30 deletions

View File

@ -74,7 +74,6 @@ class Flake:
try: try:
return run_wrapper( return run_wrapper(
["uname", "-n"], ["uname", "-n"],
check=True,
capture_output=True, capture_output=True,
remote=target_host, remote=target_host,
).stdout.strip() ).stdout.strip()

View File

@ -36,7 +36,6 @@ def copy_closure(
host.host, host.host,
closure, closure,
], ],
check=True,
extra_env={"NIX_SSHOPTS": " ".join(host.opts)}, extra_env={"NIX_SSHOPTS": " ".join(host.opts)},
) )
@ -171,7 +170,6 @@ def get_generations(
r = run_wrapper( r = run_wrapper(
["nix-env", "-p", profile.path, "--list-generations"], ["nix-env", "-p", profile.path, "--list-generations"],
stdout=PIPE, stdout=PIPE,
check=True,
remote=target_host, remote=target_host,
sudo=sudo, sudo=sudo,
) )
@ -216,7 +214,6 @@ def list_generations(profile: Profile) -> list[GenerationJson]:
configuration_revision = run_wrapper( configuration_revision = run_wrapper(
[generation_path / "sw/bin/nixos-version", "--configuration-revision"], [generation_path / "sw/bin/nixos-version", "--configuration-revision"],
capture_output=True, capture_output=True,
check=True,
).stdout.strip() ).stdout.strip()
except (CalledProcessError, IOError): except (CalledProcessError, IOError):
configuration_revision = "Unknown" configuration_revision = "Unknown"
@ -260,7 +257,7 @@ def nixos_build(
else: else:
run_args = ["nix-build", "<nixpkgs/nixos>", "--attr", attr] run_args = ["nix-build", "<nixpkgs/nixos>", "--attr", attr]
run_args += dict_to_flags(nix_flags) run_args += dict_to_flags(nix_flags)
r = run_wrapper(run_args, check=True, stdout=PIPE) r = run_wrapper(run_args, stdout=PIPE)
return Path(r.stdout.strip()) return Path(r.stdout.strip())
@ -281,7 +278,7 @@ def nixos_build_flake(
f"{flake}.config.system.build.{attr}", f"{flake}.config.system.build.{attr}",
*dict_to_flags(flake_flags), *dict_to_flags(flake_flags),
] ]
r = run_wrapper(run_args, check=True, stdout=PIPE) r = run_wrapper(run_args, stdout=PIPE)
return Path(r.stdout.strip()) return Path(r.stdout.strip())
@ -289,7 +286,6 @@ def rollback(profile: Profile, target_host: Remote | None, sudo: bool) -> Path:
"Rollback Nix profile, like one created by `nixos-rebuild switch`." "Rollback Nix profile, like one created by `nixos-rebuild switch`."
run_wrapper( run_wrapper(
["nix-env", "--rollback", "-p", profile.path], ["nix-env", "--rollback", "-p", profile.path],
check=True,
remote=target_host, remote=target_host,
sudo=sudo, sudo=sudo,
) )
@ -329,7 +325,6 @@ def set_profile(
"Set a path as the current active Nix profile." "Set a path as the current active Nix profile."
run_wrapper( run_wrapper(
["nix-env", "-p", profile.path, "--set", path_to_config], ["nix-env", "-p", profile.path, "--set", path_to_config],
check=True,
remote=target_host, remote=target_host,
sudo=sudo, sudo=sudo,
) )
@ -361,7 +356,6 @@ def switch_to_configuration(
run_wrapper( run_wrapper(
[path_to_config / "bin/switch-to-configuration", str(action)], [path_to_config / "bin/switch-to-configuration", str(action)],
extra_env={"NIXOS_INSTALL_BOOTLOADER": "1" if install_bootloader else "0"}, extra_env={"NIXOS_INSTALL_BOOTLOADER": "1" if install_bootloader else "0"},
check=True,
remote=target_host, remote=target_host,
sudo=sudo, sudo=sudo,
) )

View File

@ -8,13 +8,6 @@ from pathlib import Path
from typing import Self, Sequence, TypedDict, Unpack from typing import Self, Sequence, TypedDict, Unpack
# Not exhaustive, but we can always extend it later.
class RunKwargs(TypedDict, total=False):
capture_output: bool
stderr: int | None
stdout: int | None
@dataclass(frozen=True) @dataclass(frozen=True)
class Remote: class Remote:
host: str host: str
@ -46,6 +39,13 @@ class Remote:
return None return None
# Not exhaustive, but we can always extend it later.
class RunKwargs(TypedDict, total=False):
capture_output: bool
stderr: int | None
stdout: int | None
def cleanup_ssh(tmp_dir: Path) -> None: def cleanup_ssh(tmp_dir: Path) -> None:
"Close SSH ControlMaster connection." "Close SSH ControlMaster connection."
for ctrl in tmp_dir.glob("ssh-*"): for ctrl in tmp_dir.glob("ssh-*"):
@ -55,7 +55,7 @@ def cleanup_ssh(tmp_dir: Path) -> None:
def run_wrapper( def run_wrapper(
args: Sequence[str | bytes | os.PathLike[str] | os.PathLike[bytes]], args: Sequence[str | bytes | os.PathLike[str] | os.PathLike[bytes]],
*, *,
check: bool, # make it explicit so we always know if the code is handling errors check: bool = True,
extra_env: dict[str, str] | None = None, extra_env: dict[str, str] | None = None,
remote: Remote | None = None, remote: Remote | None = None,
sudo: bool = False, sudo: bool = False,

View File

@ -22,7 +22,6 @@ def test_copy_closure(mock_run: Any) -> None:
n.copy_closure(closure, target_host) n.copy_closure(closure, target_host)
mock_run.assert_called_with( mock_run.assert_called_with(
["nix-copy-closure", "--to", "user@host", closure], ["nix-copy-closure", "--to", "user@host", closure],
check=True,
extra_env={"NIX_SSHOPTS": "--ssh opt"}, extra_env={"NIX_SSHOPTS": "--ssh opt"},
) )
@ -220,7 +219,6 @@ def test_nixos_build_flake(mock_run: Any) -> None:
"--nix-flag", "--nix-flag",
"foo", "foo",
], ],
check=True,
stdout=PIPE, stdout=PIPE,
) )
@ -234,28 +232,24 @@ def test_nixos_build(mock_run: Any, monkeypatch: Any) -> None:
assert n.nixos_build("attr", None, None, nix_flag="foo") == Path("/path/to/file") assert n.nixos_build("attr", None, None, nix_flag="foo") == Path("/path/to/file")
mock_run.assert_called_with( mock_run.assert_called_with(
["nix-build", "<nixpkgs/nixos>", "--attr", "attr", "--nix-flag", "foo"], ["nix-build", "<nixpkgs/nixos>", "--attr", "attr", "--nix-flag", "foo"],
check=True,
stdout=PIPE, stdout=PIPE,
) )
n.nixos_build("attr", "preAttr", "file") n.nixos_build("attr", "preAttr", "file")
mock_run.assert_called_with( mock_run.assert_called_with(
["nix-build", "file", "--attr", "preAttr.attr"], ["nix-build", "file", "--attr", "preAttr.attr"],
check=True,
stdout=PIPE, stdout=PIPE,
) )
n.nixos_build("attr", None, "file", no_out_link=True) n.nixos_build("attr", None, "file", no_out_link=True)
mock_run.assert_called_with( mock_run.assert_called_with(
["nix-build", "file", "--attr", "attr", "--no-out-link"], ["nix-build", "file", "--attr", "attr", "--no-out-link"],
check=True,
stdout=PIPE, stdout=PIPE,
) )
n.nixos_build("attr", "preAttr", None, no_out_link=False, keep_going=True) n.nixos_build("attr", "preAttr", None, no_out_link=False, keep_going=True)
mock_run.assert_called_with( mock_run.assert_called_with(
["nix-build", "default.nix", "--attr", "preAttr.attr", "--keep-going"], ["nix-build", "default.nix", "--attr", "preAttr.attr", "--keep-going"],
check=True,
stdout=PIPE, stdout=PIPE,
) )
@ -270,7 +264,6 @@ def test_rollback(mock_run: Any, tmp_path: Path) -> None:
assert n.rollback(profile, None, False) == profile.path assert n.rollback(profile, None, False) == profile.path
mock_run.assert_called_with( mock_run.assert_called_with(
["nix-env", "--rollback", "-p", path], ["nix-env", "--rollback", "-p", path],
check=True,
remote=None, remote=None,
sudo=False, sudo=False,
) )
@ -279,7 +272,6 @@ def test_rollback(mock_run: Any, tmp_path: Path) -> None:
assert n.rollback(profile, target_host, True) == profile.path assert n.rollback(profile, target_host, True) == profile.path
mock_run.assert_called_with( mock_run.assert_called_with(
["nix-env", "--rollback", "-p", path], ["nix-env", "--rollback", "-p", path],
check=True,
remote=target_host, remote=target_host,
sudo=True, sudo=True,
) )
@ -312,7 +304,6 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None:
"--list-generations", "--list-generations",
], ],
stdout=PIPE, stdout=PIPE,
check=True,
remote=None, remote=None,
sudo=False, sudo=False,
) )
@ -330,7 +321,6 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None:
"--list-generations", "--list-generations",
], ],
stdout=PIPE, stdout=PIPE,
check=True,
remote=target_host, remote=target_host,
sudo=True, sudo=True,
) )
@ -353,7 +343,6 @@ def test_set_profile(mock_run: Any) -> None:
mock_run.assert_called_with( mock_run.assert_called_with(
["nix-env", "-p", profile_path, "--set", config_path], ["nix-env", "-p", profile_path, "--set", config_path],
check=True,
remote=None, remote=None,
sudo=False, sudo=False,
) )
@ -378,7 +367,6 @@ def test_switch_to_configuration(mock_run: Any, monkeypatch: Any) -> None:
mock_run.assert_called_with( mock_run.assert_called_with(
[profile_path / "bin/switch-to-configuration", "switch"], [profile_path / "bin/switch-to-configuration", "switch"],
extra_env={"NIXOS_INSTALL_BOOTLOADER": "0"}, extra_env={"NIXOS_INSTALL_BOOTLOADER": "0"},
check=True,
sudo=False, sudo=False,
remote=None, remote=None,
) )
@ -416,7 +404,6 @@ def test_switch_to_configuration(mock_run: Any, monkeypatch: Any) -> None:
"test", "test",
], ],
extra_env={"NIXOS_INSTALL_BOOTLOADER": "1"}, extra_env={"NIXOS_INSTALL_BOOTLOADER": "1"},
check=True,
sudo=True, sudo=True,
remote=target_host, remote=target_host,
) )