Skip to content

Add env vars and mounts

This content is for the v0.1 version. Switch to the latest version for up-to-date documentation.

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.

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

how-to/application/web-package.nix (L96–L98)
env = kix.mkEnvVars {
APP_ENV = config.environment;
};

View source on GitHub ↗

The rendered container contains:

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:

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

This package stores its page content in a ConfigMap:

how-to/application/web-package.nix (L74–L79)
content = scope.mkResource {
apiVersion = "v1";
kind = "ConfigMap";
inherit name;
data."index.html" = "${config.message}\n";
};

View source on GitHub ↗

Create a mount from the wrapped resource:

how-to/application/web-package.nix (L65–L69)
contentMount = kix.mount.configMap self.content {
name = "content";
mountPath = "/usr/share/nginx/html";
readOnly = true;
};

View source on GitHub ↗

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.

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

how-to/application/web-package.nix (L116–L116)
|> kix.withMounts [ contentMount ]

View source on GitHub ↗

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:

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:

Run in kix-examples/ Output excerpt
❱ kix build how-to-application --output json Show output
{
  "containers": [
    {
      "name": "nginx",
      "env": [
        {
          "name": "APP_ENV",
          "value": "production"
        }
      ],
      "volumeMounts": [
        {
          "mountPath": "/usr/share/nginx/html",
          "name": "content",
          "readOnly": true
        }
      ]
    }
  ],
  "volumes": [
    {
      "configMap": {
        "name": "production"
      },
      "name": "content"
    }
  ]
}

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.