diff --git a/pkgs/clan-cli/clan_cli/clan/create.py b/pkgs/clan-cli/clan_cli/clan/create.py index 75921131d..3e43e70c7 100644 --- a/pkgs/clan-cli/clan_cli/clan/create.py +++ b/pkgs/clan-cli/clan_cli/clan/create.py @@ -9,12 +9,10 @@ from clan_cli.arg_actions import AppendOptionAction from clan_cli.inventory import Meta, load_inventory, save_inventory from ..cmd import CmdOut, run +from ..dirs import clan_templates from ..errors import ClanError from ..nix import nix_command, nix_shell -default_template_url: str = "git+https://git.clan.lol/clan/clan-core" -minimal_template_url: str = "git+https://git.clan.lol/clan/clan-core#templates.minimal" - @dataclass class CreateClanResponse: @@ -33,7 +31,7 @@ class CreateOptions: # Metadata can be shown with `clan show` meta: Meta | None = None # URL to the template to use. Defaults to the "minimal" template - template_url: str = minimal_template_url + template: str = "minimal" def git_command(directory: Path, *args: str) -> list[str]: @@ -43,7 +41,7 @@ def git_command(directory: Path, *args: str) -> list[str]: @API.register def create_clan(options: CreateOptions) -> CreateClanResponse: directory = Path(options.directory).resolve() - template_url = options.template_url + template_url = f"{clan_templates()}#{options.template}" if not directory.exists(): directory.mkdir() else: @@ -112,10 +110,11 @@ def create_clan(options: CreateOptions) -> CreateClanResponse: def register_create_parser(parser: argparse.ArgumentParser) -> None: parser.add_argument( - "--url", + "--template", type=str, - help="url to the clan template", - default=default_template_url, + choices=["default", "minimal"], + help="Clan template name", + default="default", ) parser.add_argument( @@ -135,7 +134,7 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None: create_clan( CreateOptions( directory=args.path, - template_url=args.url, + template=args.template, ) ) diff --git a/pkgs/clan-cli/clan_cli/dirs.py b/pkgs/clan-cli/clan_cli/dirs.py index 9b687aa43..9249eab21 100644 --- a/pkgs/clan-cli/clan_cli/dirs.py +++ b/pkgs/clan-cli/clan_cli/dirs.py @@ -4,6 +4,8 @@ import sys import urllib from pathlib import Path +from .errors import ClanError + log = logging.getLogger(__name__) @@ -41,6 +43,18 @@ def find_toplevel(top_level_files: list[str]) -> Path | None: return None +def clan_templates() -> Path: + template_path = module_root().parent.parent / "templates" + if template_path.exists(): + return template_path + else: + template_path = module_root() / "templates" + if not template_path.exists(): + msg = f"BUG! clan core not found at {template_path}. This is an issue with packaging the cli" + raise ClanError(msg) + return template_path + + def user_config_dir() -> Path: if sys.platform == "win32": return Path(os.getenv("APPDATA", os.path.expanduser("~\\AppData\\Roaming\\"))) diff --git a/pkgs/clan-cli/default.nix b/pkgs/clan-cli/default.nix index a1968f850..8a29c9a0d 100644 --- a/pkgs/clan-cli/default.nix +++ b/pkgs/clan-cli/default.nix @@ -63,6 +63,7 @@ let rm $out/clan_cli/config/jsonschema ln -sf ${nixpkgs'} $out/clan_cli/nixpkgs cp -r ${../../lib/jsonschema} $out/clan_cli/config/jsonschema + cp -r ${../../templates} $out/clan_cli/templates ${classgen}/bin/classgen ${inventory-schema}/schema.json $out/clan_cli/inventory/classes.py ''; diff --git a/pkgs/clan-cli/pyproject.toml b/pkgs/clan-cli/pyproject.toml index 28fef0831..ee6d46137 100644 --- a/pkgs/clan-cli/pyproject.toml +++ b/pkgs/clan-cli/pyproject.toml @@ -18,7 +18,14 @@ Repository = "https://git.clan.lol/clan/clan-core" exclude = ["clan_cli.nixpkgs*", "result"] [tool.setuptools.package-data] -clan_cli = ["py.typed", "config/jsonschema/*", "webui/assets/**/*", "vms/mimetypes/**/*", "**/allowed-programs.json"] +clan_cli = [ + "**/allowed-programs.json", + "config/jsonschema/*", + "py.typed", + "templates/**/*", + "vms/mimetypes/**/*", + "webui/assets/**/*", +] [tool.pytest.ini_options] testpaths = "tests" diff --git a/templates/flake-module.nix b/templates/flake-module.nix index e48375b57..1caecf690 100644 --- a/templates/flake-module.nix +++ b/templates/flake-module.nix @@ -1,35 +1,26 @@ { self, inputs, ... }: { - flake.templates = { - new-clan = { - description = "Initialize a new clan flake"; - path = ./new-clan; - }; - default = self.templates.new-clan; - minimal = { - description = "for clans managed via (G)UI"; - path = ./minimal; - }; - }; - flake.checks.x86_64-linux.template-minimal = - let - path = self.templates.minimal.path; - initialized = inputs.nixpkgs.legacyPackages.x86_64-linux.runCommand "minimal-clan-flake" { } '' - mkdir $out - cp -r ${path}/* $out - mkdir -p $out/machines/foo - echo '{ "nixpkgs": { "hostPlatform": "x86_64-linux" } }' > $out/machines/foo/settings.json - ''; - evaled = (import "${initialized}/flake.nix").outputs { - self = evaled // { - outPath = initialized; + flake = (import ./flake.nix).outputs {} // { + checks.x86_64-linux.template-minimal = + let + path = self.templates.minimal.path; + initialized = inputs.nixpkgs.legacyPackages.x86_64-linux.runCommand "minimal-clan-flake" { } '' + mkdir $out + cp -r ${path}/* $out + mkdir -p $out/machines/foo + echo '{ "nixpkgs": { "hostPlatform": "x86_64-linux" } }' > $out/machines/foo/settings.json + ''; + evaled = (import "${initialized}/flake.nix").outputs { + self = evaled // { + outPath = initialized; + }; + clan-core = self; }; - clan-core = self; + in + { + type = "derivation"; + name = "minimal-clan-flake-check"; + inherit (evaled.nixosConfigurations.foo.config.system.build.vm) drvPath outPath; }; - in - { - type = "derivation"; - name = "minimal-clan-flake-check"; - inherit (evaled.nixosConfigurations.foo.config.system.build.vm) drvPath outPath; - }; + }; } diff --git a/templates/flake.nix b/templates/flake.nix new file mode 100644 index 000000000..438d8ab54 --- /dev/null +++ b/templates/flake.nix @@ -0,0 +1,16 @@ +{ + outputs = + { ... }: + { + templates = { + default = { + description = "Initialize a new clan flake"; + path = ./new-clan; + }; + minimal = { + description = "for clans managed via (G)UI"; + path = ./minimal; + }; + }; + }; +}