Skip to content

Your first host

This page builds a small project from an empty directory: one laptop, called laptop, with SSH and a user. It only generates the host module; the next page turns it into something you can deploy.

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

Terminal window
mkdir -p homelab/hosts && cd homelab
git init

Create hosts/laptop.kdl:

host "laptop" {
system "x86_64-linux"
}

host "laptop" names the machine (it also becomes its networking.hostName), and system is the Nix platform it runs on.

Terminal window
knixl plan
missing generated/hosts/laptop.nix

plan works out what the generated files should be and compares them with what’s on disk, without writing anything. Here it says the host’s file doesn’t exist yet.

Terminal window
knixl generate

This writes two things:

  • generated/hosts/laptop.nix, the NixOS module for the host.
  • knixl.lock.kdl, the lockfile: the knixl and formatter versions, the version of every module, and a hash of each input and output file.

The module is short so far:

# Generated by knixl 1.8.0 from hosts/laptop.kdl
# Do NOT edit. Regenerate from the KDL source.
# Overrides: add a sibling module and use lib.mkForce / lib.mkAfter.
{
config,
lib,
pkgs,
...
}:
{
nixpkgs.hostPlatform = "x86_64-linux";
networking.hostName = "laptop";
}

The header matters: generated files are knixl’s, and if you need to change something it produced, you do it in the KDL or in a separate module of your own (When knixl doesn’t cover it shows how).

Everything inside a host block apart from a few settings like system is a module node: a named block that a module turns into NixOS options. Let’s give the laptop SSH and a user:

host "laptop" {
system "x86_64-linux"
openssh {
permit-root "no"
}
user "wes" {
description "Wes Mason"
group "wheel"
ssh-key "ssh-ed25519 AAAA... wes@desktop"
}
}

How do you know openssh takes permit-root? Ask knixl:

Terminal window
knixl doc openssh
openssh: Hardened OpenSSH (password auth off) with port and login knobs.
Children:
port : int (repeated) Listen port(s) (services.openssh.ports). NixOS default [ 22 ] if omitted.
permit-root : node (repeated) PermitRootLogin value, e.g. "no" or "prohibit-password". At most one.
x11-forwarding : bool Enable X11 forwarding (off by default).

knixl doc works for every module, built in or your own. A child marked “repeated” with “At most one” in its description, like permit-root, is simply optional: write it once or leave it out. Using modules lists every module.

Now regenerate:

Terminal window
knixl plan
knixl generate

plan reports the host file as stale: its KDL changed, so the generated file is out of date. generate rewrites it, and the module now switches on OpenSSH with password logins off, and declares the wes user with the wheel group and the key.

If you get a node or a child name wrong, knixl refuses with exit code 5 and says where (hosts/laptop.kdl:8: unknown child `prot` ), and nothing is written.

Terminal window
knixl check
clean generated/hosts/laptop.nix

check is the command for CI. It recomputes everything, writes nothing, and exits non-zero if any generated file is out of date or has been edited by hand.

Commit the lot: hosts/, generated/ and knixl.lock.kdl. The generated Nix is meant to be committed, so reviewers see the real change a KDL edit makes, and so a machine can be rebuilt from the repository without knixl installed.

generated/hosts/laptop.nix is a normal NixOS module. If you already have a flake or a configuration.nix for the laptop, import it from there:

nixosConfigurations.laptop = nixpkgs.lib.nixosSystem {
modules = [
./hardware-configuration.nix
./generated/hosts/laptop.nix
];
};

This is a good way to adopt knixl gradually: move one service at a time out of your hand-written config into KDL, and keep the rest as it is. In this setup your flake decides which nixpkgs the machine builds from. If you give the host a nixpkgs release (next page) so knixl can validate it, keep that release and your flake’s nixpkgs input in step, since knixl checks against one and Nix builds with the other. If you’d rather knixl wrote the flake as well, carry on to From KDL to a running machine.