Added gcroot for clan Icon and VM

This commit is contained in:
Qubasa
2023-12-31 00:49:57 +01:00
parent 5c08242b7e
commit cb933787ff
5 changed files with 97 additions and 75 deletions

View File

@@ -1,3 +1,4 @@
import base64
import logging import logging
import os import os
import sys import sys
@@ -35,6 +36,23 @@ def user_config_dir() -> Path:
return Path(os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))) return Path(os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config")))
def user_gcroot_dir() -> Path:
p = user_config_dir() / "clan" / "gcroots"
p.mkdir(parents=True, exist_ok=True)
return p
def specific_groot_dir(*, clan_name: str, flake_url: str) -> Path:
# Always build icon so that we can symlink it to the gcroot
gcroot_dir = user_gcroot_dir()
burl = base64.urlsafe_b64encode(flake_url.encode()).decode()
# Create the directory if it already exists append a number to it till it doesn't exist and then create it
clan_gcroot = gcroot_dir / f"{clan_name}-{burl}"
clan_gcroot.mkdir(parents=True, exist_ok=True)
return clan_gcroot
def user_history_file() -> Path: def user_history_file() -> Path:
return user_config_dir() / "clan" / "history" return user_config_dir() / "clan" / "history"

View File

@@ -4,6 +4,7 @@ import subprocess
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from ..dirs import specific_groot_dir
from ..errors import ClanError from ..errors import ClanError
from ..machines.list import list_machines from ..machines.list import list_machines
from ..nix import nix_build, nix_config, nix_eval, nix_metadata from ..nix import nix_build, nix_config, nix_eval, nix_metadata
@@ -22,22 +23,7 @@ class FlakeConfig:
revision: str | None revision: str | None
def inspect_flake(flake_url: str | Path, flake_attr: str) -> FlakeConfig: def run_cmd(cmd: list[str]) -> str:
config = nix_config()
system = config["system"]
machines = list_machines(flake_url)
if flake_attr not in machines:
raise ClanError(
f"Machine {flake_attr} not found in {flake_url}. Available machines: {', '.join(machines)}"
)
cmd = nix_eval(
[
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanIcon'
]
)
proc = subprocess.run(cmd, text=True, stdout=subprocess.PIPE) proc = subprocess.run(cmd, text=True, stdout=subprocess.PIPE)
assert proc.stdout is not None assert proc.stdout is not None
if proc.returncode != 0: if proc.returncode != 0:
@@ -49,53 +35,54 @@ stdout:
{proc.stdout} {proc.stdout}
""" """
) )
res = proc.stdout.strip()
if res == "null":
icon_path = None
else:
icon_path = res.strip('"')
if not Path(icon_path).exists(): return proc.stdout.strip()
cmd = nix_build(
[
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanIcon' def inspect_flake(flake_url: str | Path, flake_attr: str) -> FlakeConfig:
] config = nix_config()
) system = config["system"]
proc = subprocess.run(cmd, text=True, capture_output=True)
assert proc.stdout is not None # Check if the machine exists
if proc.returncode != 0: machines = list_machines(flake_url)
if flake_attr not in machines:
raise ClanError( raise ClanError(
f""" f"Machine {flake_attr} not found in {flake_url}. Available machines: {', '.join(machines)}"
command: {shlex.join(cmd)}
exit code: {proc.returncode}
stdout:
{proc.stdout}
stderr:
{proc.stderr}
"""
) )
# Get the cLAN name
cmd = nix_eval( cmd = nix_eval(
[ [
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanName' f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanName'
] ]
) )
res = run_cmd(cmd)
clan_name = res.strip('"')
proc = subprocess.run(cmd, text=True, capture_output=True) # Get the clan icon path
assert proc.stdout is not None cmd = nix_eval(
if proc.returncode != 0: [
raise ClanError( f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanIcon'
f""" ]
command: {shlex.join(cmd)}
exit code: {proc.returncode}
stdout:
{proc.stdout}
stderr:
{proc.stderr}
"""
) )
clan_name = proc.stdout.strip().strip('"') res = run_cmd(cmd)
# If the icon is null, no icon is set for this cLAN
if res == "null":
icon_path = None
else:
icon_path = res.strip('"')
cmd = nix_build(
[
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanIcon'
],
specific_groot_dir(clan_name=clan_name, flake_url=str(flake_url))
/ "clanIcon",
)
run_cmd(cmd)
# Get the flake metadata
meta = nix_metadata(flake_url) meta = nix_metadata(flake_url)
return FlakeConfig( return FlakeConfig(

View File

@@ -25,9 +25,21 @@ def nix_flake_show(flake_url: str | Path) -> list[str]:
) )
def nix_build( def nix_build(flags: list[str], gcroot: Path | None = None) -> list[str]:
flags: list[str], if gcroot is not None:
) -> list[str]: return (
nix_command(
[
"build",
"--out-link",
str(gcroot),
"--print-out-paths",
"--no-write-lock-file",
]
)
+ flags
)
else:
return ( return (
nix_command( nix_command(
[ [

View File

@@ -10,7 +10,7 @@ from dataclasses import dataclass, field
from pathlib import Path from pathlib import Path
from typing import IO from typing import IO
from ..dirs import module_root from ..dirs import module_root, specific_groot_dir
from ..errors import ClanError from ..errors import ClanError
from ..nix import nix_build, nix_config, nix_shell from ..nix import nix_build, nix_config, nix_shell
from .inspect import VmConfig, inspect_vm from .inspect import VmConfig, inspect_vm
@@ -131,7 +131,9 @@ def get_vm_create_info(vm: VmConfig, nix_options: list[str]) -> dict[str, str]:
[ [
f'{clan_dir}#clanInternals.machines."{system}"."{machine}".config.system.clan.vm.create', f'{clan_dir}#clanInternals.machines."{system}"."{machine}".config.system.clan.vm.create',
*nix_options, *nix_options,
] ],
specific_groot_dir(clan_name=vm.clan_name, flake_url=str(vm.flake_url))
/ f"vm-{machine}",
) )
proc = subprocess.run( proc = subprocess.run(
cmd, cmd,

View File

@@ -19,15 +19,18 @@
"**/.mypy_cache": true, "**/.mypy_cache": true,
"**/.reports": true, "**/.reports": true,
"**/.ruff_cache": true, "**/.ruff_cache": true,
"**/result": true "**/result": true,
"/nix/store/**": true
}, },
"search.exclude": { "search.exclude": {
"**/.direnv": true,
"**/.mypy_cache": true,
"**/.ruff_cache": true,
"**/.hypothesis": true,
"**/__pycache__": true, "**/__pycache__": true,
"**/.reports": true "**/.direnv": true,
} "**/.hypothesis": true,
"**/.mypy_cache": true,
"**/.reports": true,
"**/.ruff_cache": true,
"/nix/store/**": true
},
"files.autoSave": "off"
} }
} }