Chore(templates): move helper functions into a util file

This commit is contained in:
Johannes Kirschbauer
2025-06-09 20:50:57 +02:00
parent 44f8edd52f
commit 92fdba3965
5 changed files with 43 additions and 35 deletions

View File

@@ -16,9 +16,9 @@ from clan_lib.persist.util import set_value_by_path
from clan_lib.templates import ( from clan_lib.templates import (
InputPrio, InputPrio,
TemplateName, TemplateName,
copy_from_nixstore,
get_template, get_template,
) )
from clan_lib.templates.filesystem import copy_from_nixstore
from clan_cli.completions import add_dynamic_completer, complete_tags from clan_cli.completions import add_dynamic_completer, complete_tags
from clan_cli.machines.list import list_full_machines from clan_cli.machines.list import list_full_machines

View File

@@ -15,11 +15,11 @@ from clan_lib.templates import (
ClanExports, ClanExports,
InputName, InputName,
TemplateName, TemplateName,
copy_from_nixstore,
get_clan_nix_attrset, get_clan_nix_attrset,
get_template, get_template,
list_templates, list_templates,
) )
from clan_lib.templates.filesystem import copy_from_nixstore
# Function to write clan attributes to a file # Function to write clan attributes to a file

View File

@@ -11,9 +11,9 @@ from clan_lib.persist.inventory_store import InventorySnapshot, InventoryStore
from clan_lib.templates import ( from clan_lib.templates import (
InputPrio, InputPrio,
TemplateName, TemplateName,
copy_from_nixstore,
get_template, get_template,
) )
from clan_lib.templates.filesystem import copy_from_nixstore
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@@ -1,15 +1,11 @@
import logging import logging
from dataclasses import dataclass, field from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Literal, NewType, TypedDict, cast from typing import Any, Literal, NewType, TypedDict, cast
from clan_lib.cmd import run
from clan_lib.dirs import clan_templates from clan_lib.dirs import clan_templates
from clan_lib.errors import ClanCmdError, ClanError from clan_lib.errors import ClanCmdError, ClanError
from clan_lib.flake import Flake from clan_lib.flake import Flake
from clan_lib.nix import ( from clan_lib.templates.filesystem import realize_nix_path
nix_command,
)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -170,11 +166,6 @@ class InputPrio:
return InputPrio(prioritize_self=True, input_names=input_names) return InputPrio(prioritize_self=True, input_names=input_names)
def copy_from_nixstore(src: Path, dest: Path) -> None:
run(["cp", "-r", str(src), str(dest)])
run(["chmod", "-R", "u+w", str(dest)])
@dataclass @dataclass
class TemplateList: class TemplateList:
inputs: dict[InputName, dict[TemplateName, Template]] = field(default_factory=dict) inputs: dict[InputName, dict[TemplateName, Template]] = field(default_factory=dict)
@@ -205,28 +196,6 @@ def list_templates(
return result return result
def realize_nix_path(flake: Flake, nix_store_path: str) -> None:
"""
Downloads / realizes a nix path into the nix store
"""
if Path(nix_store_path).exists():
return
cmd = [
"flake",
"prefetch",
"--inputs-from",
flake.identifier,
"--option",
"flake-registry",
"",
nix_store_path,
]
run(nix_command(cmd))
def get_template( def get_template(
template_name: TemplateName, template_name: TemplateName,
template_type: TemplateType, template_type: TemplateType,

View File

@@ -0,0 +1,39 @@
from pathlib import Path
from clan_lib.flake import Flake
from clan_lib.nix import (
nix_command,
run,
)
def realize_nix_path(clan_dir: Flake, nix_path: str) -> None:
"""
Downloads / realizes a nix path into the nix store
"""
if Path(nix_path).exists():
return
cmd = [
"flake",
"prefetch",
"--inputs-from",
clan_dir.identifier,
"--option",
"flake-registry",
"",
nix_path,
]
run(nix_command(cmd))
def copy_from_nixstore(src: Path, dest: Path) -> None:
"""
Copy a directory from the nix store to a destination path.
Uses `cp -r` to recursively copy the directory.
Ensures the destination directory is writable by the user.
"""
run(["cp", "-r", str(src), str(dest)])
run(["chmod", "-R", "u+w", str(dest)])