Merge pull request 'pkgs/clan/lib: Fix clan template creation when already in a flake' (#4501) from kenji/ke-clan-flakes-create-existing-flake-fix into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4501
This commit is contained in:
Kenji Berthold
2025-07-29 11:19:04 +00:00
2 changed files with 52 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ import logging
from pathlib import Path
import pytest
from clan_cli.tests.fixtures_flakes import substitute
from clan_cli.tests.fixtures_flakes import FlakeForTest, substitute
from clan_cli.tests.helpers import cli
from clan_cli.tests.stdout import CaptureOutput
from clan_lib.cmd import run
@@ -125,3 +125,39 @@ def test_ui_template(
flake_outputs["nixosConfigurations"]["machine1"]
except KeyError:
pytest.fail("nixosConfigurations.machine1 not found in flake outputs")
@pytest.mark.with_core
def test_create_flake_fallback_from_non_clan_directory(
monkeypatch: pytest.MonkeyPatch,
temporary_home: Path,
test_flake: FlakeForTest,
) -> None:
"""Test that clan flakes create falls back to builtin templates from non-clan flake."""
monkeypatch.chdir(test_flake.path)
new_clan_dir = temporary_home / "new-clan"
monkeypatch.setenv("LOGNAME", "testuser")
cli.run(
["flakes", "create", str(new_clan_dir), "--template=default", "--no-update"]
)
assert (new_clan_dir / "flake.nix").exists()
@pytest.mark.with_core
def test_create_flake_with_local_template_reference(
monkeypatch: pytest.MonkeyPatch,
temporary_home: Path,
test_flake: FlakeForTest,
) -> None:
monkeypatch.chdir(test_flake.path)
new_clan_dir = temporary_home / "new-clan"
monkeypatch.setenv("LOGNAME", "testuser")
# TODO: should error with: localFlake does not export myLocalTemplate clan template
cli.run(
["flakes", "create", str(new_clan_dir), "--template=.#default", "--no-update"]
)
assert (new_clan_dir / "flake.nix").exists()

View File

@@ -4,7 +4,7 @@ from collections.abc import Callable, Iterator
from contextlib import contextmanager
from pathlib import Path
from clan_lib.dirs import specific_machine_dir
from clan_lib.dirs import clan_templates, specific_machine_dir
from clan_lib.errors import ClanError
from clan_lib.flake import Flake
from clan_lib.machines.actions import list_machines
@@ -138,8 +138,20 @@ def clan_template(
try:
template = template_flake.select(template_selector)
except ClanError as e:
msg = f"Failed to select template '{template_ident}' from flake '{flake_ref}' (via attribute path: {printable_template_ref})"
raise ClanError(msg) from e
try:
log.info(
f"Template '{template_ident}' not found in {flake_ref}, trying builtin template"
)
builtin_flake = Flake(str(clan_templates()))
[_, builtin_selector] = transform_url(
"clan", template_ident, flake=builtin_flake
)
template = builtin_flake.select(builtin_selector)
template_flake = builtin_flake
printable_template_ref = f"{clan_templates()}#{builtin_selector}"
except ClanError:
msg = f"Failed to select template '{template_ident}' from flake '{flake_ref}' (via attribute path: {printable_template_ref})"
raise ClanError(msg) from e
src = template.get("path")
if not src: