knixl 1.9.0

Compile opinionated KDL into maintainable, committed NixOS module source.
knixl-1.9.0 is not a library.

knixl

CI crates.io licence

knixl generates maintainable, human-readable Nix from small amounts of opinionated KDL.

Documentation: knixl.dev/guide, starting with your first host.

Pronounced "nix-ull". You describe each machine in a few lines of KDL; knixl writes ordinary NixOS modules from it, checks them against the real NixOS options, and records a hash of everything in a lockfile, so the same KDL always gives the same Nix and a knixl upgrade can't change your output without showing 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 NixOS module (nginx enabled, TLS and proxy recommendations on, ACME wired up, security headers added), formats it with nixfmt, 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)

nix run github:1stvamp/knixl -- --help      # run without installing
nix profile install github:1stvamp/knixl    # install into your profile

Or add overlays.default and put pkgs.knixl in your environment.systemPackages (NixOS) or home.packages (home-manager).

crates.io

cargo install knixl

Prebuilt binary

Download a tarball for your platform from the latest release, or run the installer:

curl --proto '=https' --tlsv1.2 -LsSf https://github.com/1stvamp/knixl/releases/latest/download/knixl-installer.sh | sh

Built for Linux (gnu and musl) and macOS, on x86_64 and aarch64.

From source

git clone https://github.com/1stvamp/knixl && cd knixl
cargo build --release        # or: mise run build

The binary lands at target/release/knixl.

What it needs at runtime

A Nix formatter (nixfmt or nixfmt-rfc-style) on your PATH, and Nix for knixl install and for building the option set that validation checks against. generate and check work offline. Option validation checks against the NixOS option set for each host's nixpkgs, which knixl upgrade builds: see Validation and the oracle.

Quickstart

A knixl project is a directory with a hosts/ folder in it, one .kdl file per machine.

mkdir -p demo/hosts && cd demo
cat > hosts/web.kdl <<'EOF'
host "web" {
    system "x86_64-linux"
}
EOF

knixl plan        # missing generated/hosts/web.nix
knixl generate    # writes generated/hosts/web.nix and knixl.lock.kdl
knixl check       # clean

From there, add modules to the host and regenerate. Your first host walks through it, and on to a bootable system.

What comes with it

Modules for the common ground are built in, so there's nothing to install or copy before you use them:

  • os: boot loader, kernel, time zone, locale, sysctls, nix settings, system packages, tmpfiles rules.
  • user, openssh, firewall, tailscale: users with SSH keys, a hardened SSH server, open ports and trusted interfaces, and Tailscale with its auth key from a secret.
  • disko, zfs: declarative disk layouts (GPT, LUKS, swap, ZFS pools) and ZFS support.
  • web-service, security-headers, postgres, backups: an nginx reverse proxy with TLS, shared security headers, PostgreSQL, and restic backups.
  • incus, guest: an Incus host, and NixOS containers configured with knixl modules.
  • home-manager, nix-ld, package: home-manager for a user, running prebuilt binaries, and packages (with version pins, or from another flake input).
  • raw-nix and import: for the Nix no module covers.

knixl doc <node> prints what any of them accepts. Using modules has an example of each, and you can write your own as a short KDL file.

knixl can also write the flake.nix that builds your machines (so nixos-rebuild switch --flake works straight away), installer ISOs, and NixOS images for Incus. See From KDL to a running machine.

Commands

  • knixl plan: show what would change. Writes nothing.
  • knixl generate: write the generated Nix and the lockfile.
  • knixl check: for CI. Fails unless every generated file is up to date and unedited.
  • knixl upgrade: review and apply version changes (a new knixl, a nixpkgs release, flake inputs).
  • knixl doc <node>: what a module accepts.
  • knixl install <pkg>: add a package to a host, check it with Nix, and regenerate. pkg@version pins a version.
  • knixl tui: the interactive interface (shown above): add packages with a live preview, browse modules, and build your own.

If someone edits a generated file by hand, knixl notices and refuses to overwrite their change:

Day to day covers the everyday loop and what to do when check fails.

Examples

examples/hosts/ holds worked hosts, with the Nix knixl generates for each under examples/expected/. They're also on the site, side by side: knixl.dev/examples.

Status

Released and in use; the changelog lists what each version added. New work is tracked in GitHub issues. The design specs and decision records, for contributors, are under docs/ and at knixl.dev/design.

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.