Skip to content

Configuration

config

Configuration management for Crux using TOML files.

Reads/writes ~/.crux/config.toml (or wherever paths.config_path() points).

detect_secrets_backend

detect_secrets_backend()

Return 'keychain' on macOS, 'secret-service' on Linux, 'plaintext' otherwise.

Source code in src/crux_cli/config.py
def detect_secrets_backend() -> str:
    """Return 'keychain' on macOS, 'secret-service' on Linux, 'plaintext' otherwise."""
    system = platform.system().lower()
    if system == "darwin":
        return "keychain"
    if system == "linux":
        return "secret-service"
    return "plaintext"

default_config

default_config()

Return the default configuration dict.

Source code in src/crux_cli/config.py
def default_config() -> dict[str, Any]:
    """Return the default configuration dict."""
    return {
        "secrets": {
            "backend": detect_secrets_backend(),
        },
        "paths": {
            "crux_home": str(crux_home()),
        },
    }

load_config

load_config(path=None)

Load configuration from TOML, merging file values over defaults.

Source code in src/crux_cli/config.py
def load_config(path: Path | None = None) -> dict[str, Any]:
    """Load configuration from TOML, merging file values over defaults."""
    cfg = default_config()
    target = path or config_path()

    if target.exists():
        with open(target, "rb") as f:
            file_cfg = tomllib.load(f)
        _deep_merge(cfg, file_cfg)

    return cfg

save_config

save_config(cfg, path=None)

Atomically write configuration to a TOML file.

Source code in src/crux_cli/config.py
def save_config(cfg: dict[str, Any], path: Path | None = None) -> None:
    """Atomically write configuration to a TOML file."""
    target = path or config_path()
    target.parent.mkdir(parents=True, exist_ok=True)
    fd, tmp = tempfile.mkstemp(dir=target.parent, suffix=".tmp")
    try:
        with open(fd, "w") as f:
            f.write(_to_toml(cfg))
        Path(tmp).replace(target)
    except BaseException:
        Path(tmp).unlink(missing_ok=True)
        raise