From 52fd912b45f847bc1b13eb6d1599b61a3f259792 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Mon, 17 Feb 2025 13:23:31 +0700 Subject: [PATCH] clan-cli: Fix clan flakes create inside an already existing git repo --- pkgs/clan-cli/clan_cli/clan/create.py | 9 ++++- pkgs/clan-cli/tests/test_create_flake.py | 48 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pkgs/clan-cli/clan_cli/clan/create.py b/pkgs/clan-cli/clan_cli/clan/create.py index 5dcfce84d..2c5231682 100644 --- a/pkgs/clan-cli/clan_cli/clan/create.py +++ b/pkgs/clan-cli/clan_cli/clan/create.py @@ -9,7 +9,7 @@ from clan_cli.cmd import CmdOut, RunOpts, run from clan_cli.errors import ClanError from clan_cli.flake import Flake from clan_cli.inventory import Inventory, init_inventory -from clan_cli.nix import nix_shell +from clan_cli.nix import nix_metadata, nix_shell from clan_cli.templates import ( InputPrio, TemplateName, @@ -48,6 +48,13 @@ def git_command(directory: Path, *args: str) -> list[str]: def create_clan(opts: CreateOptions) -> CreateClanResponse: dest = opts.dest.resolve() + try: + nix_metadata(str(opts.src_flake)) + except ClanError: + log.error(f"Found a repository, but it is not a valid flake: {opts.src_flake}") + log.warning("Setting src_flake to None") + opts.src_flake = None + template = get_template( TemplateName(opts.template_name), "clan", diff --git a/pkgs/clan-cli/tests/test_create_flake.py b/pkgs/clan-cli/tests/test_create_flake.py index 0dc7a613b..7e58d06a8 100644 --- a/pkgs/clan-cli/tests/test_create_flake.py +++ b/pkgs/clan-cli/tests/test_create_flake.py @@ -3,6 +3,7 @@ import subprocess from pathlib import Path import pytest +from clan_cli.cmd import run from fixtures_flakes import substitute from helpers import cli from stdout import CaptureOutput @@ -53,6 +54,53 @@ def test_create_flake( pytest.fail("nixosConfigurations.machine1 not found in flake outputs") +@pytest.mark.impure +def test_create_flake_existing_git( + monkeypatch: pytest.MonkeyPatch, + temporary_home: Path, + clan_core: Path, + capture_output: CaptureOutput, +) -> None: + flake_dir = temporary_home / "test-flake" + + run(["git", "init", str(temporary_home)]) + + cli.run(["flakes", "create", str(flake_dir), "--template=default"]) + + assert (flake_dir / ".clan-flake").exists() + # Replace the inputs.clan.url in the template flake.nix + substitute( + flake_dir / "flake.nix", + clan_core, + ) + # Dont evaluate the inventory before the substitute call + + monkeypatch.chdir(flake_dir) + cli.run(["machines", "create", "machine1"]) + + # create a hardware-configuration.nix that doesn't throw an eval error + + for patch_machine in ["jon", "sara"]: + ( + flake_dir / "machines" / f"{patch_machine}/hardware-configuration.nix" + ).write_text("{}") + + with capture_output as output: + cli.run(["machines", "list"]) + assert "machine1" in output.out + flake_show = subprocess.run( + ["nix", "flake", "show", "--json"], + check=True, + capture_output=True, + text=True, + ) + flake_outputs = json.loads(flake_show.stdout) + try: + flake_outputs["nixosConfigurations"]["machine1"] + except KeyError: + pytest.fail("nixosConfigurations.machine1 not found in flake outputs") + + @pytest.mark.impure def test_ui_template( monkeypatch: pytest.MonkeyPatch,