knixl
knixl generates maintainable, human-readable Nix from small amounts of opinionated KDL.
Pronounced "nix-ull". Written in Rust. KDL is the source of truth, the generated Nix is a committed build artefact, and regeneration is version-aware so a framework upgrade cannot change your output without telling you first.
What it does
You write a few lines of KDL:
host "web" {
system "x86_64-linux"
web-service "example.com" {
upstream "http://127.0.0.1:3000"
acme email="ops@example.com"
hardened #true
}
}
knixl expands it into a full, idiomatic NixOS module (nginx enabled, TLS and proxy recommendations on, ACME wired up, security headers added), formats it with a pinned formatter, writes it to generated/, and records hashes in a lockfile so the output is reproducible byte-for-byte.
The whole loop is compile, check, and read back the typed reference for any node:
Install
Nix (flake)
Or add overlays.default and put pkgs.knixl in your environment.systemPackages (NixOS) or home.packages (home-manager).
crates.io
Prebuilt binary
Download a tarball for your platform from the latest release, or run the installer:
|
Built for Linux (gnu and musl) and macOS, on x86_64 and aarch64.
From source
&&
The binary lands at target/release/knixl.
Runtime prerequisites
generate, check, and install also need:
- Nix on PATH, for the oracle (option-path validation) and for
install's package eval. - a formatter:
nixfmt-rfc-styleornixfmton PATH.KNIXL_FORMATTERoverrides which binary is used. The Nix flake install already wrapsnixfmtonto knixl's PATH.
Note: without the oracle's options.json cache populated, path validation is quietly skipped, so a typo'd option path will not be caught. See docs/06-oracle.md for the cache.
Quickstart
A knixl project is a directory with a hosts/ folder. The stdlib modules are built into knixl, so that is all you need to start; your own declarative modules, if you write any, live in an optional modules/ folder beside it. knixl walks up from the current directory to the first folder that has hosts/ or knixl.lock.kdl, and treats that as the root.
&&
From there, add a module node to the host and regenerate. The modules are built in (see below), so there is nothing to install or copy first. knixl doc <node> prints what a node accepts before you write it.
Batteries included
knixl ships a curated set of modules (built-in Rust and declarative KDL alike), all embedded in the binary and usable in any project with no setup (no modules/ directory to place, nothing to fetch):
- web-service : an nginx vhost with TLS, ACME, and hardening presets.
- disko : declarative disk layout, GPT partitions with ext4/vfat/swap/LUKS content and ZFS pools, plus a
boot-root-zfsshorthand. - zfs, user, openssh : the host primitives, ZFS with a stable
hostId, a login user with SSH keys, a hardened sshd. - tailscale : Tailscale with an auth key pulled from a named secret.
- incus : an Incus host, enable plus the web UI, the storage/network/profile preseed (with optional bridge ipv6), an optional API listener (static or bound to an interface's address at runtime), and host-firewall integration.
- home-manager : per-user home-manager with the "Words of warning" guardrails (required
stateVersion, safeuseUserPackages,nix.gcleft to NixOS) baked in. - os : core host config, hostName, stateVersion, boot loader and kernel, timezone/locale, sysctl, nix.settings, and systemPackages.
- guest : a NixOS system container whose config is a nested knixl module tree (
web-serviceinside a guest just works, re-rooted undercontainers.<name>.config). - host, postgres, backups, package, raw-nix, security-headers : the rest of the built-ins.
Run knixl doc <node> for the typed reference of any of them.
Three things worth calling out:
- Generate a bootable system. Add a
system { }block toknixl.kdlandgeneratealso emitsgenerated/flake.nixwithnixosConfigurations.<host>, each host pinned to its own nixpkgs baseline.nixos-rebuild switch --flake .#<host>(or nixos-anywhere) and it boots. Absent the block, knixl emits modules only and the assembly seam stays yours (ADR 0009). - Reference secrets without seeing them. A
(secret)"name"value wires a module option to a decrypted path (config.sops.secrets."name".path, or agenix), so the encrypted material stays out of band and knixl never reads or hashes the plaintext. - Bring your own modules. Declare
modules { module "x" flake="github:org/x" }inknixl.kdl; knixl resolves it to a pinned rev, caches the manifest, and records it in the lock, sogeneratestays offline and byte-reproducible. Precedence is built-in, then your localmodules/, then fetched, then stdlib, and a shadowed module is reported, never silent (ADR 0010). - Build installer media. Declare
installer "usb" { ... }inknixl.kdlwith an ordinary module tree; knixl emits an installer NixOS module (theinstallation-cdbase plus your modules) and, alongsidesystem {}, anix build .#usb-isooutput. Handy for a self-bootstrapping ISO that joins a tailnet sonixos-anywherecan reach a fresh box (ADR 0012).
The model in four points
- KDL is authoritative. Generated Nix is derived and disposable. There is no round-trip from edited Nix back to KDL (that is a tar pit, see ADR 0001).
- Override via the module system, not by editing generated files. Anything expressed as a NixOS option is overridable from a sibling module with
lib.mkForce/lib.mkAfter. Structural choices (which files, which modules) change at the KDL layer. - Escape hatch: a
raw-nixpassthrough node for inline snippets, or just import a hand-written.nixmodule alongside. knixl does not need to model all of Nix, only provide a clean seam. - Reproducible + version-aware:
output = f(kdl, tool_version, module_versions, formatter_version, oracle_rev), deterministic to the byte. A lockfile pins all five. Regeneration is a reconcile, and a version bump is opt-in and reviewable.
Commands
knixl check: CI gate. Exits 0 only if every generated file matches the lock. Never writes.knixl plan: recompute and report, write nothing.knixl generate: apply. Silent for input changes, refuses hand-edited (tainted) files without--accept-drift, refuses version skew (points you atupgrade).knixl upgrade: the only path that bumps recorded versions. Shows migration notes and a diff, applies on--yes.knixl doc <node>: typed reference for a module node, generated from its schema.knixl install <pkg>: add a package to a host. Drafts the KDL, verifies it under nix, previews, then regenerates.pkg@versionpins the package to a nixpkgs commit. On a real terminal it opens the TUI install screen; piped or--yesit uses a plain confirm.knixl tui: the interactive hub (shown above). Install a package, browse registered modules and their schemas, or author a new declarative module (build its schema and emit template, validated live as you type).
Drift and versions
A generated file that someone hand-edited is tainted. knixl tells drift apart from a stale input by a third hash, and refuses to silently overwrite a human's edit:
Key terms
- Clean : the generated file matches what its inputs and versions produce. Nothing to do.
- Stale : an input (KDL) changed, so a regeneration is owed.
generatefixes it. - Drifted : the generated file was hand-edited (tainted).
generaterefuses it without--accept-drift. - Missing : the file should exist and does not. Orphaned : it exists but no host claims it (deleted only with
--prune). - taint : the whole-file drift concept. A hand-edit taints the file, because a partial re-merge would lose the edit silently (ADR 0004).
- oracle : the NixOS option set knixl validates emitted paths against, built from a pinned nixpkgs rev (ADR 0003).
- baseline : a per-host nixpkgs rev. A host may declare
nixpkgs release="25.05", resolved to a commit at install/upgrade time (ADR 0007). - pin strategy : how a version-pinned package is emitted,
overrideorcommit-mix, chosen automatically at pin time (ADR 0005/0006).
Examples
examples/ holds worked hosts (db, web, shared, pinned, pinned-override, plus nas, gateway, vault, vmhost, workstation exercising the newer modules) with their golden Nix output under examples/expected/ and the matching lock. They double as the acceptance tests, and run with nothing beside them: every module they use is part of the embedded stdlib. See examples/README.md.
Status
1.0.0 is released (on crates.io, with prebuilt binaries). check, plan, generate, upgrade, doc, install, and tui work; every example host reproduces byte-for-byte through the pinned nixfmt; the oracle validates emitted paths against the NixOS option set; and the module set covers disks (disko), ZFS, users, OpenSSH, Tailscale, Incus, home-manager, nginx, and core host config (os: hostName, boot, kernel, sysctl, nix.settings, packages), plus nested NixOS system containers (guest) and installer-media/ISO generation. On top of that: opt-in bootable-system flake emission, reference-by-name secrets, and flake-based fetched modules. Design specs are under docs/superpowers/specs/; new work is tracked in GitHub issues.
Prior art
Nothing does KDL to committed Nix source. The ecosystem goes the other way (home-manager toKDL, niri-flake, the pkgs.formats.kdl request all generate KDL from Nix). Nickel exports to JSON/YAML/TOML, not Nix source. dhall-to-nix emits Nix but at eval time as normalised values, and cannot express callPackage, overlays, or the module system. terranix is the closest structural precedent (a DSL compiling to target config), just mirrored. See docs/00-overview.md for the full write-up.
Licence
Licensed under either of Apache License, Version 2.0 (LICENSE-APACHE) or the MIT licence (LICENSE-MIT) at your option. Unless you state otherwise, any contribution you submit for inclusion is dual licensed as above, with no additional terms.