clan-cli: Filter out flake select traces to improve debug log visibility

This commit is contained in:
Qubasa
2025-07-11 14:34:23 +07:00
parent e875df5665
commit 0cf35480a2

View File

@@ -1,6 +1,7 @@
import json import json
import logging import logging
import os import os
import textwrap
from dataclasses import asdict, dataclass, field from dataclasses import asdict, dataclass, field
from enum import Enum from enum import Enum
from hashlib import sha1 from hashlib import sha1
@@ -588,7 +589,6 @@ class FlakeCache:
def load_from_file(self, path: Path) -> None: def load_from_file(self, path: Path) -> None:
with path.open("r") as f: with path.open("r") as f:
log.debug(f"Loading cache from {path}")
data = json.load(f) data = json.load(f)
self.cache = FlakeCacheEntry.from_json(data["cache"]) self.cache = FlakeCacheEntry.from_json(data["cache"])
@@ -662,7 +662,7 @@ class Flake:
""" """
Loads the flake into the store and populates self.store_path and self.hash such that the flake can evaluate locally and offline Loads the flake into the store and populates self.store_path and self.hash such that the flake can evaluate locally and offline
""" """
from clan_lib.cmd import run from clan_lib.cmd import RunOpts, run
from clan_lib.nix import ( from clan_lib.nix import (
nix_command, nix_command,
) )
@@ -681,7 +681,8 @@ class Flake:
self.identifier, self.identifier,
] ]
flake_prefetch = run(nix_command(cmd)) trace_prefetch = os.environ.get("CLAN_DEBUG_NIX_PREFETCH", "0") == "1"
flake_prefetch = run(nix_command(cmd), RunOpts(trace=trace_prefetch))
flake_metadata = json.loads(flake_prefetch.stdout) flake_metadata = json.loads(flake_prefetch.stdout)
self.store_path = flake_metadata["storePath"] self.store_path = flake_metadata["storePath"]
self.hash = flake_metadata["hash"] self.hash = flake_metadata["hash"]
@@ -698,8 +699,6 @@ class Flake:
nix_metadata, nix_metadata,
) )
log.debug(f"Invalidating cache for {self.identifier}")
self.prefetch() self.prefetch()
self._cache = FlakeCache() self._cache = FlakeCache()
@@ -814,11 +813,14 @@ class Flake:
}} }}
""" """
if len(selectors) > 1 : if len(selectors) > 1 :
log.debug(f""" msg = textwrap.dedent(f"""
selecting: {selectors} clan select "{selectors}"
""").lstrip("\n").rstrip("\n")
if os.environ.get("CLAN_DEBUG_NIX_SELECTORS"):
msg += textwrap.dedent(f"""
to debug run: to debug run:
nix repl --expr 'rec {{ nix repl --expr 'rec {{
flake = builtins.getFlake "self.identifier"; flake = builtins.getFlake "{self.identifier}";
selectLib = (builtins.getFlake "path:{select_source()}?narHash={select_hash}").lib; selectLib = (builtins.getFlake "path:{select_source()}?narHash={select_hash}").lib;
query = [ query = [
{" ".join( {" ".join(
@@ -829,20 +831,23 @@ nix repl --expr 'rec {{
)} )}
]; ];
}}' }}'
""") """).lstrip("\n")
log.debug(msg)
# fmt: on # fmt: on
elif len(selectors) == 1: elif len(selectors) == 1:
log.debug( msg = textwrap.dedent(f"""
f""" $ clan select "{selectors[0]}"
selecting: {selectors[0]} """).lstrip("\n").rstrip("\n")
if os.environ.get("CLAN_DEBUG_NIX_SELECTORS"):
msg += textwrap.dedent(f"""
to debug run: to debug run:
nix repl --expr 'rec {{ nix repl --expr 'rec {{
flake = builtins.getFlake "{self.identifier}"; flake = builtins.getFlake "{self.identifier}";
selectLib = (builtins.getFlake "path:{select_source()}?narHash={select_hash}").lib; selectLib = (builtins.getFlake "path:{select_source()}?narHash={select_hash}").lib;
query = selectLib.select '"''{selectors[0]}''"' flake; query = selectLib.select '"''{selectors[0]}''"' flake;
}}' }}'
""" """).lstrip("\n")
) log.debug(msg)
build_output = Path( build_output = Path(
run( run(