pkgs/clan/lib: Fix clan template creation when already in a flake

Fix clan template creation when already in a flake.
Currently we already fail with very clear and descriptive error when
trying to evaluate the template of the flake we are in:
```
Failed to select template 'flake-parts' from flake '/tmp/superclan' (via attribute path: /tmp/superclan#clanInternals.templates.clan."flake-parts")
```

This is undesired behavior.
When we are trying to create a clan with `clan flakes create`.
We can't rely on the fact that the flake we are currently in exports flake templates.

Now we *try* to evaluate the flake we are in upon creation.
If there are no clan templates available, we now will fall back to
builtin templates.

Closes: #4472
This commit is contained in:
a-kenji
2025-07-26 15:03:41 +02:00
parent 1d8ac7b1b5
commit 0a43721a45
2 changed files with 34 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
@@ -132,3 +132,21 @@ 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()