# Use auto-instantiation with `availablePackages`, `optional`, and `autoNamespace`

Use auto-instantiation when a cluster has a standard package for a dependency,
but should include it only when something needs it. Use an optional instance
when you also need to configure that dormant provider.

This guide assumes you already have packages whose required build arguments
describe their dependencies.

## Declare the dependencies

In the consuming package, make each dependency a required build argument. This
application reads values from `database` and `metrics`:

<Snippet {...autoInstantiatedDeps} />

Required arguments create demand. Kix first looks for a matching instance or
alias, then consults the cluster's package catalog.

## Add a default package to the catalog

Add the package under the dependency name in `availablePackages`. Set
`autoNamespace` if auto-instantiated packages should live in a shared
namespace:

<Snippet {...autoInstantiationCatalog} />

When an instance requires `database`, Kix creates a `database` instance in
`platform` with the package's default configuration. Without `autoNamespace`,
Kix creates it in the requesting instance's namespace. A `namespace` set on
the individual catalog entry takes precedence over `autoNamespace`. A package
that runs once per cluster, such as an operator, needs a namespace of its own;
if Kix would place it in the requesting instance's namespace, evaluation fails
with `[K2377]`. Declare the operator under `instances`, set `autoNamespace`, or
set `namespace` on its catalog entry.

## Keep a configured provider dormant

An `availablePackages` entry is instantiated with default configuration. When
the provider needs cluster-specific configuration, declare a normal instance
and set `optional = true`:

<Snippet {...optionalInstance} />

Kix excludes this instance when nothing depends on `metrics`. A required
`metrics` argument activates it with the configuration shown above. So does a
consumer that wires it explicitly with `deps.<arg> = ref.platform.metrics`,
whatever the argument is called.

## Add the consumer

Declare only the package you intend to run directly:

<Snippet {...autoInstantiationConsumer} />

The package's required arguments pull both providers into the evaluated
cluster. This also works transitively when an activated provider has required
dependencies of its own.

## Check the result

Evaluate the cluster:

<Command {...check} />

Inspect the dependency graph to confirm where the providers were created:

<Command expandable {...graph} />

The graph contains `database@platform`, `metrics@platform`, and `api@apps`.
Both provider ConfigMaps appear before the application ConfigMap that consumes
their outputs.

## Choose between catalog packages that answer one name

A catalog entry answers its own name, the package's `meta.roles` and
`meta.defaultAliases`, and the entry's `aliases`. When a required argument
names something more than one entry answers, Kix stops the build. Here a second entry, `postgres`, lists `database` among its aliases:

<Command {...contestedCheck} />

The error lists each entry and why it answers the name. To choose one,
declare it as an optional instance named after the dependency. Kix looks for
instances before the catalog, and still creates this one only when something
depends on it:

```nix title="cluster.nix"
instances.platform.database = {
  package = databasePackage;
  optional = true;
};
```

Alternatively, remove the other entries from `availablePackages`.

When the name is a role the environment already provides, set
`cluster.roles.<role>.binding` instead.

A name several entries answer is not an error while nothing requires it, so a
catalog built from a whole package set still evaluates. An argument the
consumer wires with `deps.<arg>` makes no demand on the catalog.

:::note[Explanation]
See [Dependency resolution and why explicit deps matter](/docs/explanation/the-dependency-graph/#dependency-resolution-and-explicit-deps)
for how Kix chooses among explicit instances, aliases, and catalog packages.
:::

:::note[Reference]
See [`availablePackages`](/docs/reference/cluster-configuration/#availablepackages),
[`autoNamespace`](/docs/reference/cluster-configuration/#autonamespace), and
[`optional`](/docs/reference/instances-and-composition/#optional) for the exact schemas.
:::