nixos-rebuild-ng: move edit to services.py

This commit is contained in:
Thiago Kenji Okada
2025-06-28 20:45:20 +01:00
parent ccade214e3
commit cb1adbc4ac
4 changed files with 54 additions and 40 deletions
@@ -9,7 +9,13 @@ from . import nix
from .constants import EXECUTABLE, WITH_NIX_2_18, WITH_REEXEC, WITH_SHELL_FILES
from .models import Action, BuildAttr, Flake, Profile
from .process import Remote
from .services import build_and_activate_system, list_generations, reexec, repl
from .services import (
build_and_activate_system,
edit,
list_generations,
reexec,
repl,
)
from .utils import LogFormatter
logger: Final = logging.getLogger(__name__)
@@ -342,7 +348,7 @@ def execute(argv: list[str]) -> None:
)
case Action.EDIT:
nix.edit(flake, flake_build_flags)
edit(flake, flake_build_flags)
case Action.DRY_RUN:
raise AssertionError("DRY_RUN should be a DRY_BUILD alias")
@@ -242,33 +242,33 @@ def copy_closure(
nix_copy_closure(to_host, to=True)
def edit(flake: Flake | None, flake_flags: Args | None = None) -> None:
def edit() -> None:
"Try to find and open NixOS configuration file in editor."
if flake:
run_wrapper(
[
"nix",
*FLAKE_FLAGS,
"edit",
*dict_to_flags(flake_flags),
"--",
str(flake),
],
check=False,
)
else:
if flake_flags:
raise NixOSRebuildError("'edit' does not support extra Nix flags")
nixos_config = Path(
os.getenv("NIXOS_CONFIG") or find_file("nixos-config") or "/etc/nixos"
)
if nixos_config.is_dir():
nixos_config /= "default.nix"
nixos_config = Path(
os.getenv("NIXOS_CONFIG") or find_file("nixos-config") or "/etc/nixos"
)
if nixos_config.is_dir():
nixos_config /= "default.nix"
if nixos_config.exists():
run_wrapper([os.getenv("EDITOR", "nano"), nixos_config], check=False)
else:
raise NixOSRebuildError("cannot find NixOS config file")
if nixos_config.exists():
run_wrapper([os.getenv("EDITOR", "nano"), nixos_config], check=False)
else:
raise NixOSRebuildError("cannot find NixOS config file")
def edit_flake(flake: Flake | None, flake_flags: Args | None = None) -> None:
"Try to find and open NixOS configuration file in editor for Flake config."
run_wrapper(
[
"nix",
*FLAKE_FLAGS,
"edit",
*dict_to_flags(flake_flags),
"--",
str(flake),
],
check=False,
)
def find_file(file: str, nix_flags: Args | None = None) -> Path | None:
@@ -318,6 +318,13 @@ def build_and_activate_system(
)
def edit(flake: Flake | None, flake_flags: Args | None = None) -> None:
if flake:
nix.edit_flake(flake, flake_flags)
else:
nix.edit()
def list_generations(
args: argparse.Namespace,
profile: Profile,
@@ -295,9 +295,21 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None:
@patch(get_qualified_name(n.run_wrapper, n), autospec=True)
def test_edit(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None:
# Flake
with monkeypatch.context() as mp:
default_nix = tmpdir / "default.nix"
default_nix.write_text("{}", encoding="utf-8")
mp.setenv("NIXOS_CONFIG", str(tmpdir))
mp.setenv("EDITOR", "editor")
n.edit()
mock_run.assert_called_with(["editor", default_nix], check=False)
@patch(get_qualified_name(n.run_wrapper, n), autospec=True)
def test_editd_flake(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None:
flake = m.Flake.parse(f"{tmpdir}#attr")
n.edit(flake, {"commit_lock_file": True})
n.edit_flake(flake, {"commit_lock_file": True})
mock_run.assert_called_with(
[
"nix",
@@ -311,17 +323,6 @@ def test_edit(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None:
check=False,
)
# Classic
with monkeypatch.context() as mp:
default_nix = tmpdir / "default.nix"
default_nix.write_text("{}", encoding="utf-8")
mp.setenv("NIXOS_CONFIG", str(tmpdir))
mp.setenv("EDITOR", "editor")
n.edit(None)
mock_run.assert_called_with(["editor", default_nix], check=False)
@patch(
get_qualified_name(n.run_wrapper, n),