# Use aliases and default aliases

Use an alias when a package should satisfy a dependency name that differs from
its instance name. Put a reusable default on the package, or set an alias on
one instance when the mapping belongs to a particular cluster.

## Declare a package default alias

Add `meta.defaultAliases` when every instance of a package provides the same
kind of dependency. A persistent-volume-claim package can advertise itself as
`storage` even when cluster authors choose names such as `data` or `uploads`:

```nix title="packages/persistent-volume-claim/default.nix"
{
  meta = {
    version = "1.0.0";
    defaultAliases = [ "storage" ];
  };

  build = { ... }: {
    # Package resources.
  };
}
```

A consumer declares the alias as its build argument:

```nix title="packages/web/default.nix"
build = { storage, ... }: {
  dataMount = kix.mount.pvc storage { mountPath = "/data"; };
  # Other package resources.
};
```

The cluster can now give the provider a task-specific instance name:

```nix title="cluster.nix"
instances.apps.data = {
  package = packages.persistent-volume-claim;
};
```

Kix indexes the instance under both `data` and `storage`.

## Set aliases on one instance

Use the instance's `aliases` field when the name applies only in this cluster:

```nix title="cluster.nix"
instances.apps.redis-primary = {
  package = packages.valkey;
  aliases = [ "cache" ];
};
```

A package with a required `cache` build argument can now resolve this
instance.

An instance alias list replaces `meta.defaultAliases` for that instance, and
`aliases = [ ]` removes them. Include any package defaults you still need:

```nix title="cluster.nix"
aliases = [ "cache" "redis" ];
```

Package roles remain available as aliases even when instance aliases replace
the defaults.

## Check for collisions

Evaluate the cluster after adding an alias:

<Command commands={["kix check production"]} />

If more than one instance in the consumer's namespace has the requested name
or alias, Kix reports that the dependency matches multiple instances. Wire
that consumer to the provider it should use:

```nix title="cluster.nix"
instances.apps.web = {
  package = packages.web;
  deps.cache = ref.apps.redis-primary;
};
```

This records the choice at the consumer, where the dependency is easiest to
understand.

Aliases can also make two `availablePackages` entries answer one name. See
[Choose between catalog packages that answer one name](/docs/how-to/debug-dependency-wiring/use-auto-instantiation-with-availablepackages-optional-and-autonamespace/#choose-between-catalog-packages-that-answer-one-name).

:::note[Explanation]
See [Dependency resolution and why explicit deps matter](/docs/explanation/the-dependency-graph/#dependency-resolution-and-explicit-deps)
for resolution order and ambiguity handling.
:::

:::note[Reference]
See [`aliases`](/docs/reference/instances-and-composition/#aliases) and
[`meta.defaultAliases`](/docs/reference/package-anatomy/#meta) for the exact
schemas.
:::