# Dependency contracts

When an application package depends on a database, Redis cache, mail server, single sign-on provider, Gateway, or OpenTelemetry collector, Kix validates the connection details against a shared **dependency contract** (`kix.contracts.*`). Reach for this page when you are wiring an external service into a cluster with `kix.mkImport`, setting up `oidc-client` or `smtp`, or writing a package that provides or consumes one of these interfaces.

:::note[Helpful background]
- [Dependency contracts](/docs/explanation/dependency-contracts/): understand why Kix uses contracts to decouple application packages from in-cluster operators and external cloud services.
- [Connect external databases, caches, mail, and single sign-on](/docs/how-to/adopt-existing-resources/connect-external-databases-caches-mail-and-sso/): follow task recipes for wiring contract-backed services in `cluster.nix`.
- [Package anatomy](/docs/reference/package-anatomy/#build-and-deps): see how packages declare dependencies in `build` and attach contract assertions in `deps`.
- [Helm and imports](/docs/reference/helm-and-imports/#mkimport): review the arguments and namespace scoping rules for `kix.mkImport`.
:::

## Contract overview

Every contract in `kix.contracts.*` defines the default dependency alias, the required and optional fields on `dep.out`, and validator functions for both providers and consumers.

| Contract | Default alias | Typical providers | Typical consumers |
| --- | --- | --- | --- |
| [`kix.contracts.postgresDatabase`](#postgresql-database-kixcontractspostgresdatabase) | `"database"` | `cnpg-database`, `db-operator-database`, `stackgres-database`, `kix.mkImport` | `harbor`, `immich`, `keycloak`, `metabase`, `n8n`, `plausible`, `vaultwarden` |
| [`kix.contracts.redis`](#redis-kixcontractsredis) | `"redis"` | `valkey`, `kix.mkImport` | `harbor`, `immich`, `n8n` |
| [`kix.contracts.smtp`](#mail-kixcontractssmtp) | `"smtp"` | `kix.mkImport` | `grafana`, `immich`, `keycloak`, `plausible`, `vaultwarden` |
| [`kix.contracts.oidc`](#single-sign-on-kixcontractsoidc-and-kixcontractsexperimentaloidcprovider) | `"oidc"` | `oidc-client`, `kix.mkImport` | `grafana`, `harbor`, `immich` |
| [`kix.contracts.experimental.oidcProvider`](#multi-realm-providers-kixcontractsexperimentaloidcprovider-and-packagesexperimentaloidc-client) | `"oidcProvider"` | `keycloak`, `kix.mkImport` | `oidc-client` |
| [`kix.contracts.gateway`](#gateway-api-kixcontractsgateway) | `"gateway"` | `envoy-gateway`, `traefik`, `kix.mkImport` | `kix.expose` |
| [`kix.contracts.experimental.otlpEndpoint`](#opentelemetry-kixcontractsexperimentalotlpendpoint) | `"otlp"` | `tempo`, `otel-collector`, `kix.mkImport` | `otel-collector` |
| [`kix.contracts.experimental.clickhouse`](#clickhouse-kixcontractsexperimentalclickhouse) | `"clickhouse"` | `clickhouse`, `clickhouse-cluster` | `clickhouse-database` |
| [`kix.contracts.secret`](#secret-kixcontractssecret) | `"secret"` | `secret`, `secret-ref`, `external-secret`, any Secret from `scope.mkSecret` or `scope.mkSecretRef` | every package that reads a secret value |

### Shared contract attributes and methods

Each stable contract is one file in `kix/contracts/stable/`, and the file name is the contract's name under `kix.contracts`. For example, `kix/contracts/stable/redis.nix` is `kix.contracts.redis`. Each experimental contract is one file in `kix/contracts/experimental/`, read as `kix.contracts.experimental.<name>`. Every file builds its contract with `mkContract` in `kix/lib/vocab/contract.nix`, so every contract has exactly these parts:

| Attribute / method | Signature | Behavior |
| --- | --- | --- |
| `name` | `string` | The contract's name in error messages, such as `"Redis"`. |
| `alias` | `string` | Default dependency argument name (`"database"`, `"redis"`, `"smtp"`, `"oidc"`, `"oidcProvider"`, `"gateway"`, `"otlp"`, `"clickhouse"`, or `"secret"`). |
| `fields` | `attrset` | Map of field names to `{ required; shape; test; }` definitions. A `credentials` field also lists its Secret key names in `fields.credentials.keys`. |
| `check` | `scope -> out -> out` | Provider-side validator. Verifies that `root.out` defines **every** field in `fields` (both required and optional) with a valid shape and returns `out`, or fails through `scope.fail` listing the missing or invalid fields. |
| `problems` | `out -> [ string ]` | Consumer-side problem finder. Checks that all `required = true` fields are present and valid, and validates the shape of any `required = false` fields that are present on `out`. |
| `assertions` | `dep -> [ { assertion; message; } ]` | Consumer-side assertion builder for `deps.<alias>.assertions`, such as `deps.database.assertions = kix.contracts.postgresDatabase.assertions;`. Kix attaches `instance`, `package`, and `dependency` to each assertion automatically. Because `kix.mkImport` instances are checked on the consumer side, they only need to define `required = true` fields (plus any optional fields the consumer reads). |
| `require` | `scope -> dep -> dep` | Eager consumer-side validator (used by `kix.expose` for `kix.contracts.gateway`, and called by `kix.contracts.smtp.helpers.server` and `kix.contracts.oidc.helpers.client`). Validates `dep.out` with `problems` and returns `dep`, or fails through `scope.fail` with `dependency` set to `alias`. |
| `requireAs` | `dependency -> scope -> dep -> dep` | Like `require`, for a consumer whose dependency argument is not named `alias`. Sets `dependency` on the error to the given name. |
| `helpers` | `attrset` | The contract's own functions and values for its consumers, such as `kix.contracts.redis.helpers.usesSentinel`. An empty attribute set when the contract has none. |
| `options` | option attrset, a function that returns one, or `null` | The option set a consumer package adds to its own `options`, such as `kix.contracts.smtp.options`. `null` when the contract has none. |

A stable contract breaks only in a minor release. An experimental contract is a file in `kix/contracts/experimental/` and is read as `kix.contracts.experimental.<name>`. It may break in any release, including a patch release. A stable package reads only stable contracts, and an experimental package may read both. `oidcProvider`, `otlpEndpoint`, and `clickhouse` are experimental. The other contracts on this page are stable. See [Stable and experimental packages](/docs/reference/instances-and-composition/#stable-and-experimental-packages) for the two levels.

In a provider package, pass `root.out` through `check` so Kix validates every required and optional field:

<Snippet {...postgresDatabaseCheck} />

In a consumer package, attach `assertions` to `deps.<alias>` so Kix validates the bound dependency (whether it is a managed provider or a `kix.mkImport`):

<Snippet {...postgresDatabaseAssertions} />

---

## Credentials in contracts (`out.credentials`)

Whenever a dependency contract carries a login, it places the login in `out.credentials`: a `secret` or `secret-ref` instance, or a Secret built by the provider package, whose `out.keyRef` reads the keys required by the contract. Every contract with a login defines `out.credentials` using `credentialsField { keys; required ? true; }` in `kix/lib/vocab/contract.nix`. A consumer package reads credentials directly from the dependency (`dep.out.credentials.out.keyRef "<key>"`) rather than taking a separate secret dependency.

- **In-cluster providers**: A managed provider (such as `cnpg-database`, `stackgres-database`, or `valkey`) builds its own Secret in the consumer's namespace and publishes it at `out.credentials`.
- **External imports (`kix.mkImport`)**: An external service declared with [`kix.mkImport`](/docs/reference/helm-and-imports/#mkimport) points `out.credentials` at a `secret` or `secret-ref` instance in the same namespace using `credentials = ref.<namespace>.<instance>`.

Because a Kubernetes pod can only read Secrets from its own namespace, and Kix rejects cross-namespace Secret references at build time, any `kix.mkImport` that sets `out.credentials` must be placed in the consumer's namespace beside its secret with `scope = "namespace"`. An import without a login can keep the default `scope = "cluster"` and serve every namespace. When several namespaces connect to the same external service with their own secrets, use a Nix `let` binding to share the common host and port fields across the per-namespace imports.

---

## Secret (`kix.contracts.secret`)

`kix.contracts.secret` (`kix/contracts/stable/secret.nix`) defines what a package reads from a secret dependency. A package takes every secret value through such a dependency and never through an option, so the cluster chooses where the value comes from. A test in the Kix repository fails a package that Kix ships if one of its options holds a secret or a path to one.

A secret dependency has no fixed name: Grafana's is `grafanaAdmin`, caddy's is `caddyfile`. The alias `secret` is the default alias of `packages.secret` and `packages.secret-ref`. A cluster wires a dependency by an alias that matches the package's argument name, or with `deps.<name> = ref.<namespace>.<instance>`.

| Field | Required | Type / shape | Description |
| --- | --- | --- | --- |
| `name` | Yes | `str` | The Secret's name. It carries the dependency, so a workload that reads it is ordered after it. |
| `keyRef` | Yes | `key -> valueFrom` | An env var's `valueFrom` for one key. |
| `mkEnv` | Yes | `{ ENV = key; } -> [ env ]` | Env entries for several keys. |
| `envFrom` | Yes | `attrset` | An `envFrom` entry for the whole Secret. |
| `volume` | Yes | `name -> volume` | A volume that mounts the Secret. |
| `keys` | No | `list` of `str` or `null` | The keys the Secret holds. |
| `sops` | No | `{ source; digest; }` | The SOPS file that Kix fills the Secret from, and its SHA-256, set only when it does. |

A consumer checks the dependency with `kix.contracts.secret.helpers.use { scope; arg; dep; keys ? [ ]; anyOf ? null; }`, which returns `dep` or fails through `scope.fail` with `dependency` set to `arg`. See [`kix.contracts.secret.helpers.use`](/docs/reference/package-anatomy/#kixcontractssecrethelpersuse).

---

## PostgreSQL database (`kix.contracts.postgresDatabase`)

`kix.contracts.postgresDatabase` (`kix/contracts/stable/postgresDatabase.nix`) defines the contract for packages that answer the `database` alias.

### `postgresDatabase` fields

| Field | Required | Type / shape | Description |
| --- | --- | --- | --- |
| `host` | Yes | `str` | PostgreSQL hostname, such as a Service FQDN or an RDS / Cloud SQL endpoint. |
| `port` | Yes | `str` | PostgreSQL port as a string (such as `"5432"`), kept as a string for URL and environment variable interpolation. |
| `database` | Yes | `str` | Database name. |
| `credentials` | Yes | secret (`username`, `password`) | Secret or secret instance exposing `out.keyRef "username"` and `out.keyRef "password"`. |
| `username` | No (required on provider `check`) | `str` | Database role name. Consumers read the role from `out.credentials.out.keyRef "username"`, so a `kix.mkImport` does not need to repeat `out.username`. |
| `extensions` | No (required on provider `check`) | `list` of `str` or `{ name; version; }` | PostgreSQL extensions installed in the database. |
| `tls` | No (required on provider `check`) | `bool` or `null` | Whether PostgreSQL serves TLS, or `null` when the provider cannot determine it. |

### Importing an external PostgreSQL database

<Snippet {...importedPostgresDatabase} />

---

## Redis (`kix.contracts.redis`)

`kix.contracts.redis` (`kix/contracts/stable/redis.nix`) defines the contract for servers speaking the Redis protocol. `valkey` provides it in-cluster, and consumers (`harbor`, `immich`, `n8n`) request it as their `redis` dependency.

### `redis` fields

| Field | Required | Type / shape | Description |
| --- | --- | --- | --- |
| `host` | Yes | `str` | Server hostname, such as a Service FQDN or an ElastiCache endpoint. |
| `port` | Yes | `str` of digits | Redis port as a string of digits (for example, `"6379"`). |
| `credentials` | No (required on provider `check`) | secret (`password`) | Secret or secret instance exposing `out.keyRef "password"`. Omit on a `kix.mkImport` when the external server takes no password. |
| `tls` | No (required on provider `check`) | `bool` | Whether the server serves TLS (`false` on managed `valkey`). |
| `sentinel` | No (required on provider `check`) | `null` or `{ port; primarySet; }` | When Redis Sentinel fronts the server, an attribute set with `port` (string of digits, such as `"26379"`) and `primarySet` (string, such as `"primary"`). `null` when Sentinel is disabled. |

### `redis` helper functions

| Helper | Signature | Behavior |
| --- | --- | --- |
| `kix.contracts.redis.fields.credentials.keys` | `[ "password" ]` | Required key names when `out.credentials` is present. |
| `kix.contracts.redis.helpers.usesSentinel` | `dep -> bool` | Returns `true` when `dep.out.sentinel` is non-null. |
| `kix.contracts.redis.helpers.requirePlaintext` | `scope -> dep -> dep` | Fails through `scope.fail` (with `dependency = "redis"`) if `dep.out.tls` is `true`; otherwise returns `dep`. Used by `harbor` and `immich`, which do not support Redis over TLS (`n8n` supports `tls = true`). |
| `kix.contracts.redis.helpers.renamedFrom` | `{ name = "valkey"; id; }` | The name this dependency had in earlier releases. `harbor`, `immich`, and `n8n` set it as [`deps.redis.renamedFrom`](/docs/reference/package-anatomy/#enriching-dependencies-with-depsarg), so the error for a missing `redis` dependency says that it was named `valkey`. |

### Importing an external Redis server

<Snippet {...importedRedisCache} />

---

## Mail (`kix.contracts.smtp`)

`kix.contracts.smtp` (`kix/contracts/stable/smtp.nix`) defines the contract for the optional `smtp ? null` dependency taken by packages that send email (`grafana`, `immich`, `keycloak`, `plausible`, and `vaultwarden`).

### `smtp` fields

| Field | Required | Type / shape | Description |
| --- | --- | --- | --- |
| `host` | Yes | `str` | SMTP server hostname or IP address (no whitespace, `:`, or `/`). |
| `port` | Yes | `str` of digits or `int` (`1..65535`) | SMTP port number (for example, `"587"` or `"465"`). |
| `tls` | Yes | `"starttls"` \| `"implicit"` \| `"none"` | Connection security mode. `"none"` is refused when `external` is `true` (or omitted). |
| `credentials` | No | secret (`username`, `password`) | Optional secret or secret instance exposing `out.keyRef "username"` and `out.keyRef "password"`. |
| `from` | No | `str` (`user@domain`) or `null` | Default sender email address, overridden when the consuming instance sets `config.mail.from`. |
| `external` | No | `bool` | `true` when the server is outside the cluster (the default assumed by consumer helpers when omitted), or `false` when it runs inside the cluster. |

### `smtp` consumer helpers and options

| Helper | Signature | Behavior |
| --- | --- | --- |
| `kix.contracts.smtp.helpers.settings` | `{ scope, smtp, from ? null } -> attrset or null` | Returns `null` when `smtp` is `null`. Otherwise validates `smtp` and returns `{ host; port; portNumber; tls; external; from; credentials; }`, where `port` is a string, `portNumber` is an integer, `from` is the `from` argument (if non-null) or `smtp.out.from`, and `credentials` is `smtp.out.credentials` or `null`. Fails through `scope.fail` if `external` is `true` with `tls = "none"`, or if both `from` and `smtp.out.from` are `null`. |
| `kix.contracts.smtp.helpers.server` | `{ scope, smtp } -> attrset or null` | Validates `smtp` with the same rules and return shape as `settings`, except `from` may be `null`. Used by packages such as `keycloak` that configure sender addresses inside application realms rather than at the container level. |
| `kix.contracts.smtp.helpers.egressPolicy` | `{ scope, name, selector, smtp } -> resource or null` | Returns a network policy opening TCP egress on `smtp.portNumber` to `"world"` for pods matching `selector`, or `null` when `smtp` is `null` or `smtp.external` is `false`. |
| `kix.contracts.smtp.fields.credentials.keys` | `[ "username" "password" ]` | Required key names when `out.credentials` is present. |
| `kix.contracts.smtp.options` | option attrset | Declares `from` (`nullOr str` containing `@`, default `null`) for `options.mail = kix.contracts.smtp.options;`. |

### Consumer mailer constraints

When `smtp` is wired, consuming packages refuse overlapping SMTP keys in `env` or `settings` and check that `tls` and `port` match their mailer's behavior:
- **`plausible` and `immich`**: Require `tls = "implicit"` whenever `out.credentials` is set, because their mailers send credentials in clear text if a server on a `starttls` port does not offer `STARTTLS`.
- **`grafana`**: Uses implicit TLS only on port 465, so it refuses `tls = "implicit"` on any port other than 465 and refuses `tls = "starttls"` or `"none"` on port 465.

### Importing an SMTP server

Without a login, one cluster-wide import can serve every namespace:

<Snippet {...clusterWideSmtpRelay} />

With a login, place the import in the consumer's namespace beside its secret with `scope = "namespace"`:

<Snippet {...authenticatedSmtpImport} />

---

## Single sign-on (`kix.contracts.oidc` and `kix.contracts.experimental.oidcProvider`)

Applications that sign users in with OpenID Connect (`grafana`, `harbor`, and `immich`) take an optional `oidc ? null` dependency checked against `kix.contracts.oidc` (`kix/contracts/stable/oidc.nix`). Single sign-on is enabled whenever `oidc` is bound.

Two kinds of instance satisfy `oidc`, both placed in the application's namespace beside the client secret:
1. **`packages.experimental.oidc-client`**: Pairs a multi-realm identity provider (`kix.contracts.experimental.oidcProvider`, such as `keycloak`) with one realm name and one client secret (`oidcClient`).
2. **A namespaced `kix.mkImport`**: Declares an external single-issuer provider directly in the application's namespace with `scope = "namespace"` and `credentials = ref.<namespace>.<secret>`.

### `oidc` fields (`kix.contracts.oidc`)

| Field | Required | Type / shape | Description |
| --- | --- | --- | --- |
| `issuerUrl` | Yes | `str` (`https?://...`) | Expected `iss` claim in ID tokens and base URL for `/.well-known/openid-configuration` discovery. |
| `credentials` | Yes | secret (`clientId`, `clientSecret`) | Secret or secret instance exposing `out.keyRef "clientId"` and `out.keyRef "clientSecret"`. |
| `endpoints` | No (required by consumers that do not use OIDC discovery, such as `grafana`) | `{ authorizationUrl; tokenUrl; userinfoUrl; }` | Explicit OAuth 2.0 / OIDC endpoint URLs, each an `http://` or `https://` URL string. |

### `oidc` consumer helpers and options

| Helper | Signature | Behavior |
| --- | --- | --- |
| `kix.contracts.oidc.helpers.client` | `{ scope, oidc, options } -> attrset or null` | Validates `options` (`config.oidc`), failing through `scope.fail` if the removed `enabled` or `realm` options are set. Returns `null` when `oidc` is `null`, or `{ issuerUrl; endpoints; credentials; }` when bound (reading `.endpoints` fails through `scope.fail` if the provider omits `out.endpoints`). |
| `kix.contracts.oidc.options` | `{ scopes, autoLogin ? null } -> option attrset` | Declares `scopes` (`listOf str` that must include `"openid"`, defaulting to `scopes`) and, when `autoLogin != null`, `autoLogin` (`bool`, default `false`). |
| `kix.contracts.oidc.fields.credentials.keys` | `[ "clientId" "clientSecret" ]` | Required key names on `out.credentials`. |
| `kix.contracts.oidc.helpers.endpointKeys` | `[ "authorizationUrl" "tokenUrl" "userinfoUrl" ]` | Required attribute names when `out.endpoints` is set. |

### Multi-realm providers (`kix.contracts.experimental.oidcProvider` and `packages.experimental.oidc-client`)

A multi-realm identity provider such as `keycloak` publishes functions of the realm name on `root.out`, checked by `kix.contracts.experimental.oidcProvider` (`kix/contracts/experimental/oidcProvider.nix`):

| Field | Required | Type / shape | Description |
| --- | --- | --- | --- |
| `issuerUrl` | Yes | `realm -> url` | Function taking a realm name string and returning that realm's issuer URL (`https?://...`). |
| `endpoints` | No (required on provider `check`) | `realm -> { authorizationUrl; tokenUrl; userinfoUrl; }` | Function taking a realm name string and returning that realm's authorization, token, and userinfo URLs. |

`packages.experimental.oidc-client` (`meta.scope = "namespace"`, `meta.defaultAliases = [ "oidc" ]`) connects a consumer namespace to an `oidcProvider`:
- **`config.realm`** (`str`, required): Realm name passed to `oidcProvider.out.issuerUrl` and `oidcProvider.out.endpoints`.
- **`deps.oidcProvider`** (required): Explicit `ref.<namespace>.<provider>` pointing to the `keycloak` instance (or `oidcProvider` import).
- **`oidcClient` dependency** (required): Secret or `secret-ref` instance in the same namespace declaring `keys = [ "clientId" "clientSecret" ]`.

<Snippet {...keycloakOidcClient} />

### Importing a single-issuer external OIDC provider

When the external identity provider has a single issuer URL, declare a `kix.mkImport` in the application's namespace with `scope = "namespace"` (add `out.endpoints` when the consumer, such as `grafana`, requires explicit endpoints):

<Snippet {...externalOidcImport} />

---

## Gateway API (`kix.contracts.gateway`)

`kix.contracts.gateway` (`kix/contracts/stable/gateway.nix`) defines the contract between Gateway API providers (`envoy-gateway`, `traefik`, or `kix.mkImport`) and `kix.expose`.

### `gateway` fields

| Field | Required | Type / shape | Description |
| --- | --- | --- | --- |
| `gatewayRef` | Yes | `{ name; namespace; }` | Name and namespace of the Kubernetes `Gateway` resource attached as `parentRefs` on generated `HTTPRoute` resources. |
| `listeners` | No (required on provider `check` and when reading `exposure.url`) | `list` of `{ name; port; protocol; }` | Gateway listener definitions (`port` is an integer, `protocol` is `"HTTP"` or `"HTTPS"`). |
| `podSelector` | No (required on provider `check`) | non-empty attrset of `str`, or `null` | Labels of the gateway data-plane pods so `kix.expose` can generate an ingress `NetworkPolicy` on exposed backends. Must be set together with `podNamespace` (or both must be `null`). |
| `podNamespace` | No (required on provider `check`) | `str` or `null` | Namespace of the gateway data-plane pods. Must be set together with `podSelector` (or both must be `null`). |
| `public` | No (required on provider `check`) | `bool` or `null` | Whether the Gateway may be reachable from outside the cluster: `false` when its `Service` is `ClusterIP`, `true` for other `Service` types, or `null` when unknown. Consumers that refuse public exposure should treat `null` as `true`. |

### `gateway` helper functions

`kix.expose` reads its gateway through `require` and the `claims` helper:
- **`kix.contracts.gateway.require scope dep`**: Validates `dep.out` (requiring `gatewayRef` and checking that `podSelector` and `podNamespace` are either both set or both `null`) and returns `dep`.
- **`kix.contracts.gateway.helpers.claims dep`**: Returns `true` when `dep` declares the `"gateway"` alias or publishes `out.gatewayRef`. `kix.expose` uses this check so an unrelated application instance named `gateway` in the same namespace is not mistaken for a Gateway API controller.

---

## OpenTelemetry (`kix.contracts.experimental.otlpEndpoint`)

`kix.contracts.experimental.otlpEndpoint` (`kix/contracts/experimental/otlpEndpoint.nix`) defines the contract for OpenTelemetry receivers that answer the `otlp` alias (`tempo`, `otel-collector`, or `kix.mkImport`).

### `otlpEndpoint` fields

| Field | Required | Type / shape | Description |
| --- | --- | --- | --- |
| `otlpGrpcUrl` | Yes | `str` (`https?://host:port`) | OTLP/gRPC base URL with no path (port `4317` by convention). |
| `otlpHttpUrl` | Yes | `str` (`https?://host:port`) | OTLP/HTTP base URL with no path (port `4318` by convention; HTTP exporters append `/v1/<signal>`). |
| `tls` | No (required on provider `check`) | `bool` or `null` | Whether the receivers serve TLS, or `null` when the provider cannot determine it. |

---

## ClickHouse (`kix.contracts.experimental.clickhouse`)

`kix.contracts.experimental.clickhouse` (`kix/contracts/experimental/clickhouse.nix`) defines the contract for ClickHouse servers that answer the `clickhouse` alias. Both single-node `clickhouse` and operator-managed `clickhouse-cluster` provide it, and `clickhouse-database` consumes it. It requires `httpUrl`, `nativeEndpoint`, `host`, `ports` (`{ http; native; }`), `username`, `credentials` (a secret with key `"password"`), `clusterName`, `version` (the server version, `"YY.M"` or longer), `replicas`, and `replicaHosts`.