Unverified Commit 028893d7 authored by Thiago Kenji Okada's avatar Thiago Kenji Okada Committed by GitHub
Browse files

nixos-rebuild-ng: run upgrade_channels with sudo (#424802)

parents 5a87a15e 0cbdae41
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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
+12 −2
Original line number Diff line number Diff line
@@ -693,16 +693,26 @@ 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
    that has a `.update-on-nixos-rebuild` file) or all.
    """
    if not sudo and os.geteuid() != 0:
        raise NixOSRebuildError(
            "if you pass the '--upgrade' or '--upgrade-all' flag, you must "
            "also pass '--sudo' or run the command as root (e.g., with sudo)"
        )

    for channel_path in Path("/nix/var/nix/profiles/per-user/root/channels/").glob("*"):
        if channel_path.is_dir() and (
            all_channels
            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,
            )
+26 −9
Original line number Diff line number Diff line
@@ -836,17 +836,34 @@ 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)
@patch("os.geteuid", autospec=True, return_value=1000)
@patch(get_qualified_name(n.run_wrapper, n), autospec=True)
def test_upgrade_channels(
    mock_run: Mock,
    mock_geteuid: Mock,
    mock_is_dir: Mock,
    mock_glob: Mock,
) -> None:
    with pytest.raises(m.NixOSRebuildError) as e:
        n.upgrade_channels(all_channels=False, sudo=False)
    assert str(e.value) == (
        "error: if you pass the '--upgrade' or '--upgrade-all' flag, you must "
        "also pass '--sudo' or run the command as root (e.g., with sudo)"
    )

    with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run:
        n.upgrade_channels(True)
    n.upgrade_channels(all_channels=False, sudo=True)
    mock_run.assert_called_once_with(
        ["nix-channel", "--update", "nixos"], check=False, sudo=True
    )

    mock_geteuid.return_value = 0
    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),
        ]
    )