# Use `mkFlake` in a consuming repo

Use `kixpkgs.lib.mkFlake` in the repository that owns your cluster definitions.
It exposes the cluster outputs that the Kix CLI discovers.

## Add the flake inputs

Create a `flake.nix` and declare nixpkgs and kixpkgs:

```nix title="flake.nix"
{
  description = "Application infrastructure";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    kixpkgs.url = "github:kix-run/kixpkgs";
    kixpkgs.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = inputs: inputs.kixpkgs.lib.mkFlake {
    inherit inputs;
    clusters.production = ./clusters/production.nix;
  };
}
```

Following the same nixpkgs input keeps package evaluation on one nixpkgs
revision.

## Register cluster files

The examples repository registers each cluster by name:

<Snippet {...mkFlakeOutputs} />

Each value under `clusters` is a cluster function. Its attribute name becomes
the CLI cluster name, while the file's `kix.buildCluster.name` becomes the
cluster identity recorded in manifests and activations.

:::caution
Keep the two the same. The Activation record is labelled with the
`buildCluster` name, and every command that reads cluster state filters on the
CLI name. If the names differ, `kix health`, `kix rollback --list`, `kix gc`
retention, and the baseline comparison in `kix deploy` cannot find the
Activation. These commands currently report an empty result instead of
identifying the mismatch.
:::

For a single project, a compact registration is enough:

```nix title="flake.nix"
outputs = inputs: inputs.kixpkgs.lib.mkFlake {
  inherit inputs;
  clusters.application = ./cluster.nix;
};
```

## Apply one setting to every cluster

`clusterModules` lists modules every cluster in the flake starts from, ahead
of the cluster's own. Use it for a setting the repository wants everywhere,
such as making scorecard findings fail the build:

```nix title="flake.nix"
outputs = inputs: inputs.kixpkgs.lib.mkFlake {
  inherit inputs;
  clusters.production = ./clusters/production.nix;
  clusterModules = [ { scorecard.maxSeverity = "error"; } ];
};
```

A cluster that needs a different value sets the option itself with
`lib.mkForce`.

## Lock the inputs

Create or update `flake.lock` after adding the inputs:

<Command commands={["nix flake lock"]} cwd="infrastructure/" />

Commit `flake.nix` and `flake.lock` together. Other machines and CI will then
evaluate the same Kix and nixpkgs revisions.

## Verify CLI discovery

List the clusters exposed by the flake:

<Command {...listClusters} />

If the flake is not in the current directory, pass it explicitly:

<Command commands={["kix list clusters --flake ./infrastructure"]} />

You can now use the discovered names with `check`, `build`, `diff`, `deploy`,
and the other cluster commands.

:::note[Reference]
See [`list`](/docs/reference/cli/list/) for the cluster and package discovery
commands.
:::