# Package anatomy

A Kix package is a reusable blueprint for an application or cluster service. It declares the settings operators can customize (`options`), the other services it needs (`build` arguments), and the Kubernetes objects it creates (`build`). Reach for this page when you are writing a new package from scratch or adding typed configuration options to an existing one.

:::note[Helpful background]
- [07. Package anatomy and safe resource composition](/docs/tutorials/07-package-anatomy-and-safe-resource-composition/): walk through a complete Kix package from top to bottom.
- [09. Typed package options](/docs/tutorials/09-typed-package-options/): learn how to declare typed options with `lib.mkOption` and catch invalid settings at build time.
- [Personas, packages, and clusters](/docs/explanation/packages-and-clusters/): see how packages separate application authoring from cluster-specific configuration.
- [Module system (nix.dev)](https://nix.dev/tutorials/module-system/): review the NixOS `lib.mkOption` and `lib.types` system that Kix packages use for option declarations.
:::

```nix
# pkgs/echo/default.nix
{
  scope,
  lib,
  kix,
  availability,
  ...
}:

{
  meta = {
    version = "1.10.0";
    description = "HTTP echo server for testing";
    availability.max = "node";
  };

  options = {
    image = kix.options.image {
      repository = "hashicorp/http-echo";
      tag = "1.0.0";
      digest = "sha256:fcb74f691c8b0bd1ebff992c0a3ce2bf29f61ccb2f3d85fe72c4bd8b34e05061";
    };
    replicas = lib.mkOption {
      type = lib.types.ints.positive;
      default = kix.availability.pick availability {
        none = 1;
        pod = 2;
        node = 3;
        zone = 3;
      };
      description = "Number of echo server pods.";
    };
    resources = kix.options.resources {
      requests = {
        cpu = "50m";
        memory = "64Mi";
      };
      limits.memory = "64Mi";
    };
    ingress = kix.options.ingress;
  };

  build = [
    (
      { self, config }:
      {
        deployment = scope.mkDeployment {
          name = "echo";
          spec = {
            replicas = config.replicas;
            selector.matchLabels = scope.selectorLabels;
            template.spec.containers = [
              {
                name = "echo";
                image = kix.image.ref config.image;
                ports = [ (kix.port "http" 5678) ];
                resources = config.resources;
              }
            ];
          };
        };

        service = scope.mkResource (
          kix.service.fromWorkload { name = "echo"; } self.deployment
        );

        root = self.service;
      }
    )

    (kix.expose { service = "service"; })
  ];
}
```

## Root function and return shape

A package file is either a Nix function taking system arguments or a plain attribute set containing a `build` attribute.

When written as a function, Kix calls it with four Phase 1 arguments before evaluating `options`:

| Outer argument | Type | Description |
| --- | --- | --- |
| `scope` | attrset | Instance-scoped builders (`mkResource`, `mkDeployment`, `mkSecret`, `mkCRD`, `forNamespace`, `selectorLabels`, and related helpers). |
| `lib` | attrset | Standard `nixpkgs.lib`. |
| `kix` | attrset | Framework helpers (`kix.options`, `kix.availability`, `kix.image`, `kix.expose`, `kix.monitor`, `kix.mount`, `kix.contracts`, and related helpers). |
| `availability` | string | Effective availability level for the instance (`"none"`, `"pod"`, `"node"`, or `"zone"`), clamped to `meta.availability.max`. Map it to replica counts or settings with `kix.availability.pick availability { none = ...; pod = ...; node = ...; zone = ...; }` and `kix.availability.atLeast "<min>" availability`. |

Include `...` in the outer function header (`{ scope, lib, kix, ... }:`) so the package accepts future framework arguments. Do not declare cross-package dependencies in the outer function header; declare them in `build` entries instead.

### Top-level package attributes

The package returns an attribute set with the following top-level keys:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `meta` | attrset | No | Package metadata, platform role declarations, availability ceiling, compatibility bounds, and network intents. |
| `options` | attrset | No | NixOS module option declarations (`lib.mkOption` or `kix.options.*`) exposed to `instances.<name>.config`. |
| `config` | attrset or module function | No | Package-level configuration module merged with `options`. Values inside `config` that the author wraps in `lib.mkDefault` are lowered from priority `1000` to `1250` (between option defaults at `1500` and instance or injected `lib.mkDefault` values at `1000`). |
| `deps` | attrset | No | Per-dependency configuration injection (`config`), `warnings`, and `assertions` keyed by `build` argument name. |
| `build` | function or list of functions | Yes | One or more functions returning named resource parts and a `root` entry. |
| `warnings` | `{ config, self, deps, clusterConfig }: [ string or attrset ]` | No | Warnings shown when Kix evaluates the cluster or reads a bundle with `--bundle`. |
| `assertions` | `{ config, self, deps, clusterConfig }: [ { assertion; message; fix ? null; id ? null; option ? null; } ]` | No | Evaluation-time assertions checked before building manifests. |

### The `root` entry and part reachability

Across all `build` entries, the merged parts attribute set must define `root`. `root` designates the primary entry point of the package and determines what downstream packages receive when they depend on this instance.

`root` accepts either a direct resource or an extended attribute set:

```nix
# Direct resource
root = self.service;

# Extended root with extra dependencies, post-deploy hooks, and custom outputs
root = {
  resource = self.service;
  requires = [ self.migrationJob ];
  postHooks = [ self.smokeTest ];
  out = {
    endpoint = "http://${self.service.out.fqdn}:8080";
  };
};
```

| `root` field | Type | Default | Description |
| --- | --- | --- | --- |
| `resource` | resource | Required | Primary resource of the package. Must also be assigned to a named attribute in `build` (for example, `service = ...; root = self.service;`). |
| `requires` | list of resources | `[ ]` | Additional resources that must be ready before the package instance is considered ready. |
| `postHooks` | list of resources | `[ ]` | Post-deployment resources (such as `kix.healthCheck` jobs) that run after `root.resource` and `requires` are ready. |
| `out` | attrset | `{ }` | Extra fields merged onto `root.resource.out` and promoted to the top level of the dependency object passed to consumers. |

Every resource defined in `build` must be reachable from `root.resource`, `root.requires`, or `root.postHooks` (either because `root` depends on it through `.out` string context or `requires`, or because it depends on a reachable resource such as an RBAC `RoleBinding` referencing a reachable `ServiceAccount`). Resources that are disconnected from `root` emit an orphaned-resource warning during evaluation.

`root.requires` and a resource's own `requires` order different things. A resource's `requires` becomes a `kix.run/depends-on` annotation on that resource, so deploy applies it after the listed resources are ready. `root.requires` extends the dependencies of the package's `PackageInstance`, so consumers of the package wait for the listed resources, but nothing inside the package does. An operator that watches kinds its own package defines therefore puts the CRDs in the controller's `requires` (`requires = builtins.attrValues self.crds;` on the `mkDeployment` call). With the CRDs only in `root.requires`, a first deploy can start the controller before the API server serves its kinds, and the controller crashes and restarts until it does. The scorecard rule `architecture.controllerRequiresCrds` warns about this shape.

---

## meta

`meta` declares static package properties that Kix inspects during dependency binding, cluster validation, and scorecard evaluation.

```nix
meta = {
  version = "2.51.0";
  description = "Prometheus monitoring system and time series database";
  roles = [ "metricsApi" ];
  defaultAliases = [ "monitoring" ];
  availability.max = "node";
  podSecurity = "restricted";
  lifecycle = "stateful";
  owner = "platform-observability";
  annotationDomains = [ "monitoring.coreos.com" ];
  compatibleWith.grafana = {
    minVersion = "10.0.0";
    reason = "Requires Grafana 10+ data source provisioning format";
  };
  k8s = {
    minVersion = "1.28";
    reason = "The CRDs use a validation rule added in Kubernetes 1.28";
    supported = {
      minVersion = "1.30";
      maxVersion = "1.32";
      source = "the compatibility table in the upstream README";
    };
  };
};
```

### Summary of `meta` fields

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `version` | string | None | Upstream software version the package deploys by default (`[vV]?<digits>(.<digits>)*[-<suffix>][+<build>]`). An instance reports it unless the package builds an `out.version`. See [The version an instance reports](#the-version-an-instance-reports). |
| `description` | string | None | Non-empty one-line summary of the package. Checked by the `governance.hasDescription` scorecard rule (`info` severity). |
| `roles` | list of strings | `[ ]` | Platform capabilities provided by this package. |
| `defaultAliases` | list of strings | `[ ]` | Default dependency-injection names registered when `instances.<name>.aliases` is empty. |
| `availability` | `{ max; reason?; }` | None | Highest availability level the package supports (`max`: `"none"`, `"pod"`, `"node"`, or `"zone"`), plus an optional `reason` string shown when Kix caps availability or rejects multiple replicas under `"none"`. |
| `compatibleWith` | attrset of `{ minVersion?; maxVersion?; reason?; }` | `{ }` | Version bounds for upstream dependencies keyed by dependency argument name. |
| `k8s` | `{ minVersion?; maxVersion?; reason?; supported?; }` | None | Kubernetes version bounds checked against [`k8sVersion`](/docs/reference/cluster-configuration/). Top-level `minVersion` and `maxVersion` fail the build outside that range; `supported = { minVersion?; maxVersion?; source?; }` emits a warning without stopping the build. |
| `conflictsWith` | list of strings or `{ key; reason; }` | `[ ]` | Packages that cannot coexist with this package (`"<name>"` for same-namespace, `"cluster:<name>"` for cluster-wide), with an optional `reason` included in conflict errors. |
| `podSecurity` | `"privileged"` \| `"baseline"` \| `"restricted"` | None | Pod Security Standards enforcement level applied to the instance namespace. |
| `unsafe` | list of strings | `[ ]` | Acknowledged security or architectural exceptions (`"raw-pvc"`, `"host-network"`, `"privileged"`, `"run-as-root"`, `"cluster-admin"`). |
| `annotationDomains` | list of strings | `[ ]` | Annotation domain prefixes (such as `"cert-manager.io"`) owned and consumed by this package. |
| `dnsPeers` | list | unset | DNS endpoints allowed on UDP and TCP port 53 by generated network policies when this package claims the `"dns"` role. Each entry is `{ namespace?; podSelector; }` (`namespace` defaults to the instance's namespace) or `{ cidr; }` (rejected by the `cilium` enforcer). When unset, policies allow pods labeled `k8s-app: kube-dns` in `kube-system`. See [DNS peers](/docs/explanation/network-policy/#dns-peers). |
| `network` | attrset | `{ }` | Ambient network intents (`intents.<part>.{egress,ingress}`) and `externalIngressEntities`. |
| `lifecycle` | `"stateful"` | None | Marks the `PackageInstance` with `kix.run/lifecycle: stateful`. |
| `scope` | `"cluster"` \| `"namespace"` | unset | `"cluster"`: consumers in every namespace find an instance by name, and auto-instantiation places the package once per cluster in `autoNamespace` or the catalog entry's `namespace` (without either, evaluation fails; declare the instance under `instances` or set `autoNamespace`). `"namespace"`: an instance is never used from another namespace, not even with `ref`. Unset: an instance in another namespace is used only when the consumer names it with `deps.<arg> = ref.<namespace>.<instance>`. Packages that claim a role or declare a `cluster:` conflict still serve the whole cluster, as do instances that Kix creates from `availablePackages` or enables via `optional = true` to satisfy a dependency. A `kix.mkImport` stand-in defaults to `"cluster"`. |
| `owner` | string | None | Team or owner identifier used by `governance.hasOwner` and `byOwner` policy rules. Built-in packages use `"platform"` for cluster infrastructure and `"app-team"` for applications, so you can set different rule severities for each. |
| `deprecated` | `{ message; replacement?; }` | None | Emits an evaluation warning when the package is instantiated. |

### `meta.version`

Set `meta.version` to the software version the package deploys by default, such as `"1.30.6"` for nginx 1.30.6. Define the version once in a local `release` variable and reuse it for the default image tag so the two stay in sync. If the upstream image tag starts with `v` (such as `"v1.12.1"`), pass `release` directly to `meta.version`. Kix strips the leading `v` when reading `meta.version`, so `kix list packages`, the `PackageInstance` record, and `compatibleWith` all see `"1.12.1"`, matching `kix.image.version`:

```nix
let
  release = "v1.12.1";
in
{
  meta = {
    version = release;
    ...
  };

  options.image = kix.options.image {
    repository = "quay.io/prometheus/node-exporter";
    tag = release;
    digest = "sha256:1b4e4438faca4dd7e001dd445d161a4a2091b0fededa84093b3a8dfeae1f1be0";
  };

  ...
}
```

Omit `meta.version` when the package runs no software of its own, such as a package that only creates Secrets or custom resources for a separate controller. Without `meta.version`, `kix list packages` shows `-`, and the instance's `PackageInstance` record omits `spec.version` unless `out.version` or the instance's `version` option is set.

Version strings must match `<digits>(.<digits>)*` with an optional leading `v` or `V`, an optional `-<suffix>`, and an optional `+<build>` (for example, `"1.28.2"`, `"v2.4.0-rc1"`, `"1.31.2-gke.1209000"`, or `"1.31.2+rke2r1"`).

When comparing versions in `compatibleWith` and `k8s`:
- Numeric segments are compared as integers from left to right.
- A `-<suffix>` whose first dot-separated identifier starts with `alpha`, `beta`, `rc`, `pre`, `dev`, or `preview` is treated as a pre-release and sorts **before** the corresponding release (`1.0.0-rc1 < 1.0.0`).
- All other `-<suffix>` values (such as `-r0`, `-debian-12`, or `-gke.1209000`) and `+<build>` suffixes are treated as vendor build metadata and compare **equal** to the base version (`1.0.0-debian-12 == 1.0.0`).

### The version an instance reports

`meta.version` is fixed for the package, and an instance can run another image. Kix works out one version for each instance and uses it in two places: `spec.version` on the instance's `PackageInstance` record, which `kix plan` shows, and the `meta.compatibleWith` checks of the packages that depend on the instance. Kix takes the first of these that is set:

1. The instance's [`version`](/docs/reference/instances-and-composition/) option.
2. The `out.version` the package builds on its `root`.
3. The package's `meta.version`.

A package whose image a user can change publishes `out.version` with [`kix.image.version`](/docs/reference/composition-helpers/#image-and-pin-helpers), which reads the version from the image tag:

```nix
root = {
  resource = self.service;
  out.version = kix.image.version config.image;
};
```

A package that runs several images reads the image of its main software. When the tag names no version, such as `latest`, `out.version` is `null`. The record then carries no version, and a `meta.compatibleWith` check warns that it cannot compare. Set the instance's `version` option to state the version yourself.

`kix list` shows the package's `meta.version`, because it reads packages without building instances.

### `meta.roles` and `meta.defaultAliases`

`meta.roles` registers the instance as a provider of one or more cluster roles from a closed vocabulary:

| Role | Required `out` fields | Effect |
| --- | --- | --- |
| `"cni"` | None | Satisfies the cluster CNI capability check. |
| `"dns"` | `fqdn` | Provides cluster DNS resolution; workloads automatically receive a dependency and network egress rule to the DNS service. |
| `"gatewayApi"` | `crds` | Provides the `gateway.networking.k8s.io` CRD family to Gateway API packages (`envoy-gateway-controller`, `envoy-gateway`, `traefik`). |
| `"loadBalancer"` | None | Satisfies `Service` resources of `type = "LoadBalancer"`. |
| `"metricsApi"` | None | Satisfies consumers that call `scope.requireMetricsApi` (such as `HorizontalPodAutoscaler`). |
| `"network-policy-enforcer"` | `netpol` | Enables cluster-wide `NetworkPolicy` enforcement when `cluster.networkPolicy.enable = true`. |
| `"registryMirror"` | None | Registers a cluster container registry mirror. |
| `"snapshotController"` | `crds` | Provides the `VolumeSnapshot` CRD family for backup and restore workflows. |
| `"storageClasses"` | `storageClassName` | Provides a dynamic `StorageClass` for `PersistentVolumeClaim` resources. |

Each role in `meta.roles` also registers that role name as a dependency alias so other packages can request `{ dns ? null }:` or `{ loadBalancer ? null }:`. At most one active instance in a cluster may provide a given role unless disambiguated.

`meta.defaultAliases` provides additional dependency-injection names when the instance does not set `instances.<name>.aliases`. If an instance sets an `aliases` list, even an empty one, `defaultAliases` are replaced while `meta.roles` remain active.

### `meta.availability`

`meta.availability.max` declares the highest availability topology the package architecture supports (`"none"`, `"pod"`, `"node"`, or `"zone"`). When a cluster requests a higher level (for example, `"zone"`), Kix clamps `availability` inside the package to `meta.availability.max` so replica counts and `topologySpreadConstraints` never exceed what the software can safely run.

Individual workloads can further clamp their own ceiling by passing `availability.max` to `scope.mkDeployment`, `scope.mkStatefulSet`, `scope.mkJob`, or `scope.mkCronJob`.

Add `reason` alongside `max` (in `meta.availability` or on a workload builder) to explain why the software cannot run at a higher availability level. Kix includes `reason` when it warns that an instance or workload was capped at `max`, and when it rejects a workload with more than one replica under `max = "none"`. Write `reason` as one or more complete sentences; Kix prints `Reason: <reason>` and adds a final period if missing:

```nix
availability = {
  max = "none";
  reason = "The service stores state in a single SQLite file that supports only one writer at a time.";
};
```

### `meta.compatibleWith` and `meta.k8s`

`meta.compatibleWith.<dep>` checks the `meta.version` of the bound `<dep>` provider at evaluation time:

```nix
meta.compatibleWith.postgres = {
  minVersion = "15";
  maxVersion = "17";
  reason = "Uses PostgreSQL 15+ logical replication syntax";
};
```

Bounds are inclusive (`minVersion <= actual <= maxVersion`) and compare only up to the segment precision of the bound. For example, `maxVersion = "1.29"` accepts `"1.29.4"`, while `maxVersion = "1.29.0"` rejects `"1.29.4"`. `meta.k8s` uses the same comparison rules against `k8sVersion`.

`meta.k8s` holds two ranges:

- Top-level `minVersion` and `maxVersion` fail the build when `k8sVersion` falls outside them. Set these bounds only when the package is known to fail beyond them, such as when a workload reads an API version that older Kubernetes releases do not serve or a CRD uses a validation rule added in a newer Kubernetes release, and explain what fails in `reason`. When a package can disable just the unsupported feature on older clusters, compare `clusterConfig.k8sVersion` with [`kix.version.atLeast`](/docs/reference/composition-helpers/#version-helpers) in `build` instead of setting a hard bound.
- `supported` records the Kubernetes versions the upstream project lists as supported or tests in CI, and `source` names where upstream states that range. When `k8sVersion` falls outside `supported`, the build continues and emits a warning with the range and its source.

When a cluster leaves `k8sVersion` unset, Kix skips both checks without warning.

```nix
meta.k8s = {
  minVersion = "1.36";
  reason = "Reads admissionregistration.k8s.io/v1 MutatingAdmissionPolicy, added in Kubernetes 1.36";
  supported = {
    minVersion = "1.36";
    maxVersion = "1.36";
    source = "the compatibility matrix in the upstream README";
  };
};
```

Each Kix release supports a range of Kubernetes versions (1.36 and 1.37 in this release). Every stable package accepts all versions in that range, so a cluster on Kubernetes 1.36 or 1.37 builds with any combination of stable packages. Setting `k8sVersion` outside that range emits a warning without stopping the build.

### `meta.conflictsWith`

Entries in `meta.conflictsWith` prevent incompatible packages from being deployed together:
- `"<pkg>"` forbids another instance of `<pkg>` in the **same namespace**.
- `"cluster:<pkg>"` forbids `<pkg>` anywhere in the **same cluster**.
- Listing the package's own name in `"cluster:<self>"` enforces a cluster-wide singleton.

An instance counts when it answers `<pkg>` by its name or its aliases, or when its package lists `<pkg>` in `meta.defaultAliases`. The last case holds even when the instance sets its own `aliases`, so two instances of a singleton package fail whatever aliases they set.

An entry can also be `{ key; reason; }`, where `key` is `"<pkg>"` or `"cluster:<pkg>"` and `reason` (one or more complete sentences) explains why the instances cannot coexist. Kix prints `Reason: <reason>` in the conflict error:

```nix
conflictsWith = [
  {
    key = "cluster:tailscale";
    reason = "The operator watches cluster-wide tailscale.com resources, Services with loadBalancerClass \"tailscale\", and Ingresses with IngressClass \"tailscale\" across all namespaces, so two instances would compete for the same objects.";
  }
];
```

### `meta.podSecurity` and `meta.unsafe`

Setting `meta.podSecurity` to `"restricted"`, `"baseline"`, or `"privileged"` applies the corresponding `pod-security.kubernetes.io/enforce` label to the generated `Namespace` resource. When multiple instances share a namespace, the least restrictive level wins (`privileged` > `baseline` > `restricted`).

`meta.unsafe` acknowledges intentional security or isolation trade-offs so the scorecard records them as reviewed grants (`architecture.unsafeGrants`) rather than unexpected violations:

| `meta.unsafe` token | What it permits |
| --- | --- |
| `"raw-pvc"` | Constructing a raw `PersistentVolumeClaim` via `scope.mkResource` instead of a managed storage operator. |
| `"host-network"` | Setting `hostNetwork = true`, `hostPID = true`, or `hostIPC = true` on a pod spec. |
| `"privileged"` | Setting `securityContext.privileged = true` on a container. |
| `"run-as-root"` | Running containers with `runAsNonRoot = false` or `runAsUser = 0`. |
| `"cluster-admin"` | Binding a `ServiceAccount` to the built-in `cluster-admin` `ClusterRole` or granting wildcard `*` verbs and resources. |

### `meta.network`

When `cluster.networkPolicy.enable = true`, Kix derives `NetworkPolicy` resources from the dependency graph. Workloads that need ambient network access not represented by a Kix resource dependency (such as scraping all pods or calling external webhooks) declare that access in `meta.network`:

```nix
meta.network = {
  intents.deployment = {
    egress = [
      {
        to = "apiserver";
      }
      {
        to = "world";
        ports = [ { port = 443; protocol = "TCP"; } ];
      }
    ];
    ingress = [
      {
        from = "all-pods";
        ports = [ { port = 9090; protocol = "TCP"; } ];
      }
    ];
  };
  externalIngressEntities = [ "world" ];
};
```

- `intents.<partName>.egress`: list of rules where `to` is `"world"`, `"pods"`, `"all-pods"`, `"apiserver"`, `"dns"`, `"node-local"` (with required `cidr`, for a link-local or metadata address on the pod's own node), or `{ entities = [ ... ]; }`, with optional `ports = [ { port; protocol ? "TCP"; } ]`. `"pods"` allows traffic only to pods. `"all-pods"` means all pods in the cluster; although the `cilium` enforcer maps `"all-pods"` to Cilium's `cluster` entity (which also includes cluster nodes and the Kubernetes API server), any rule that also needs to reach cluster nodes, host-network peers, or NodePort traffic SNATed by a node should declare `{ to = "world"; entities = [ "cluster" ]; }` (or `from = "world"` for ingress) explicitly.
- `intents.<partName>.ingress`: list of rules where `from` is `"world"`, `"all-pods"`, or `{ entities = [ ... ]; }`, with optional `ports`.
- `externalIngressEntities`: list of entity names (such as `["world"]` or `["remote-node"]`) used when this package acts as a cluster ingress controller.
- `entities` (on an intent rule) and `externalIngressEntities` accept only `"world"`, `"host"`, `"remote-node"`, `"kube-apiserver"`, `"cluster"`, and `"all"`. Any other entity name fails evaluation with `[K2060]`. Each network policy enforcer maps these names to its own rule format.

---

## options

`options` defines the configuration schema for `instances.<name>.config` using standard NixOS module options (`lib.mkOption`) or [`kix.options.*` helpers](#reusable-option-sets).

```nix
options = {
  logLevel = lib.mkOption {
    type = lib.types.enum [ "debug" "info" "warn" "error" ];
    default = "info";
    description = "Minimum log severity emitted by the server.";
  };

  extraEnv = lib.mkOption {
    type = lib.types.attrsOf lib.types.str;
    default = { };
    description = "Additional environment variables passed to the main container.";
  };
};
```

### Evaluation order and module priority

When Kix evaluates an instance, it merges configuration layers using the NixOS module system in the following priority order (lowest number wins):

1. **Option defaults** (`options.<name>.default`): priority `1500`.
2. **Package `config` defaults** (`lib.mkDefault` values inside the top-level `config` block in the package file): lowered from priority `1000` to priority `1250` so package-level computed defaults override option defaults (`1500`) while yielding to dependency injections and instance config (including instance `lib.mkDefault` at `1000`). Unwrapped definitions in the package `config` block keep normal module priority (`100`).
3. **Dependency config injections** (`deps.<arg>.config` from downstream consumers) and **instance `config`** (`instances.<name>.config`): priority `100` (or `lib.mkDefault` at `1000`, `lib.mkForce` at `50`).

Both the package `config` block and instance `config` modules can take `{ config, lib, clusterConfig, ... }:` as module arguments to compute values from sibling options or cluster settings.

### The option default contract

Every package in a flake checked by `kix.mkFlake` or `kix.checkPackageOptions` must obey three rules on `options` defaults so users can override individual keys without unexpectedly wiping sibling defaults:

1. **No untyped options with non-empty attrset defaults.** An option without a `type` (or typed as `lib.types.anything` / `lib.types.raw`) cannot have a non-empty attribute set as its `default`, because setting one subkey in `instances.<name>.config` replaces the entire attribute set. Use a submodule (`lib.types.submodule`) instead.
2. **No `attrsOf` or `lazyAttrsOf` with non-empty defaults.** `lib.types.attrsOf` replaces the default map wholesale when a user sets a single key unless merged carefully, and prevents per-key schema documentation. Define known keys as explicit options or put user extensions in `extra*` options with `default = { };`.
3. **Submodule defaults must match field defaults.** If a `lib.types.submodule` option has `default = { foo = "bar"; };`, the `foo` option inside that submodule must also declare `default = "bar";` so specifying another field in the submodule does not drop `foo`.

---

## Reusable option sets

`kix.options.*` provides pre-typed option schemas that pair directly with composition helpers like `kix.expose`, `kix.monitor`, and `kix.healthCheck`.

### `kix.options.*` helpers

| Helper | Arguments / shape | Generated options and types |
| --- | --- | --- |
| `kix.options.image` | `{ repository ? null, tag ? null, digest ? null, pullPolicy ? "IfNotPresent", tagText ? null, description ? ... }` | Submodule option with `repository` (`str`), `tag` (`str`), `digest` (`nullOr str`, defaults to `digest` while `config.tag == tag`, otherwise `null`), `pullPolicy` (`nullOr str`, default `pullPolicy`), plus pin record-keeping options `sourceRev`, `promotedBy`, `resolvedAt`, and `note` (all `nullOr str`, default `null`). Consumed by `kix.image.ref`. |
| `kix.options.resources` | `{ requests ? { }, limits ? { }, description ? ... }` | Submodule option with `requests` and `limits` (each supporting `cpu`, `memory`, and `ephemeral-storage` defaults plus freeform `hugepages-<size>` or extended `<domain>/<name>` quantities validated by `kix.types.quantity`; setting a field to `null` clears its default) and `claims` (`nullOr (listOf (submodule { name; request ? null; }))`). Cleaned via `apply` so it passes directly to a container's `resources` field. When [`cluster.defaultLimits.cpu` or `cluster.defaultLimits.memory`](/docs/reference/cluster-configuration/#default-resource-limits) is `"none"`, `kix.options.resources` (from the package's `kix` argument) drops that default limit while keeping any limit set on the instance. |
| `kix.options.env` | option value (not a function) | `lib.mkOption` of type `attrsOf (nullOr envValue)` with `default = { }`, where each value is a string, a `{ valueFrom = ...; }` source, or `null` (to clear a package default). Consumed by `kix.mergeEnv`. |
| `kix.options.ingress` | option attrset (not a function) | Attribute set of options `{ host; extraHosts; annotations; timeouts; }`: `host` (`nullOr str`, default `null`), `extraHosts` (`listOf str`, default `[ ]`), `annotations` (`attrsOf str`, default `{ }`), and `timeouts` (`nullOr (submodule { request; backendRequest; })` with `nullOr str` fields, default `null`). Assign as `options.ingress = kix.options.ingress;`; read by `kix.expose` from `config.ingress`. |
| `kix.options.monitor` | option attrset (not a function) | Attribute set of options `{ enabled; interval; }` where `enabled` is `bool` (default `true`) and `interval` is `nullOr str` (default `null`). Assign as `options.metrics = kix.options.monitor;`; read by `kix.monitor` from `config.metrics`. |
| `kix.options.alerts` | option attrset (not a function) | Attribute set `{ enabled; }` where `enabled` is `bool` (default `true`), for packages that render alert rules without a `kix.monitor` scrape. Assign as `options.metrics = kix.options.alerts;`; read by `kix.alerts` from `config.metrics.enabled`. |
| `kix.options.healthCheck` | option attrset (not a function) | Attribute set `{ enabled; retryFor; }` where `enabled` is `bool` (default `false`) and `retryFor` is `int` (default `90`). Assign as `options.healthCheck = kix.options.healthCheck;`; read by `kix.healthCheck` from `config.healthCheck`. |
| `kix.contracts.oidc.options` | `{ scopes, autoLogin ? null }` | Attribute set of options `{ scopes; autoLogin?; }` where `scopes` is `listOf str` that must include `"openid"` (default `scopes`), and `autoLogin` is `bool` (default `false`, included only when `autoLogin != null`). Setting the removed `enabled` or `realm` options fails evaluation in `kix.contracts.oidc.helpers.client` with migration steps. The single sign-on option set lives on its contract. |
| `kix.contracts.smtp.options` | option attrset (not a function) | Attribute set `{ from; }` where `from` is `nullOr str` holding one `@` (default `null`): the sender address that replaces the `smtp` server's `out.from`. Assign as `options.mail = kix.contracts.smtp.options;` and pass `config.mail.from` to `kix.contracts.smtp.helpers.settings`. The mail option set lives on its contract. |

### Resource quantities (`kix.types.quantity` and `kix.parseQuantity`)

`kix.types.quantity` is a NixOS option type that accepts standard Kubernetes resource quantity strings (such as `"100m"`, `"0.5"`, `"256Mi"`, `"2Gi"`, or `"1e3"`) and rejects malformed suffixes or numbers exceeding 64-bit signed integer limits or 18 significant digits.

To compare or compute resource quantities in Nix code, use `kix.parseQuantity`:

```nix
# Returns millicores (integer) when the first argument is "cpu"
cpuMillis = kix.parseQuantity "cpu" config.resources.requests.cpu; # "250m" -> 250, "2" -> 2000

# Returns bytes (integer) for memory or storage
memBytes = kix.parseQuantity "memory" config.resources.requests.memory; # "256Mi" -> 268435456
```

### Tolerations (`kix.types.toleration` and `kix.tolerations`)

`kix.types.toleration` is a submodule option type for Kubernetes pod tolerations. Each entry defines `key` (`nullOr str`), `operator` (`"Equal"` by default, or `"Exists"`), `value` (`nullOr str`), `effect` (`"NoSchedule"`, `"PreferNoSchedule"`, `"NoExecute"`, or `null`), and `tolerationSeconds` (`nullOr ints.unsigned`). Every field except `operator` defaults to `null`. Because Kubernetes rejects some combinations of these fields, validate the list in `assertions` with `kix.tolerations.problems` and format it for the pod spec with `kix.tolerations.render`:

| Helper | Signature | Behavior |
| --- | --- | --- |
| `kix.tolerations.problems` | `{ optionPath, tolerations } -> [ message ]` | Returns an error message for each toleration that Kubernetes would reject: a missing or empty `key` when `operator` is not `"Exists"`, `value` set when `operator` is `"Exists"`, or `tolerationSeconds` set when `effect` is not `"NoExecute"`. Each message identifies the entry as `<optionPath>[<index>]`. |
| `kix.tolerations.render` | `tolerations -> list` | Strips `null` fields from each toleration so you can assign the list directly to `spec.tolerations` on a pod spec. |

```nix
options.node.tolerations = lib.mkOption {
  type = lib.types.listOf kix.types.toleration;
  default = [ ];
  description = "Taints the node plugin tolerates.";
};

assertions =
  { config, ... }:
  map (message: {
    assertion = false;
    inherit message;
  }) (kix.tolerations.problems {
    optionPath = "config.node.tolerations";
    inherit (config.node) tolerations;
  });

# In the pod spec:
tolerations = kix.tolerations.render config.node.tolerations;
```

### VolumeSnapshotClasses (`kix.types.volumeSnapshotClass` and `kix.storage`)

CSI driver packages expose `VolumeSnapshotClass` settings as an option of type `lib.types.attrsOf kix.types.volumeSnapshotClass`, keyed by class name. Each entry accepts `deletionPolicy` (`"Delete"` by default, or `"Retain"`) plus `parameters`, `labels`, and `annotations` (each `attrsOf str`, default `{ }`). The provider of the `snapshotController` role supplies the `VolumeSnapshotClass` CRD, so take `snapshotController ? null` as an optional `build` argument:

| Helper | Signature | Behavior |
| --- | --- | --- |
| `kix.storage.snapshotClassProblems` | `{ optionPath, classes, snapshotController } -> [ message ]` | Returns error messages when `classes` is non-empty and `snapshotController` is `null`, when more than one class sets `annotations."snapshot.storage.kubernetes.io/is-default-class" = "true"`, or when a class is named `root` or `meta` (keys Kix reserves). Call this in `assertions` with `snapshotController = deps.snapshotController or null`. |
| `kix.storage.mkSnapshotClasses` | `{ scope, snapshotController, driver, classes, requires ? [ ] } -> attrset` | Builds a `VolumeSnapshotClass` resource for each entry in `classes` (under the same attribute keys) using `scope.mkCR snapshotController.out.crds.VolumeSnapshotClass`, setting `driver` on each class and omitting `parameters` when empty. Returns `{ }` when `snapshotController` is `null`. |

```nix
snapshotClasses = kix.storage.mkSnapshotClasses {
  inherit scope snapshotController;
  driver = self.csiDriver.out.name;
  classes = config.volumeSnapshotClasses;
  requires = [ self.controller ];
};
```

### Escape hatches and guard helpers

Packages often expose an escape hatch option (`extraSpec`, `extraEnv`, `extraArgs`, or `settings`) so cluster operators can set fields not covered by first-class options. Use Kix's guard helpers to merge operator overrides while protecting invariants owned by the package:

| Helper | Signature | Behavior |
| --- | --- | --- |
| `kix.mergeGuarded` | `{ scope, locked, option ? "extraSpec", object ? null } base overrides` | Recursively merges `overrides` into `base`. Fails evaluation if `overrides` sets any path in `locked` (a list of path-segment lists, such as `[ [ "replicas" ] [ "selector" ] ]` or `[ [ "template" "spec" "serviceAccountName" ] ]`) or replaces an ancestor of a locked path with a non-attribute-set value such as `null`. On conflict, Kix fails with `option` set on the error and tells the user to remove the locked field from that option. Set `option = null` when `overrides` comes from a function argument (such as a builder in `out`) rather than an instance option. Pass `object` (`{ kind; name; namespace ? null; }`) to name the affected Kubernetes resource, such as when one option configures several resources or a builder in `out` creates one. |
| `kix.mergeEnv` | `{ scope, option ? "env", owned ? [ ], defaults ? { } } userEnv` | Checks that `userEnv` does not set any environment variable claimed in `owned` (even to `null`), then returns `defaults // userEnv // <all owned.vars>` for `kix.mkEnvVars`. Each `owned` entry is `{ vars ? { }; names ? [ ]; from; fix ? null; }` for variables the package provides, or `{ names; reason; fix ? null; }` for variables the package rejects outright. If `userEnv` sets a claimed variable, Kix fails with `option` set on the error and reports `<option> sets <VAR>, which comes from <from>.` or `<option> sets <VAR>, which this package does not accept: <reason>`. If `fix` is omitted, the error tells the user to remove the variable from `option`. |
| `kix.mergeArgs` | `{ scope, parser, option ? "extraArgs", args ? [ ], id ? null } userArgs` | Builds the container's argument list (`args`, then `userArgs`) and stops the build if `userArgs` sets a reserved flag. Set `parser` to match how the program reads flags: `"gnu"` (getopt, clap, pflag, kingpin) matches `--flag`, `--flag=value`, or a single-letter `-f` with an attached value; `"component-base"` (Kubernetes component-base `cli.Run`, or pflag with `WordSepNormalizeFunc`) matches like `"gnu"` and treats `_` and `-` as the same character in flag names, so `--leader_elect` matches `--leader-elect`; `"go"` (Go's `flag` package) treats `-flag` and `--flag` as the same flag; `"go-arg"` (alexflint/go-arg) matches like `"go"` with any number of leading dashes; `"paerser"` (Traefik) matches like `"go"` and ignores letter case. Each item in `args` is one of three forms: a plain string (passed as written; `userArgs` may repeat it), `{ defaults = [ "--flag=value" ]; }` (passed unless `userArgs` sets the same flag; write each flag and value as one string), or `{ flags ? [ ]; names ? [ ]; families ? [ ]; reason; fix ? null; }` (or `from` instead of `reason`). Kix passes `flags` to the container and rejects them in `userArgs`; write each item as `"--flag=value"` or as a list `[ "--config" path ]` where the first item is the flag name and the rest are its values. `names` rejects extra flag names without passing them (such as short aliases, unsupported flags, or a conditional flag in `flags = lib.optional <cond> "--flag=value"` that must stay blocked when `<cond>` is false). `families` rejects a flag and every `<flag>.<name>` sub-flag. When `userArgs` sets a reserved flag, Kix sets `option` on the error, reports `<option> sets "<word>", which comes from <from>.` or `<option> sets "<word>", which this package does not accept: <reason>`, shows `fix` (or tells the user to remove the flag from `option`), and notes that [`partsOverlays`](/docs/reference/instances-and-composition/#partsoverlays) can modify the built resource after the package's checks. When `userArgs` is an attribute set (such as `extraArgs` on a VictoriaMetrics custom resource), `flags` and `defaults` must also be attribute sets of flag values, plain string entries are rejected, and keys match with or without leading dashes. Set `id` to a change ID (such as `"K4740"`) when a package starts rejecting a flag or spelling it previously accepted. |
| `kix.argValues` | `{ parser } flag userArgs -> [ value ]` | Returns all values passed for `flag` in `userArgs`, in order, using the same `parser` rules as `kix.mergeArgs`. Reads values after `=`, attached to a single-letter flag (under `"gnu"` and `"component-base"`), or from the next list item. Only call `kix.argValues` for flags that take a value: if `flag` appears without `=`, Kix treats the next list item as its value. When `userArgs` is an attribute set, returns the values of all keys matching `flag`. |

Pass `inherit scope;` to `kix.mergeGuarded`, `kix.mergeEnv`, and `kix.mergeArgs`. All three report errors through `scope.fail`, which sets `instance` and `package` in Kix's error output and in `kix check -o json`.

`kix.mergeArgs` replaces `kix.guardArgs`; calling `kix.guardArgs` stops the build with `[K5396]`.

```nix
# Guarding CLI flags and environment variables owned by typed options
args = kix.mergeArgs {
  inherit scope;
  parser = "gnu";
  args = [
    {
      flags = [ "--storage.tsdb.retention.time=${config.retention}" ];
      from = "the retention option";
      fix = "Remove it from extraArgs and set retention instead.";
    }
    {
      flags = [ "--web.listen-address=:9090" ];
      reason = "the Service and the probes use port 9090.";
    }
  ];
} config.extraArgs;

env = kix.mkEnvVars (
  kix.mergeEnv {
    inherit scope;
    option = "extraEnv";
    defaults = { LOG_FORMAT = "json"; };
    owned = [
      {
        vars = {
          PORT = "8080";
          DATABASE_HOST = database.host;
        };
        from = "the package and its `database` dependency";
        fix = "Wire the database with `deps.database` instead.";
      }
    ];
  } config.extraEnv
);
```

---

## build and deps

### `build` function arguments

Each function in `build` is called via pattern matching on its attribute set parameter list. Kix recognizes the following built-in framework arguments, and treats **any other argument name** as a cross-package dependency request:

| Built-in argument | Description |
| --- | --- |
| `self` | Lazy fixed point of the merged parts attribute set returned across all `build` entries. Use `self.<partName>` to reference sibling resources in the same package. |
| `config` | Evaluated package options (`options` merged with package `config`, dependency injections, and `instances.<name>.config`). |
| `clusterConfig` | Evaluated cluster module configuration (such as `clusterConfig.cluster.domain`, `clusterConfig.cluster.name`, `clusterConfig.k8sVersion`, `clusterConfig.cluster.availability`, and `clusterConfig.networkPolicy.enable`). |
| `scope` | Instance-scoped resource builders (identical to outer `scope`). |
| `lib` | Standard `nixpkgs.lib`. |
| `kix` | Framework helpers. |
| `deps` | Attribute set of all bound dependencies declared across `build` entries and `deps`. |
| `availability` | Effective availability level string for the instance (`"none"`, `"pod"`, `"node"`, or `"zone"`). |

### Declaring dependencies

Any argument to a `build` function outside the built-in list above requests another package instance by instance name, `aliases`, `meta.defaultAliases`, or `meta.roles`:

- **Required dependency** (`{ postgres, ... }:` without `?`): Kix requires a matching instance (or auto-instantiates the package when it is listed in the cluster's `availablePackages`).
- **Optional dependency** (`{ prometheus ? null, ... }:` with `? null`): Bound to the provider's output object if present in the cluster, or `null` if absent.

When a dependency is bound, the argument receives the provider's consumer view:
- `dep.out`: Full output attribute set (`dep.root.resource.out // dep.root.out`).
- Top-level convenience fields: every key in `dep.out` is promoted directly onto `dep` (for example, `postgres.fqdn` and `postgres.port`), alongside any top-level part folders from the provider's `build` (such as `dep.crds` and `dep.secrets`) and the view's system keys (`_type`, `_packageInstance`, `root`, `out`, `raw`, and `_meta`). A `root.out` key or part folder that collides with a system key (or a part folder other than `crds` and `secrets` that collides with a `root.out` key) throws an evaluation error.
- `dep.crds`: Attribute set of CRD resources from the provider's `crds` part folder (unless overridden by `root.out.crds`).

### Enriching dependencies with `deps.<arg>`

The top-level `deps` attribute set lets a consumer package inject configuration into an upstream provider or attach dependency-specific assertions and warnings:

```nix
deps.postgres = {
  config = consumerConfig: {
    databases.${scope.instanceName} = {
      owner = consumerConfig.dbUser;
    };
  };
  assertions = pg: [
    {
      assertion = pg.out.tlsEnabled or false;
      message = "myapp requires postgres to have TLS enabled.";
    }
  ];
  warnings = pg:
    lib.optional (pg.out.maxConnections or 100 < 200)
      "myapp recommends postgres maxConnections >= 200.";
};
```

| `deps.<arg>` field | Type | Description |
| --- | --- | --- |
| `config` | attrset or `consumerConfig: attrset` | Configuration module injected into the upstream provider instance. Throws an error if the provider instance's `config` in the cluster definition locked the target option with `kix.mkLocked`. |
| `assertions` | `dep: [ { assertion; message; } ]` | Assertions evaluated only when `<arg>` is non-null. Kix sets `dependency` to `<arg>` unless the assertion sets it. |
| `warnings` | `dep: [ string or attrset ]` | Warnings evaluated only when `<arg>` is non-null. Each takes the fields of a package's top-level `warnings`, and Kix sets `dependency` to `<arg>` unless the warning sets it. |
| `renamedFrom` | `{ name; id; }` | The name this dependency had in earlier releases, and a change id such as `"K1234"`. When no instance matches the dependency, the error carries `id` and adds a sentence saying that the dependency was named `name` in earlier releases. |

### Built-in dependency contracts (`kix.contracts.*`)

`kix.contracts.*` provides shared contract validators (`postgresDatabase`, `redis`, `smtp`, `oidc`, and `gateway` under `kix.contracts.<name>`, and `oidcProvider`, `otlpEndpoint`, and `clickhouse` under `kix.contracts.experimental.<name>`) that verify the `out` surface passed between providers (including `kix.mkImport` stand-ins) and consumers:

- **Provider validation (`check`)**: A provider passes `root.out` through `kix.contracts.<name>.check scope { ... }` (or `kix.contracts.experimental.<name>.check scope { ... }`), which validates every required and optional field and returns `out`.
- **Consumer validation (`assertions` or helper functions)**: A consumer sets `deps.<alias>.assertions` to `kix.contracts.<name>.assertions` (or `kix.contracts.experimental.<name>.assertions`), or reads the dependency through a contract helper such as `kix.contracts.smtp.helpers.settings`, `kix.contracts.smtp.helpers.server`, `kix.contracts.oidc.helpers.client`, or `kix.contracts.gateway.require`. Consumer checks require all `required = true` fields and validate any optional fields that are present.
- **Automatic error context**: Contract checks that take `scope` fail through `scope.fail`, and Kix attaches `instance`, `package`, and `dependency` to failed dependency assertions automatically, so error messages should not repeat the instance or package name.
- **Credentials on contracts (`out.credentials`)**: Every contract that carries a login places the secret or secret instance at `out.credentials` (read via `dep.out.credentials.out.keyRef "<key>"`) rather than taking a second secret dependency.

```nix
deps.database.assertions = kix.contracts.postgresDatabase.assertions;
```

:::note[Reference]
See [Dependency contracts](/docs/reference/dependency-contracts/) for the full field schemas, secret key requirements, consumer helpers (`kix.contracts.smtp.helpers.*` and `kix.contracts.oidc.helpers.*`), and `kix.mkImport` examples for every contract.
:::

---

## Build entries

`build` can be a single function or a list of functions (or file paths to functions). When `build` is a list:
1. Kix inspects the function arguments of every entry and takes the union of all dependency requests. If any entry declares a dependency without a default (`{ redis }:`), the dependency is required for the package even if another entry declares `{ redis ? null }:`.
2. Each entry is called with its requested arguments and returns an attribute set of named parts.
3. Kix merges the returned attribute sets across all entries with `mergeCollide`. Any duplicate part name across `build` entries throws an evaluation error.

### Parts, folders, and conditional `null` entries

Each attribute in a `build` return set is one of the following:
- **A resource** created by `scope.mkResource`, `scope.mkDeployment`, `scope.mkSecret`, `scope.mkCRD`, or a provider builder.
- **`null`**, which Kix filters out automatically. Use `if config.metrics.enabled then scope.mkResource { ... } else null` or `scope.mkOptional config.metrics.enabled (scope.mkResource { ... })` for conditional resources.
- **A nested folder** (up to 3 levels deep) grouping related resources, such as `rbac = { sa = ...; role = ...; binding = ...; };`. Top-level folders are also exposed on the instance's consumer view.
- **Known folders (`secrets` and `crds`)**:
  - Both `secrets` and `crds` must be attribute sets of resources rather than a single resource directly (assigning a resource directly to `secrets` or `crds` throws an evaluation error), and any entry inside them that is not a Kix resource (`_type = "k8s-resource"`) emits an evaluation warning.
  - Place `scope.mkSecret` / `scope.mkSecretRef` resources under `secrets = { ... };` and `scope.mkCRD` / `scope.mkCRDRef` resources under `crds = { ... };`. CRDs placed in `crds` are exposed to downstream consumers at `dep.crds.<Kind>`.

### Composable build-entry helpers

Because `build` accepts a list of functions, cross-cutting concerns are packaged as standalone `build` entries:

```nix
build = [
  (
    { self, config }:
    {
      deployment = ...;
      service = ...;
      root = self.service;
    }
  )
  (kix.expose { service = "service"; })
  (kix.monitor { service = "service"; port = "metrics"; })
  (kix.healthCheck {
    runtime = kix.healthCheck.runtimes.shell;
    script = ''
      curl -sf "http://$SERVICE_FQDN:8080/healthz"
    '';
    env = { self, ... }: { SERVICE_FQDN = self.service.out.fqdn; };
  })
];
```

See [Composition helpers](/docs/reference/composition-helpers/) for the full reference on `kix.expose`, `kix.monitor`, `kix.alerts`, and `kix.healthCheck`.

### Evaluation-only store (no shell builders)

Kix evaluates packages in its local store without running shell build scripts. Package and cluster definitions can evaluate expressions, read files from the repository (`builtins.readFile`, `builtins.fromJSON`), write files with `builtins.toFile` or `builtins.path`, and render Helm charts through `scope.helm.fetchHelmChart` and `scope.helm.importChart`.

Do not call `pkgs.runCommand`, `pkgs.stdenv.mkDerivation`, or `pkgs.writeText` inside a package or cluster definition; evaluation fails under the default `kix` builder.

---

## Package diagnostics

### Top-level `warnings` and `assertions`

A package can define top-level `warnings` and `assertions` functions. Both receive `{ config, self, deps, clusterConfig }` after options and `build` parts are wired. `clusterConfig` is the same evaluated cluster configuration passed to `build`. Because `deps` only contains bound dependencies, an absent optional dependency is omitted from `deps` (test for it with `!(deps ? <name>)` or `(deps.<name> or null) == null`):

```nix
warnings =
  { config, deps, ... }:
  lib.optional (config.ingress.host != null && !(deps ? gateway) && !(deps ? ingressController)) {
    message = "ingress.host is set, but neither gateway nor ingressController is present in the cluster.";
    option = "ingress.host";
  };

assertions =
  { config, ... }:
  [
    {
      assertion = !(config.tls.enable && config.tls.secretName == null);
      message = "config.tls.secretName is not set, and config.tls.enable is true.";
      fix = "Set config.tls.secretName to the Secret that holds the certificate.";
      option = "tls.secretName";
    }
  ];
```

An assertion takes the same optional fields as `scope.fail` (below): `fix`, `id`, `object`, `option`, `dependency`, and `clusterOption`. A warning is a message, or an attribute set with `message` and the same optional fields. Kix adds the instance to each, so the message does not name it. Failed `assertions` stop evaluation, and Kix lists every failed assertion of the cluster at once. `warnings` do not stop evaluation. `kix check` lists them under its table, and every other command that evaluates a cluster or reads a bundle with `--bundle` prints them to stderr. Return warnings from `warnings` or `deps.<name>.warnings`; Kix hides `builtins.trace` and `lib.warn` output when evaluation succeeds. A field in the wrong form, such as an `id` that is not a change record id, stops evaluation in a warning as it does in an error.

### Raising an error with `scope.fail`

A package stops evaluation with `scope.fail` when something it was given cannot work. Do not use `throw` for an error a person must fix: Kix shows a `scope.fail` error from its fields, without a stack trace, and `kix check -o json` carries the fields.

```nix
_passwordCheck =
  if config.password != null then
    scope.fail {
      id = "K9503";
      option = "password";
      message = "password was removed, because it put the password in plain text.";
      fix = "Give the password as the dependency `password`.";
    }
  else
    null;
```

| Field | Required | Meaning |
| --- | --- | --- |
| `message` | Yes | What is wrong. `scope.fail "text"` is short for `scope.fail { message = "text"; }`. |
| `fix` | No | What to change. It may span several lines, such as a line to paste into the cluster definition. |
| `id` | No | The change record id, such as `"K0001"`, when the error refuses something an upgrade removed. |
| `object` | No | The Kubernetes object at fault, as `{ kind = "Deployment"; name = "web"; }`, with an optional `namespace`. |
| `option` | No | The option to change, as a path in the instance's `config`, such as `"tls.secretName"`. `kix pin set --option` takes the same form. |
| `dependency` | No | The dependency to change, as its name in the instance's `deps`, such as `"database"`. |
| `clusterOption` | No | The cluster option to change, as a path in the cluster definition, such as `"cluster.domain"`. |
| `instance` | No | Set by `scope.fail` to `<namespace>/<instance>`. |
| `package` | No | The package's name, such as `"harbor"` or `"experimental/coredns"`. Set automatically by `scope.fail` for built-in packages. |

`option`, `dependency`, and `clusterOption` name settings a person can change in a cluster definition. A package's `meta` is not an instance option, so leave `option` out when an error is about `meta`. An `option` must not start with `config.`, `deps.`, or `meta.`, and a `clusterOption` must not start with `instances.`. Kix leaves out a field in the wrong form, or one it does not take, and adds a line to the message that says why. A warning with such a field stops evaluation with that error. Start `fix` with a capital letter.

`scope.fail` also takes a list of errors, to report several problems at once. A cluster definition, which has no `scope`, uses `kix.fail`, which takes the same arguments and leaves out the instance.

Kix shows the error as:

```text
[K9503] shop/db: password was removed, because it put the password in plain text.
Option: password
Fix: Give the password as the dependency `password`.
```

### Structural build-time warnings

During package evaluation, Kix also gives structural warnings for common wiring mistakes, which `kix check` lists with the package's own warnings. Each names the resource in its `object` field:

- **Orphaned resources**: A part defined in `build` is not reachable from `root.resource`, `root.requires`, or `root.postHooks`.
- **Redundant `requires`**: A resource lists a dependency in `requires = [ ... ]` that is already tracked implicitly through `.out` string context in its manifest.
- **Duplicate `requires`**: The same resource appears more than once in a `requires` list.
- **Unordered `postHooks`**: A resource in `root.postHooks` does not depend (directly or through another resource) on `root.resource` or any entry in `root.requires`, so Kix can deploy it before the main resources are ready. Referencing a Secret or ConfigMap that `root.resource` also uses does not order the hook after `root.resource`. If a resource must finish before the instance is ready without waiting for `root.resource`, put it in `root.requires` instead.

### `kix.checkPackageOptions` and `kix.packageOptionViolations`

`kix.checkPackageOptions` validates that a package's `options` block obeys the [option default contract](#the-option-default-contract) across every availability level up to `meta.availability.max`:

```nix
kix.checkPackageOptions {
  package = import ./pkgs/myapp;
  who = "myapp";
}
```

`kix.checkPackageOptions` returns `null` when valid or throws an evaluation error listing all violations. To inspect the violations as data without throwing, call `kix.packageOptionViolations { package = ...; who = "..."; }`, which returns a list of violation attribute sets (empty when valid). `kix.mkFlake` runs `kix.checkPackageOptions` automatically across all exported packages.

### `kix.contracts.secret.helpers.use`

A package takes every secret value through a secret dependency, never through an option, so the cluster chooses where the value comes from: `packages.secret` (plain text or a SOPS file), `packages.secret-ref` (a Secret that something else writes), or another source package. A test in the Kix repository fails any package that Kix ships if one of its options holds a secret or a path to one.

Wrap the dependency with `kix.contracts.secret.helpers.use` before reading its keys. It checks that `dep` satisfies `kix.contracts.secret` (`out.name`, `out.keyRef`, `out.mkEnv`, `out.envFrom`, and `out.volume`) and that `dep.out.keys` declares every key in `keys` and at least one full group in `anyOf`. When valid, it returns `dep`; otherwise it fails through `scope.fail` with `dependency` set to `arg`. `kix.checkSecretKeys` takes the same arguments and checks only the keys.

```nix
build =
  {
    self,
    config,
    credentialsSecret ? null,
    ...
  }:
  let
    checkedSecret = kix.contracts.secret.helpers.use {
      inherit scope;
      arg = "credentialsSecret";
      dep = credentialsSecret;
      keys = [ "username" ];
      anyOf = [
        [ "password" ]
        [ "api-token" ]
      ];
    };
  in
  {
    ...
  };
```

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `scope` | scope | Required | The consumer's `scope`, used to report the instance and package through `scope.fail`. Passing the removed `who` argument fails with `[K6417]`. |
| `arg` | string | Required | Dependency argument name. |
| `dep` | resource or `null` | Required | Bound Secret dependency. Returned unchanged when `dep` is `null` or when all key requirements are satisfied (or if `dep.out.keys` is not set). |
| `keys` | list of strings | `[ ]` | Keys that must all be present in `dep.out.keys`. |
| `anyOf` | list of lists of strings or `null` | `null` | When non-null, every key in at least one inner list (`[ [ "a" "b" ] [ "c" ] ]`) must be present in `dep.out.keys`. |