# `graph`

`kix graph` evaluates a cluster and prints the dependency graph between its
rendered resources. It does not contact a cluster. An edge means one resource
must be applied and ready before another. Edges come from the
`kix.run/depends-on` annotations evaluation writes, which record values a
resource reads from another (`out` references) and explicit `requires` or
`extraDeps`.

This page is hand-maintained. Check `cli/kix-cli/src/cli.rs`,
`cli/kix-cli/src/commands/graph.rs`, and `cli/kix/src/cluster/graph.rs` when
in doubt.

## Synopsis

```text
kix graph <CLUSTER> [-o tree|dot|json|yaml] [--flake <FLAKE>] [--no-cache]
```

| Argument or flag | Default | Meaning |
| --- | --- | --- |
| `<CLUSTER>` | | Cluster name to evaluate. |
| `-o`, `--output` | `tree` | `tree`, `dot`, `json`, or `yaml`, described below. `yaml` prints the JSON document as YAML. |
| `--flake <FLAKE>` | `.` | Flake to evaluate. |
| `--no-cache` | Off | Evaluate from Nix instead of reading the [eval cache](/docs/reference/cli/eval-cache-behavior/). |

After the graph, a line on stderr gives the resource and dependency counts;
`-q` hides it.

## Formats

| Format | Content |
| --- | --- |
| `tree` | Each root is a resource with no dependencies. Beneath each resource are the resources that depend on it, so a resource with several dependencies appears under each of them. A repeated path is marked `(circular ref)`. A cluster with no resources prints `(no resources)`. |
| `dot` | A `digraph` with one box per resource. Edges point from the dependent resource to its dependency. Import PackageInstances are dashed and grey. |
| `json` | `{ "nodes": [...], "edges": [...] }`. Each node has `id`, `kind`, `group` (omitted for core `v1` kinds), `name`, `namespace`, `identityHash`, and `isImport`. Each edge has `from` (the dependent) and `to` (the dependency), both node `id`s. |

Resource IDs have the form `Kind.group/name@namespace`, such as `Deployment.apps/web@shop`. Core `v1` kinds omit `.group` (`Service/web@shop`), and cluster-scoped objects omit `@namespace` (`Namespace/shop`). The PackageInstance that
stands for an
[import](/docs/how-to/adopt-existing-resources/import-an-existing-service-with-kix-mkimport/)
is labelled `(import)` in the tree and DOT output and has `isImport: true` in
JSON.

## Examples

<Command commands={[
  "kix graph demo",
  "kix graph demo -o dot | dot -Tsvg > graph.svg",
  "kix graph demo -o json",
]} />

## Exit status

`0` when the cluster evaluates, `1` when it does not.

:::tip[Use it in a task]
See [Visualize the dependency graph with `kix graph`](/docs/how-to/debug-dependency-wiring/visualize-the-dependency-graph-with-kix-graph/)
for reading the tree and rendering the DOT output.
:::