# Add env vars and mounts

Kix provides helpers for the repetitive parts of container environment and
volume configuration. Use them to keep the source resource attached to the
workload dependency graph.

This guide uses the package created in
[Create a local package](/docs/v0.1/how-to/author-packages-and-clusters/create-a-local-package/).

## Add environment variables

`kix.mkEnvVars` converts an attribute set into the list Kubernetes expects:

<Snippet {...environmentVariables} />

The rendered container contains:

```yaml
env:
  - name: APP_ENV
    value: production
```

Entries whose value is `null` are omitted. Use `kix.mkEnv` when you need to
combine simple values with raw Kubernetes entries such as `secretKeyRef` or
`fieldRef`:

```nix title="web-package.nix"
env = kix.mkEnv
  {
    APP_ENV = config.environment;
    OPTIONAL_VALUE = config.optionalValue;
  }
  [
    {
      name = "POD_NAME";
      valueFrom.fieldRef.fieldPath = "metadata.name";
    }
  ];
```

## Create the resource to mount

This package stores its page content in a ConfigMap:

<Snippet {...contentConfigMap} />

Create a mount from the wrapped resource:

<Snippet {...configMount} />

Passing `self.content` is significant. The mount retains the dependency on the
ConfigMap, so Kix can order the resources and include changes to the content in
the workload identity.

The other mount helpers follow the same shape:

* `kix.mount.secret` mounts a Secret.
* `kix.mount.pvc` mounts a PersistentVolumeClaim.
* `kix.mount.emptyDir` creates an ephemeral volume.
* `kix.mount.hostPath` mounts a node path.

## Apply mounts to the workload

Pass the mount list through `kix.withMounts` before wrapping the Deployment:

<Snippet {...applyMount} />

With one container, Kix adds the volumes and volume mounts automatically. For
a Pod template with several containers, use `kix.withMountsOn "container-name"`
to select the recipient.

Null entries in the mount list are ignored, which makes optional mounts easy
to express with an `if` expression:

```nix title="web-package.nix"
|> kix.withMounts [
  contentMount
  (if config.cache.enable then cacheMount else null)
]
```

Render the cluster and inspect the Deployment's `env`, `volumeMounts`, and
`volumes` fields. This excerpt shows the container wiring the helpers
produced:

<Command expandable {...deploymentEnv} />

The environment variables come from the helper calls above. The mount pairs
the `volumeMounts` entry on the container with the `volumes` entry that names
the ConfigMap.

:::note[Reference]
See [Environment helpers](/docs/v0.1/reference/kix-helpers/env-helpers/),
[mount helpers](/docs/v0.1/reference/kix-helpers/mount-helpers/), and
[`withMounts` and `withMountsOn`](/docs/v0.1/reference/kix-helpers/withmounts-withmountson/)
for the complete helper signatures.
:::