# Keep clusters on different versions of Kix

By default, every cluster in a project uses the `kix` input in `flake.nix`, so
`kix upgrade` upgrades all clusters together. To upgrade `staging` first and
keep `prod` on its current version until you are ready, add a second Kix library
input to `flake.nix` and map each cluster to an input in the `[versions]` table
of `kix.toml`.

:::note[Helpful background]
- [Upgrade a project to a new version of Kix](/docs/how-to/start-and-inspect/upgrade-a-project-to-a-new-kix/): preview release notes, fix build errors one minor release at a time, and acknowledge changes to stored data.
- [`kix.toml` reference](/docs/reference/kix-toml/#the-versions-table): rules and error messages for the `[versions]` table.
- [`kix upgrade` reference](/docs/reference/cli/upgrade/): upgrading an input, moving a single cluster, and `kix-upgrade.json`.
:::

## Add a second input and `[versions]`

Suppose your project uses Kix 0.12.3 and defines two clusters, `staging` and
`prod` (the version numbers on this page are examples). Every version of Kix in
the project must support `[versions]` (Kix 0.10.0 or later); earlier versions reject `versions` as an
unknown setting in `kix.toml`.

Every machine that runs `kix` for this project, including CI, also needs an
installed Kix on `PATH` that supports `[versions]` (run `kix self upgrade` to
update it). The Kix you start is what selects the version for each cluster. A
Kix from before `[versions]` always selects the version pinned by input `kix`,
and that version then refuses any cluster on another input because its Kix
library does not match.

First, add a second Kix library input (such as `kix-stable`) to `flake.nix` with
the same URL and tag as `kix`. Leave `outputs` unchanged. `mkFlake` still reads
`inputs.kix`, and Kix swaps in `kix-stable` automatically when building clusters
assigned to it:

```nix title="flake.nix"
{
  inputs.kix.url        = "github:kix-run/kix/v0.12.3";
  inputs.kix-stable.url = "github:kix-run/kix/v0.12.3";

  outputs = inputs: inputs.kix.lib.mkFlake {
    inherit inputs;
    clusters.staging = ./clusters/staging.nix;
    clusters.prod    = ./clusters/prod.nix;
  };
}
```

Next, add a `[versions]` table to `kix.toml` listing every cluster in the
project. List the clusters you want to upgrade first under `kix`, and the
clusters you want to keep on Kix 0.12.3 under `kix-stable`:

```toml title="kix.toml"
[versions]
kix        = ["staging"]
kix-stable = ["prod"]
```

Version numbers stay in `flake.nix` and `flake.lock`. Once `[versions]` is set
in `kix.toml`, every cluster in `flake.nix` must appear under one input, and
`kix` must always pin the newest version of Kix in the project.

Track both files in Git so Kix sees them during evaluation:

<Command commands={["git add kix.toml flake.nix"]} cwd="my-project/" />

Then lock the new input in `flake.lock`:

<Command commands={["kix upgrade kix-stable"]} cwd="my-project/" />

Because `kix-stable` was not in `flake.lock` yet, Kix locks it without building
anything:

```text
Pinned input `kix-stable` to Kix 0.12.3 (updated flake.lock). Input `kix-stable` had no previous version in flake.lock, so Kix built nothing to compare.
Commit flake.lock and kix.toml.
```

Commit `kix.toml`, `flake.nix`, and `flake.lock` before starting the upgrade.

## Upgrade `staging` first

Preview and run `kix upgrade` with no arguments:

<Command
  commands={[
    "kix upgrade --dry-run",
    "kix upgrade",
  ]}
  cwd="my-project/"
/>

`kix upgrade` upgrades only the `kix` input (for example, to Kix 0.14.2) and
builds only the clusters listed under `kix`:

```text
Upgrading Kix 0.12.3 -> 0.14.2
Input `kix` in flake.nix, used by staging
```

`kix-stable` stays on `v0.12.3` in `flake.nix` and `flake.lock`, so `prod` does
not change. If the upgrade includes a data migration that requires
`kix upgrade --acknowledge`, `kix deploy staging` is blocked until you
acknowledge it, while `kix deploy prod` still runs normally.

Review the manifest changes and release notes, commit `flake.nix`, `flake.lock`,
and `kix-upgrade.json`, and deploy `staging`:

<Command commands={["kix deploy staging"]} cwd="my-project/" />

## Check which version each cluster uses

List the clusters or run `kix doctor` to confirm each cluster's version and
input:

<Command
  commands={[
    "kix list clusters",
    "kix doctor",
  ]}
  cwd="my-project/"
/>

`kix list clusters` adds `KIX` and `INPUT` columns when `[versions]` is
present:

```text
 NAME     KIX     INPUT
 prod     0.12.3  kix-stable
 staging  0.14.2  kix
```

`kix doctor` prints a line for each input:

```text
✓ Project uses Kix 0.14.2 (flake.lock)
✓ Input kix pins Kix 0.14.2, used by staging
✓ Input kix-stable pins Kix 0.12.3, used by prod
```

When you run a cluster command such as `kix check prod`, `kix plan prod`, or
`kix deploy prod`, Kix looks `prod` up in `[versions]`, runs Kix 0.12.3
(installing it if needed), and builds `prod` with the Kix library from
`kix-stable`. Clusters on different versions of Kix can also deploy to the same
Kubernetes cluster. If two of them deploy the same object while only one has
upgraded to revisions, see
[Kix clusters on two versions of Kix](/docs/explanation/server-side-apply-and-drift/#kix-clusters-on-two-versions-of-kix)
for how Kix keeps that shared object compatible until both upgrade.

Every cluster in the repository still shares `flake.nix`, `kix.toml`, and any
imported `.nix` modules, so keep shared modules compatible with both versions of
Kix until all clusters have upgraded.

## Upgrade `prod` later

Once you are ready to upgrade the remaining clusters, choose either approach:

### Upgrade every cluster on `kix-stable` together

Run `kix upgrade` with the input name:

<Command commands={["kix upgrade kix-stable"]} cwd="my-project/" />

Without `--to`, this upgrades `kix-stable` in `flake.nix` and `flake.lock` to
match `kix` (Kix 0.14.2), builds every cluster under `kix-stable` before and
after, and shows the manifest changes and release notes. Pass `--to <version>`
to upgrade `kix-stable` to an intermediate version instead.

### Move one cluster at a time

If multiple clusters share `kix-stable` (such as `prod` and `eu`) and you want
to upgrade one at a time, pass the cluster name:

<Command commands={["kix upgrade prod --to kix"]} cwd="my-project/" />

`--to` names the target input (which must appear in `[versions]`, or be `kix`)
and defaults to `kix` when omitted. To move a cluster to a new input instead,
add `inputs.<input>.url` to `flake.nix` and `<input> = []` to `[versions]` in
`kix.toml`, run `kix upgrade <input>` to lock it, and then run
`kix upgrade <cluster> --to <input>`. Kix builds `prod` on `kix-stable`, moves
`prod` to `kix` in `kix.toml`, rebuilds `prod` on `kix`, and shows the manifest
changes and release notes:

```text
Cluster `prod`, from input `kix-stable` to input `kix`
Moved cluster `prod` to input `kix` on Kix 0.14.2 (updated kix.toml)
```

`flake.nix` and `flake.lock` stay unchanged. If `prod` fails to build on the new
version, Kix leaves `prod` under `kix` in `kix.toml` so you can fix the build
error and run `kix upgrade prod --to kix` again to finish.

Commit the changed files (`flake.nix` and `flake.lock` when upgrading an input,
or `kix.toml` when moving a cluster, along with `kix-upgrade.json`) and deploy
`prod`:

<Command commands={["kix deploy prod"]} cwd="my-project/" />

## Keep or remove the second input

Once `kix-stable` has no clusters left (`kix-stable = []`), you can leave it in
`flake.nix` and `kix.toml` for your next staged upgrade, or remove
`inputs.kix-stable` from `flake.nix` and delete `kix-stable` (or the whole
`[versions]` table) from `kix.toml`.