Commit cb1adbc4 authored by Thiago Kenji Okada's avatar Thiago Kenji Okada
Browse files

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

parent ccade214
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -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")
+24 −24
Original line number Diff line number Diff line
@@ -242,9 +242,22 @@ 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:
    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")


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",
@@ -256,19 +269,6 @@ def edit(flake: Flake | None, flake_flags: Args | None = None) -> None:
        ],
        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"

        if nixos_config.exists():
            run_wrapper([os.getenv("EDITOR", "nano"), nixos_config], check=False)
        else:
            raise NixOSRebuildError("cannot find NixOS config file")


def find_file(file: str, nix_flags: Args | None = None) -> Path | None:
+7 −0
Original line number Diff line number Diff line
@@ -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,
+14 −13
Original line number Diff line number Diff line
@@ -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),