nixos-rebuild-ng: run upgrade_channels with sudo

Fix #424749.
This commit is contained in:
Thiago Kenji Okada
2025-07-13 12:11:22 +01:00
parent e941e71095
commit d75ba2e8f0
3 changed files with 17 additions and 9 deletions
@@ -281,7 +281,7 @@ def execute(argv: list[str]) -> None:
copy_flags = common_flags | vars(args_groups["copy_flags"])
if args.upgrade or args.upgrade_all:
nix.upgrade_channels(bool(args.upgrade_all))
nix.upgrade_channels(args.upgrade_all, args.sudo)
action = Action(args.action)
# Only run shell scripts from the Nixpkgs tree if the action is
@@ -693,7 +693,7 @@ def switch_to_configuration(
)
def upgrade_channels(all_channels: bool = False) -> None:
def upgrade_channels(all_channels: bool = False, sudo: bool = False) -> None:
"""Upgrade channels for classic Nix.
It will either upgrade just the `nixos` channel (including any channel
@@ -705,4 +705,8 @@ def upgrade_channels(all_channels: bool = False) -> None:
or channel_path.name == "nixos"
or (channel_path / ".update-on-nixos-rebuild").exists()
):
run_wrapper(["nix-channel", "--update", channel_path.name], check=False)
run_wrapper(
["nix-channel", "--update", channel_path.name],
check=False,
sudo=sudo,
)
@@ -838,15 +838,19 @@ def test_switch_to_configuration_with_systemd_run(
@patch("pathlib.Path.is_dir", autospec=True, return_value=True)
def test_upgrade_channels(mock_is_dir: Mock, mock_glob: Mock) -> None:
with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run:
n.upgrade_channels(False)
mock_run.assert_called_once_with(["nix-channel", "--update", "nixos"], check=False)
n.upgrade_channels(all_channels=False, sudo=True)
mock_run.assert_called_once_with(
["nix-channel", "--update", "nixos"], check=False, sudo=True
)
with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run:
n.upgrade_channels(True)
n.upgrade_channels(all_channels=True, sudo=False)
mock_run.assert_has_calls(
[
call(["nix-channel", "--update", "nixos"], check=False),
call(["nix-channel", "--update", "nixos-hardware"], check=False),
call(["nix-channel", "--update", "home-manager"], check=False),
call(["nix-channel", "--update", "nixos"], check=False, sudo=False),
call(
["nix-channel", "--update", "nixos-hardware"], check=False, sudo=False
),
call(["nix-channel", "--update", "home-manager"], check=False, sudo=False),
]
)