kogral/docs/book/architecture/adrs/001-nickel-vs-toml.html

509 lines
23 KiB
HTML
Raw Normal View History

2026-01-23 16:11:07 +00:00
<!DOCTYPE HTML>
<html lang="en" class="rust sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>ADR-001: Nickel vs TOML for Configuration - KOGRAL Documentation</title>
<!-- Custom HTML head -->
<meta name="description" content="Complete documentation for KOGRAL - Git-native knowledge graphs for developer teams">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../../favicon.svg">
<link rel="shortcut icon" href="../../favicon.png">
<link rel="stylesheet" href="../../css/variables.css">
<link rel="stylesheet" href="../../css/general.css">
<link rel="stylesheet" href="../../css/chrome.css">
<link rel="stylesheet" href="../../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="../../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../../";
const default_light_theme = "rust";
const default_dark_theme = "navy";
</script>
<!-- Start loading toc.js asap -->
<script src="../../toc.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd></kbd> or <kbd></kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('rust')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">KOGRAL Documentation</h1>
<div class="right-buttons">
<a href="../../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/your-org/knowledge-base" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/your-org/knowledge-base/edit/main/docs/./architecture/adrs/001-nickel-vs-toml.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="adr-001-nickel-vs-toml-for-configuration"><a class="header" href="#adr-001-nickel-vs-toml-for-configuration">ADR-001: Nickel vs TOML for Configuration</a></h1>
<p><strong>Status</strong>: Accepted</p>
<p><strong>Date</strong>: 2026-01-17</p>
<p><strong>Deciders</strong>: Architecture Team</p>
<p><strong>Context</strong>: Configuration Strategy for Knowledge Base System</p>
<hr />
<h2 id="context"><a class="header" href="#context">Context</a></h2>
<p>The KOGRAL requires a flexible, type-safe configuration format that supports:</p>
<ol>
<li><strong>Complex nested structures</strong> (graph settings, storage configs, embedding providers)</li>
<li><strong>Type validation</strong> (prevent runtime errors from config mistakes)</li>
<li><strong>Composition and inheritance</strong> (shared configs, environment-specific overrides)</li>
<li><strong>Documentation</strong> (self-documenting schemas)</li>
<li><strong>Validation before runtime</strong> (catch errors early)</li>
</ol>
<p>We evaluated two primary options:</p>
<h3 id="option-1-toml-traditional-config-format"><a class="header" href="#option-1-toml-traditional-config-format">Option 1: TOML (Traditional Config Format)</a></h3>
<p><strong>Pros</strong>:</p>
<ul>
<li>Widely adopted in Rust ecosystem (<code>Cargo.toml</code>)</li>
<li>Simple, human-readable syntax</li>
<li>Native <code>serde</code> support</li>
<li>IDE support (syntax highlighting, completion)</li>
</ul>
<p><strong>Cons</strong>:</p>
<ul>
<li>No type system (validation only at runtime)</li>
<li>Limited composition (no imports, no functions)</li>
<li>No schema validation (errors discovered during execution)</li>
<li>Verbose for complex nested structures</li>
<li>No documentation in config files</li>
</ul>
<p><strong>Example TOML</strong>:</p>
<pre><code class="language-toml">[graph]
name = "my-project"
version = "1.0.0"
[storage]
primary = "filesystem" # String, not validated as enum
[storage.secondary]
enabled = true
type = "surrealdb" # Typo would fail at runtime
url = "ws://localhost:8000"
[embeddings]
enabled = true
provider = "openai" # No validation of valid providers
model = "text-embedding-3-small"
</code></pre>
<p><strong>Problems</strong>:</p>
<ul>
<li>Typos in enum values (<code>"surrealdb"</code> vs <code>"surealdb"</code>) fail at runtime</li>
<li>No validation that <code>provider = "openai"</code> requires <code>api_key_env</code></li>
<li>No documentation of valid options</li>
<li>No way to compose configs (e.g., base config + environment override)</li>
</ul>
<h3 id="option-2-nickel-functional-configuration-language"><a class="header" href="#option-2-nickel-functional-configuration-language">Option 2: Nickel (Functional Configuration Language)</a></h3>
<p><strong>Pros</strong>:</p>
<ul>
<li><strong>Type system</strong> with contracts (validate before runtime)</li>
<li><strong>Composition</strong> via imports and merging</li>
<li><strong>Documentation</strong> in schemas (self-documenting)</li>
<li><strong>Validation</strong> at export time (catch errors early)</li>
<li><strong>Functions</strong> for conditional logic</li>
<li><strong>Default values</strong> in schema definitions</li>
</ul>
<p><strong>Cons</strong>:</p>
<ul>
<li>Less familiar to Rust developers</li>
<li>Requires separate <code>nickel</code> CLI tool</li>
<li>Smaller ecosystem</li>
<li>Steeper learning curve</li>
</ul>
<p><strong>Example Nickel</strong>:</p>
<pre><code class="language-nickel"># schemas/kb-config.ncl
{
KbConfig = {
graph | GraphConfig,
storage | StorageConfig,
embeddings | EmbeddingConfig,
},
StorageConfig = {
primary | [| 'filesystem, 'memory |], # Enum validated at export
secondary | {
enabled | Bool,
type | [| 'surrealdb, 'sqlite |], # Typos caught immediately
url | String,
} | optional,
},
EmbeddingConfig = {
enabled | Bool,
provider | [| 'openai, 'claude, 'fastembed |], # Valid providers enforced
model | String,
api_key_env | String | doc "Environment variable for API key",
},
}
</code></pre>
<p><strong>Benefits</strong>:</p>
<ul>
<li>Typos in enum values caught at <code>nickel export</code> time</li>
<li>Schema enforces required fields based on provider</li>
<li>Documentation embedded in schema</li>
<li>Config can be composed: <code>import "base.ncl" &amp; { /* overrides */ }</code></li>
</ul>
<hr />
<h2 id="decision"><a class="header" href="#decision">Decision</a></h2>
<p><strong>We will use Nickel for configuration.</strong></p>
<p><strong>Implementation</strong>:</p>
<ol>
<li>Define schemas in <code>schemas/*.ncl</code> with type contracts</li>
<li>Users write configs in <code>.kogral/config.ncl</code></li>
<li>Export to JSON via CLI: <code>nickel export --format json config.ncl</code></li>
<li>Load JSON in Rust via <code>serde_json</code> into typed structs</li>
</ol>
<p><strong>Pattern</strong> (double validation):</p>
<pre><code>Nickel Config (.ncl)
↓ [nickel export]
JSON (validated by Nickel contracts)
↓ [serde_json::from_str]
Rust Struct (validated by serde)
Runtime (guaranteed valid config)
</code></pre>
<p><strong>Bridge Code</strong> (<code>kb-core/src/config/nickel.rs</code>):</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub fn load_config&lt;P: AsRef&lt;Path&gt;&gt;(path: P) -&gt; Result&lt;KbConfig&gt; {
// Export Nickel to JSON
let json = export_nickel_to_json(path)?;
// Deserialize to Rust struct
let config: KbConfig = serde_json::from_str(&amp;json)?;
Ok(config)
}
fn export_nickel_to_json&lt;P: AsRef&lt;Path&gt;&gt;(path: P) -&gt; Result&lt;String&gt; {
let output = Command::new("nickel")
.arg("export")
.arg("--format").arg("json")
.arg(path.as_ref())
.output()?;
Ok(String::from_utf8(output.stdout)?)
}
<span class="boring">}</span></code></pre></pre>
<hr />
<h2 id="consequences"><a class="header" href="#consequences">Consequences</a></h2>
<h3 id="positive"><a class="header" href="#positive">Positive</a></h3>
<p><strong>Type Safety</strong>: Config errors caught before runtime</p>
<ul>
<li>Invalid enum values fail at export: <code>'filesystm</code> → error</li>
<li>Missing required fields detected: no <code>graph.name</code> → error</li>
<li>Type mismatches prevented: <code>enabled = "yes"</code> → error (expects Bool)</li>
</ul>
<p><strong>Self-Documenting</strong>: Schemas serve as documentation</p>
<ul>
<li><code>| doc "Environment variable for API key"</code> describes fields</li>
<li>Enum options visible in schema: <code>[| 'openai, 'claude, 'fastembed |]</code></li>
<li>Default values explicit: <code>| default = 'filesystem</code></li>
</ul>
<p><strong>Composition</strong>: Config reuse and overrides</p>
<pre><code class="language-nickel"># base.ncl
{ graph = { version = "1.0.0" } }
# project.ncl
import "base.ncl" &amp; { graph = { name = "my-project" } }
</code></pre>
<p><strong>Validation Before Deployment</strong>: Catch errors in CI</p>
<pre><code class="language-bash"># CI pipeline
nickel typecheck config.ncl
nickel export --format json config.ncl &gt; /dev/null
</code></pre>
<p><strong>Conditional Logic</strong>: Environment-specific configs</p>
<pre><code class="language-nickel">let is_prod = std.string.is_match "prod" (std.env.get "ENV") in
{
embeddings = {
provider = if is_prod then 'openai else 'fastembed,
},
}
</code></pre>
<h3 id="negative"><a class="header" href="#negative">Negative</a></h3>
<p><strong>Learning Curve</strong>: Team must learn Nickel syntax</p>
<ul>
<li><strong>Mitigation</strong>: Provide comprehensive examples in <code>config/</code> directory</li>
<li><strong>Mitigation</strong>: Document common patterns in <code>docs/config/</code></li>
</ul>
<p><strong>Tool Dependency</strong>: Requires <code>nickel</code> CLI installed</p>
<ul>
<li><strong>Mitigation</strong>: Document installation in setup guide</li>
<li><strong>Mitigation</strong>: Check <code>nickel</code> availability in <code>kb init</code> command</li>
</ul>
<p><strong>IDE Support</strong>: Limited compared to TOML</p>
<ul>
<li><strong>Mitigation</strong>: Use LSP (nickel-lang-lsp) for VSCode/Neovim</li>
<li><strong>Mitigation</strong>: Syntax highlighting available for major editors</li>
</ul>
<p><strong>Ecosystem Size</strong>: Smaller than TOML</p>
<ul>
<li><strong>Mitigation</strong>: Nickel actively developed by Tweag</li>
<li><strong>Mitigation</strong>: Stable language specification (v1.0+)</li>
</ul>
<h3 id="neutral"><a class="header" href="#neutral">Neutral</a></h3>
<p><strong>Two-Stage Loading</strong>: Nickel → JSON → Rust</p>
<ul>
<li>Not a performance concern (config loaded once at startup)</li>
<li>Adds resilience (double validation)</li>
<li>Allows runtime config inspection (read JSON directly)</li>
</ul>
<hr />
<h2 id="alternatives-considered"><a class="header" href="#alternatives-considered">Alternatives Considered</a></h2>
<h3 id="json-schema"><a class="header" href="#json-schema">JSON Schema</a></h3>
<p><strong>Rejected</strong>: Not ergonomic for humans to write</p>
<ul>
<li>No comments</li>
<li>Verbose syntax (<code>{"key": "value"}</code> vs <code>key = value</code>)</li>
<li>JSON Schema separate from config (duplication)</li>
</ul>
<h3 id="yaml"><a class="header" href="#yaml">YAML</a></h3>
<p><strong>Rejected</strong>: No type system, ambiguous parsing</p>
<ul>
<li>Boolean confusion: <code>yes</code>/<code>no</code>/<code>on</code>/<code>off</code>/<code>true</code>/<code>false</code></li>
<li>Indentation-sensitive (error-prone)</li>
<li>No validation without external tools</li>
</ul>
<h3 id="dhall"><a class="header" href="#dhall">Dhall</a></h3>
<p><strong>Rejected</strong>: More complex than needed</p>
<ul>
<li>Turing-incomplete by design (limits use cases)</li>
<li>Smaller ecosystem than Nickel</li>
<li>Steeper learning curve</li>
</ul>
<h3 id="kcl-kusionstack-configuration-language"><a class="header" href="#kcl-kusionstack-configuration-language">KCL (KusionStack Configuration Language)</a></h3>
<p><strong>Rejected</strong>: Kubernetes-focused, less general-purpose</p>
<ul>
<li>Designed for K8s manifests</li>
<li>Less mature than Nickel for general config</li>
</ul>
<hr />
<h2 id="implementation-timeline"><a class="header" href="#implementation-timeline">Implementation Timeline</a></h2>
<ol>
<li>✅ Define base schemas (<code>schemas/kb-config.ncl</code>)</li>
<li>✅ Implement Nickel loader (<code>kb-core/src/config/nickel.rs</code>)</li>
<li>✅ Create example configs (<code>config/defaults.ncl</code>, <code>config/production.ncl</code>)</li>
<li>✅ Document Nickel usage (<code>docs/config/nickel-schemas.md</code>)</li>
<li>⏳ Add LSP recommendations to setup guide</li>
<li>⏳ Create Nickel → TOML migration tool (for existing users)</li>
</ol>
<hr />
<h2 id="monitoring"><a class="header" href="#monitoring">Monitoring</a></h2>
<p><strong>Success Criteria</strong>:</p>
<ul>
<li>Config errors caught at export time (not runtime)</li>
<li>Users can compose configs for different environments</li>
<li>Team comfortable with Nickel syntax within 2 weeks</li>
</ul>
<p><strong>Metrics</strong>:</p>
<ul>
<li>Number of config validation errors caught before runtime</li>
<li>Time to diagnose config issues (should decrease)</li>
<li>User feedback on config complexity</li>
</ul>
<hr />
<h2 id="references"><a class="header" href="#references">References</a></h2>
<ul>
<li><a href="https://nickel-lang.org/">Nickel Language</a></li>
<li><a href="https://nickel-lang.org/user-manual/introduction">Nickel User Manual</a></li>
<li><a href="../../crates/kb-core/src/config/README.html">platform-config pattern</a> (reference implementation)</li>
<li><a href="https://toml.io/">TOML Specification</a></li>
</ul>
<hr />
<h2 id="revision-history"><a class="header" href="#revision-history">Revision History</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Date</th><th>Author</th><th>Change</th></tr></thead><tbody>
<tr><td>2026-01-17</td><td>Architecture Team</td><td>Initial decision</td></tr>
</tbody></table>
</div>
<hr />
<p><strong>Next ADR</strong>: <a href="002-fastembed-ai-providers.html">ADR-002: FastEmbed via AI Providers</a></p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../architecture/logseq-blocks-design.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../architecture/adrs/002-fastembed-ai-providers.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../../architecture/logseq-blocks-design.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../architecture/adrs/002-fastembed-ai-providers.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../../elasticlunr.min.js"></script>
<script src="../../mark.min.js"></script>
<script src="../../searcher.js"></script>
<script src="../../clipboard.min.js"></script>
<script src="../../highlight.js"></script>
<script src="../../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>