Skip to content

Commit 0f6624e

Browse files
committed
nixos-rebuild-ng: validate NixOS config path in target host
In #418243 we started to validate NixOS config path to avoid a nasty bug in Nix, but this doesn't work in the `--build-host` and `--target-host` case because the configuration will not be available in the local host to check if it contains a `nixos-version`. This moves the check to `--target-host` instead, that is probably the correct choice anyway. Fix #418868.
1 parent 7c796b5 commit 0f6624e

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import sys
66
from pathlib import Path
77
from subprocess import CalledProcessError, run
8-
from textwrap import dedent
98
from typing import Final, assert_never
109

1110
from . import nix, tmpdir
@@ -338,29 +337,6 @@ def validate_image_variant(image_variant: str, variants: ImageVariants) -> None:
338337
)
339338

340339

341-
def validate_nixos_config(path_to_config: Path) -> None:
342-
if not (path_to_config / "nixos-version").exists() and not os.environ.get(
343-
"NIXOS_REBUILD_I_UNDERSTAND_THE_CONSEQUENCES_PLEASE_BREAK_MY_SYSTEM"
344-
):
345-
msg = dedent(
346-
# the lowercase for the first letter below is proposital
347-
f"""
348-
your NixOS configuration path seems to be missing essential files.
349-
To avoid corrupting your current NixOS installation, the activation will abort.
350-
351-
This could be caused by Nix bug: https://github.com/NixOS/nix/issues/13367.
352-
This is the evaluated NixOS configuration path: {path_to_config}.
353-
Change the directory to somewhere else (e.g., `cd $HOME`) before trying again.
354-
355-
If you think this is a mistake, you can set the environment variable
356-
NIXOS_REBUILD_I_UNDERSTAND_THE_CONSEQUENCES_PLEASE_BREAK_MY_SYSTEM to 1
357-
and re-run the command to continue.
358-
Please open an issue if this is the case.
359-
"""
360-
).strip()
361-
raise NixOSRebuildError(msg)
362-
363-
364340
def execute(argv: list[str]) -> None:
365341
args, args_groups = parse_args(argv)
366342

@@ -514,7 +490,6 @@ def execute(argv: list[str]) -> None:
514490
copy_flags=copy_flags,
515491
)
516492
if action in (Action.SWITCH, Action.BOOT):
517-
validate_nixos_config(path_to_config)
518493
nix.set_profile(
519494
profile,
520495
path_to_config,

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pathlib import Path
1010
from string import Template
1111
from subprocess import PIPE, CalledProcessError
12+
from textwrap import dedent
1213
from typing import Final, Literal
1314

1415
from . import tmpdir
@@ -613,6 +614,33 @@ def set_profile(
613614
sudo: bool,
614615
) -> None:
615616
"Set a path as the current active Nix profile."
617+
if not os.environ.get(
618+
"NIXOS_REBUILD_I_UNDERSTAND_THE_CONSEQUENCES_PLEASE_BREAK_MY_SYSTEM"
619+
):
620+
r = run_wrapper(
621+
["test", "-f", path_to_config / "nixos-version"],
622+
remote=target_host,
623+
check=False,
624+
)
625+
if r.returncode:
626+
msg = dedent(
627+
# the lowercase for the first letter below is proposital
628+
f"""
629+
your NixOS configuration path seems to be missing essential files.
630+
To avoid corrupting your current NixOS installation, the activation will abort.
631+
632+
This could be caused by Nix bug: https://github.com/NixOS/nix/issues/13367.
633+
This is the evaluated NixOS configuration path: {path_to_config}.
634+
Change the directory to somewhere else (e.g., `cd $HOME`) before trying again.
635+
636+
If you think this is a mistake, you can set the environment variable
637+
NIXOS_REBUILD_I_UNDERSTAND_THE_CONSEQUENCES_PLEASE_BREAK_MY_SYSTEM to 1
638+
and re-run the command to continue.
639+
Please open an issue if this is the case.
640+
"""
641+
).strip()
642+
raise NixOSRebuildError(msg)
643+
616644
run_wrapper(
617645
["nix-env", "-p", profile.path, "--set", path_to_config],
618646
remote=target_host,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None:
674674
def test_set_profile(mock_run: Mock) -> None:
675675
profile_path = Path("/path/to/profile")
676676
config_path = Path("/path/to/config")
677+
mock_run.return_value = CompletedProcess([], 0)
678+
677679
n.set_profile(
678680
m.Profile("system", profile_path),
679681
config_path,
@@ -687,6 +689,19 @@ def test_set_profile(mock_run: Mock) -> None:
687689
sudo=False,
688690
)
689691

692+
mock_run.return_value = CompletedProcess([], 1)
693+
694+
with pytest.raises(m.NixOSRebuildError) as e:
695+
n.set_profile(
696+
m.Profile("system", profile_path),
697+
config_path,
698+
target_host=None,
699+
sudo=False,
700+
)
701+
assert str(e.value).startswith(
702+
"error: your NixOS configuration path seems to be missing essential files."
703+
)
704+
690705

691706
@patch(get_qualified_name(n.run_wrapper, n), autospec=True)
692707
def test_switch_to_configuration_without_systemd_run(

0 commit comments

Comments
 (0)