Configuration documents and migrations
Sofka uses configuration documents to combine user-wide settings with optional drop-ins and cluster or context settings. It accepts TOML and YAML, fills omitted settings with safe defaults, reports bad input without losing a usable session, and keeps personal runtime choices in separate state files. See Configuration and command-line behavior for the wider startup and CLI flow. See Shell completion for command completion behavior.
Configuration document model
Configuration sources
The configuration system reads a base document from $XDG_CONFIG_HOME/sofka, or from ~/.config/sofka when the XDG location is not set. An empty XDG_CONFIG_HOME is treated as unset so the application does not use a relative path. A document can be TOML, YAML, or YML. The same setting names and meaning apply in each format.
The base document can be extended without changing it. Drop-in documents live in conf.d/ and are applied in name order. Cluster and context documents live below clusters/; a cluster document applies to all contexts in that cluster, while a context document applies to one kubeconfig context. Directory names are safe forms of the kubeconfig names, so names with path or punctuation characters map to usable directory names. This lets shared, cluster-specific, and context-specific policy remain separate.
Each configuration level has one selected document. If config.toml, config.yaml, and config.yml appear together at the same level, Sofka reports a conflict instead of choosing one by chance.
References: src/config.rs• L1-24 src/config.rs• L1515-1651 src/config.rs• L1933-1985
Document formats
TOML is the default format, and YAML provides the same configuration contract. YAML must contain one mapping at its root. Empty documents and comment-only documents mean “no settings”; explicit null values, non-string mapping keys, merge keys, and custom tags are rejected so that a document does not silently change meaning.
References: src/config.rs• L1831-1900
Configuration resolution
Merge order
Sofka resolves effective settings by layering broad settings before narrow settings. The order is base document, drop-ins in file-name order, cluster document, then context document. Tables merge by key, while scalar values and arrays replace the earlier value. A later layer therefore has the last word for a setting unless the setting has a documented merge rule.
Plugin lists have an explicit merge rule because a list replacement is not always useful for layered configuration. A layer can remove named plugins with plugins_remove, or set plugins_merge = "name" so later entries replace earlier entries with the same name while keeping the list order. These controls apply to that layer only.
This resolution produces the settings for one cluster and context. The same base loader can resolve different contexts, so a context switch can apply a different policy without rebuilding the whole configuration model. Override files are read again when the context changes or the user reloads configuration.
This diagram shows how broad policy becomes the effective settings for one active context.
flowchart TD
A[Base document] --> B[Ordered drop-ins]
B --> C[Cluster document]
C --> D[Context document]
D --> E[Effective configuration]
E --> F[Session behavior]
References: src/config.rs• L1501-1513 src/config.rs• L1654-1760 src/config.rs• L1763-1829
Configuration errors
The loader treats configuration errors as local problems where possible. A bad base document causes Sofka to use defaults at startup, while a failed reload leaves the last valid loader in place. A bad drop-in or override is skipped with a warning. If an override creates a type error in the final merged settings, Sofka falls back to the valid base configuration rather than discarding the entire session.
Warnings remain visible to the application so the user can inspect the source and the reason for the fallback. This separates “the application can run” from “the configuration is correct” and avoids hiding a setting mistake behind a silent default.
References: src/config.rs• L1527-1585 src/config.rs• L1666-1759 src/config.rs• L1841-1905
Defaults and compatibility
Default settings
Top-level configuration settings are optional. Missing values use application defaults, so an empty document selects normal behavior. The defaults cover startup choices, safety mode, views, notifications, logs, diagnostic tools, integrations, and other session policy. Some defaults enable normal convenience features, while others provide safe limits or paths under the state directory.
Defaults are part of the user-facing contract. They apply before cluster and context overrides, and an override can replace a default for a narrower scope. A setting that is not present in any layer does not need a placeholder in the document.
References: src/config.rs• L44-149 src/config.rs• L212-249 src/config.rs• L386-518 src/config.rs• L536-749 src/config.rs• L770-824 src/config.rs• L1077-1085 src/config.rs• L1340-1387
Compatibility key migrations
The configuration system upgrades old palette key names to the current keys.command group before it merges layers. The migration maps palette_next to down, palette_prev to up, and palette_accept to accept. Applying the change before merging preserves the precedence of a context-specific binding over a broader binding.
Migration is designed to protect user documents. Sofka validates the old values, refuses an ambiguous move when the new key already exists, checks that the source did not change, and verifies that other settings remain unchanged. A successful TOML migration keeps document formatting where possible; YAML is written as a valid YAML document.
When Sofka saves a migration, it keeps the original beside the document with a .bak suffix and replaces the source through a checked temporary write. Read-only configuration inspection can resolve the effective settings without writing migrations. Read-only files and symlinks are not rewritten. If a save fails, the migrated keys still work in memory and the user receives instructions to update the source manually.
References: src/config.rs• L1666-1732 src/config.rs• L1841-1900
Durable user preferences
State directory
Configuration expresses durable policy that users or teams edit. Small personal choices use a separate state directory instead, so Sofka can remember behavior without rewriting the configuration document. The state directory is $XDG_STATE_HOME/sofka, then ~/.local/state/sofka, with a temporary fallback when no home path is available.
References: src/config.rs• L437-467 src/config.rs• L469-518 src/config.rs• L1954-1985
Namespace memory
Sofka remembers the last namespace choice for each kubeconfig context in namespaces.toml. The remembered value is a fallback: explicit CLI or navigation choices win, and default_namespace remains the configured fallback when no saved choice exists. The special value all represents all namespaces in the hand-editable state file.
References: src/config.rs• L60-67
Sort memory
Sofka remembers a sort column and direction for each resource kind in sort.toml. This state overlays the configured view sort, so a user’s learned preference can survive visits and restarts while the configuration still supplies the initial or fallback order. remember_sort = false stops new saves and restores without deleting the existing state.
References: src/config.rs• L915-956
State writes
Namespace and sort changes are saved through an ordered background writer. It uses atomic file replacement and keeps only the newest queued snapshot for each destination. Write failures reach the live status area or the exit warning, while shutdown waits briefly for accepted writes and then gives up so a slow filesystem cannot keep the terminal process open forever.
The state files are convenience memory, not an audit record. Missing or malformed state loads as empty state, which keeps startup usable but loses the affected preference until the user selects it again.
References: src/config.rs• L60-70 src/config.rs• L136-141
Change checklist
When these files or sections are changed, remember to consider these:
- Keep TOML and YAML document selection aligned at every base, drop-in, cluster, and context level.
- Preserve the merge order and the distinction between key-wise table merges and whole-value replacement.
- Keep plugin list controls scoped to the layer that declares them, and preserve name-based replacement and removal behavior.
- Treat an empty
XDG_CONFIG_HOMEas unset so config and installed plugin paths continue to agree. - Update compatibility mappings and the durable migration path together when a key name changes.
- Keep saved namespace and sort state outside user-edited configuration so runtime choices do not alter shared policy.
- Preserve the fallback behavior for invalid base documents, invalid overrides, malformed state files, and failed background writes.
- Keep the state-file schemas compatible with hand-edited values such as
all,*, andCOLUMN:desc.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| More than one supported config format exists at one level | That level is reported as conflicting and is not selected | Keep one of config.toml, config.yaml, or config.yml in each directory |
| A base document fails syntax or type validation | Startup uses defaults, or a failed reload keeps the last valid configuration | Inspect configuration warnings before relying on a changed setting |
| An override introduces a type mismatch | The override layers are ignored and the valid base settings remain active | Keep the same value type across layers |
| A plugin removal or name merge control has the wrong type | That configuration layer is skipped rather than partially applied | Keep plugins_merge as replace or name, and keep plugins_remove a string list |
A legacy key collides with its new keys.command name | The migration is not saved and the old source must be corrected manually | Remove the duplicate and reload |
| A config migration source is read-only, a symlink, or changes during save | Settings work for the session but the source is not rewritten | Apply the reported manual key move or restore a writable regular file |
| State storage is malformed, unavailable, or still busy at shutdown | A namespace or sort choice may be forgotten; the application still exits | Treat state as convenience memory and check status or exit warnings |
Key files
| File | Why to read it |
|---|---|
| src/config.rs | Defines the configuration contract, document layers, defaults, validation, plugin merge controls, and effective settings for a cluster and context. |
| src/…/document.rs | Defines supported document formats, file selection, and YAML-to-configuration rules. |
| src/…/key_migration.rs | Shows how old key names become current names and how source files are backed up and safely replaced. |
| src/nsmem.rs | Explains per-context namespace memory and its precedence over configured fallbacks. |
| src/sortmem.rs | Explains per-kind sort memory and its relationship to configured view defaults. |
| src/state_writer.rs | Shows how preference writes are ordered, reported, and bounded during shutdown. |
| src/diagnostics.rs | Defines the state-directory locations shared by durable user state and diagnostics. |
| docs/configuration.md | Provides the user-facing configuration model, format rules, override examples, and setting-specific links. |