# Upgrade a project to a new version of Kix

A project names its version of Kix in the tag of its Kix library input,
which `flake.lock` records. `kix upgrade` moves it to a newer one. It builds every cluster before and after, so you see what the
new version changes before you deploy.

:::note[Helpful background]
- [Install and run Kix](/docs/how-to/start-and-inspect/install-and-run-the-kix-cli/#choose-the-version-of-kix-for-a-project): how Kix chooses the version a project uses.
- [Keep clusters on different versions of Kix](/docs/how-to/start-and-inspect/hold-clusters-on-different-kix-versions/): upgrade staging first while keeping production on its current version.
- [`kix upgrade` reference](/docs/reference/cli/upgrade/): every flag, the steps of a run, and `kix-upgrade.json`.
:::

## Where the version lives

The tag of the `kix` input in `flake.nix` names the project's version of
Kix, and `flake.lock` records it:

```nix
inputs.kix.url = "github:kix-run/kix/v0.10.0";
```

`kix upgrade` updates that tag and refreshes `flake.lock`. If `flake.nix`
contains another matching URL for the same repository and Kix cannot tell which
input owns it, `kix upgrade` stops and asks you to edit `inputs.kix.url`
yourself. To upgrade some clusters while keeping others on their current
version, see
[Keep clusters on different versions of Kix](/docs/how-to/start-and-inspect/hold-clusters-on-different-kix-versions/).

## Pin a project that has no version

When `flake.lock` pins no version of Kix, for example because the project has
no `flake.lock` or its `kix` input has no tag, Kix stops and asks you to pin
one. Run `kix upgrade --to` with the version the project uses now:

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

With no version to compare against, `kix upgrade` builds nothing. It writes
the tag into `flake.nix`, creates or updates `flake.lock`, and names the files
to commit. To try a prerelease, name it in full, such as `--to 0.11.0-rc.1`.
Pin the version the project uses now rather than a newer one. A later
`kix upgrade` then builds the project with both versions and shows what the
newer one changes.

## Upgrade a project from Kix 0.9

A project set up for Kix 0.9 names its version in a `kix` line in
`kix.toml`, and the Kix library input in `flake.nix` usually has no tag. Kix
now reads the version from `flake.lock`, and every command stops with error
`[K4476]` while the `kix` line is there. Move the project over in this order,
so that the last step shows what the new version changes.

First, install Kix 0.10 or later and check that `kix --version` prints it.
Kix 0.9 installs it with `kix self update`. You can also use the install script from
[Install and run Kix](/docs/how-to/start-and-inspect/install-and-run-the-kix-cli/).
Run every step below with the new version of Kix. `kix upgrade` in Kix 0.9 writes
the `kix` line back into `kix.toml`, so the next command stops with
`[K4476]` again.

Next, note the version in the `kix` line, such as `kix = "0.9"`, and delete
the line from `kix.toml`.

Then, if `flake.nix` names the Kix library input `kixpkgs`, rename it to
`kix`. Kix stops with error `[K9179]` until you do. Rename every use of the
input, including `inputs.kixpkgs.lib.mkFlake` and any
`kixpkgs.inputs.nixpkgs.follows` line:

```nix title="flake.nix"
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    kix.url = "github:kix-run/kix";
    kix.inputs.nixpkgs.follows = "nixpkgs";
  };

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

After that, pin the project to the version it uses now, the one the `kix` line
named. A minor version such as `0.9` pins its newest release:

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

This builds nothing. It adds the tag to the url of the `kix` input and
writes `flake.lock`. Commit `kix.toml`, `flake.nix`, and `flake.lock`.

Finally, move to the new version:

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

`kix upgrade` builds every cluster with the version you pinned and with the
new one, and lists what changed, as in [Run the upgrade](#run-the-upgrade).

## Preview the upgrade

Start from a clean git working tree, so that you can see each file the
upgrade changes. Then preview it:

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

The preview opens with the version the project uses and the release it would
move to, such as `Kix 0.10.0 -> 0.12.1 (preview, no files changed)`. It lists
the release notes that apply to your project under `Config changes required`,
`Behavior & data changes`, `Security fixes`, and `Notes`. It changes nothing.

To move to a release other than the newest, pass `--to`, such as
`--to 0.10` for the newest 0.10 release. To wait until a release has been out
for a while, set `min-age` for the project in `kix.toml`:

```toml
# kix.toml
[upgrade]
min-age = "7d"
```

## Run the upgrade

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

When every cluster builds with the new version, `kix upgrade` pins the project
to it in `flake.nix` and `flake.lock`. Read the `Manifest changes`
summary for each cluster, then commit the changed files with
`kix-upgrade.json`. When no object changed and nothing needs a review, the
run ends with `Upgraded to Kix 0.12.1. Ready to deploy.`

## Fix a build that fails

When a cluster does not build with the new version, `kix upgrade` moves one
minor release at a time and stops at the first release the cluster does not
build with. It prints the end of each error, the matching config changes,
and where the full build log is, and leaves the project pinned to that
release:

```text
Stopped at Kix 0.11.2 (step 1 of 2 toward 0.12.1).
flake.nix and flake.lock are pinned to 0.11.2 so you can fix this step first.
Next: fix the error above, then run `kix upgrade` to continue to 0.12.1.
```

Make the edit the release note describes, then run `kix upgrade` again. It
tries that release again and continues to the newest one.

## Review what needs a person

`kix upgrade` exits with status `3` when the project builds but you should
read what changed before you deploy. It ends with a numbered list under
`Upgraded to Kix 0.12.1, but review before deploying:`. An item names:

- objects that changed in any cluster; read them, or run
  `kix plan <cluster>` to see what deploying would change in the live cluster,
- objects marked `[review: <Kind>]`, such as a Secret, a ClusterRole, or an
  Ingress whose TLS settings changed,
- a change that alters behavior with no build error, or
- a release that every cluster must deploy before the project upgrades
  further. Commit the step, deploy every cluster, then run `kix upgrade`
  again. `kix deploy` refuses a cluster that would skip that release.

To see the field changes for a cluster, compare the new build with the
cluster before you deploy:

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

## Acknowledge a change to stored data

When a change moves or reinterprets the data a package stores, `kix deploy`
blocks deploys for the upgraded clusters until someone acknowledges the change.
The error prints the release note. Read it, take any backup it asks for, and
acknowledge the change by ID:

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

Commit `kix-upgrade.json`, so that whoever deploys next sees the
acknowledgement.