From d75ba2e8f04a0f80bb9a5eb344235b1a25a3b4e9 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sun, 13 Jul 2025 12:07:57 +0100 Subject: [PATCH] nixos-rebuild-ng: run upgrade_channels with sudo Fix #424749. --- .../src/nixos_rebuild/__init__.py | 2 +- .../ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py | 8 ++++++-- .../ni/nixos-rebuild-ng/src/tests/test_nix.py | 16 ++++++++++------ 3 files changed, 17 insertions(+), 9 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 4208fb3dc1d5..59ba3fd340da 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 @@ -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 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 3cee183c96b1..ed989e1b4e24 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 @@ -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, + ) 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 601eea1da212..8078cd2b36c0 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 @@ -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), ] )