Skip to content

Commit 028893d

Browse files
authored
nixos-rebuild-ng: run upgrade_channels with sudo (#424802)
2 parents 5a87a15 + 0cbdae4 commit 028893d

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def execute(argv: list[str]) -> None:
281281
copy_flags = common_flags | vars(args_groups["copy_flags"])
282282

283283
if args.upgrade or args.upgrade_all:
284-
nix.upgrade_channels(bool(args.upgrade_all))
284+
nix.upgrade_channels(args.upgrade_all, args.sudo)
285285

286286
action = Action(args.action)
287287
# Only run shell scripts from the Nixpkgs tree if the action is

pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,16 +693,26 @@ def switch_to_configuration(
693693
)
694694

695695

696-
def upgrade_channels(all_channels: bool = False) -> None:
696+
def upgrade_channels(all_channels: bool = False, sudo: bool = False) -> None:
697697
"""Upgrade channels for classic Nix.
698698
699699
It will either upgrade just the `nixos` channel (including any channel
700700
that has a `.update-on-nixos-rebuild` file) or all.
701701
"""
702+
if not sudo and os.geteuid() != 0:
703+
raise NixOSRebuildError(
704+
"if you pass the '--upgrade' or '--upgrade-all' flag, you must "
705+
"also pass '--sudo' or run the command as root (e.g., with sudo)"
706+
)
707+
702708
for channel_path in Path("/nix/var/nix/profiles/per-user/root/channels/").glob("*"):
703709
if channel_path.is_dir() and (
704710
all_channels
705711
or channel_path.name == "nixos"
706712
or (channel_path / ".update-on-nixos-rebuild").exists()
707713
):
708-
run_wrapper(["nix-channel", "--update", channel_path.name], check=False)
714+
run_wrapper(
715+
["nix-channel", "--update", channel_path.name],
716+
check=False,
717+
sudo=sudo,
718+
)

pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -836,17 +836,34 @@ def test_switch_to_configuration_with_systemd_run(
836836
],
837837
)
838838
@patch("pathlib.Path.is_dir", autospec=True, return_value=True)
839-
def test_upgrade_channels(mock_is_dir: Mock, mock_glob: Mock) -> None:
840-
with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run:
841-
n.upgrade_channels(False)
842-
mock_run.assert_called_once_with(["nix-channel", "--update", "nixos"], check=False)
839+
@patch("os.geteuid", autospec=True, return_value=1000)
840+
@patch(get_qualified_name(n.run_wrapper, n), autospec=True)
841+
def test_upgrade_channels(
842+
mock_run: Mock,
843+
mock_geteuid: Mock,
844+
mock_is_dir: Mock,
845+
mock_glob: Mock,
846+
) -> None:
847+
with pytest.raises(m.NixOSRebuildError) as e:
848+
n.upgrade_channels(all_channels=False, sudo=False)
849+
assert str(e.value) == (
850+
"error: if you pass the '--upgrade' or '--upgrade-all' flag, you must "
851+
"also pass '--sudo' or run the command as root (e.g., with sudo)"
852+
)
843853

844-
with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run:
845-
n.upgrade_channels(True)
854+
n.upgrade_channels(all_channels=False, sudo=True)
855+
mock_run.assert_called_once_with(
856+
["nix-channel", "--update", "nixos"], check=False, sudo=True
857+
)
858+
859+
mock_geteuid.return_value = 0
860+
n.upgrade_channels(all_channels=True, sudo=False)
846861
mock_run.assert_has_calls(
847862
[
848-
call(["nix-channel", "--update", "nixos"], check=False),
849-
call(["nix-channel", "--update", "nixos-hardware"], check=False),
850-
call(["nix-channel", "--update", "home-manager"], check=False),
863+
call(["nix-channel", "--update", "nixos"], check=False, sudo=False),
864+
call(
865+
["nix-channel", "--update", "nixos-hardware"], check=False, sudo=False
866+
),
867+
call(["nix-channel", "--update", "home-manager"], check=False, sudo=False),
851868
]
852869
)

0 commit comments

Comments
 (0)