Skip to content
Install the CLI

How-to guide Platform capabilities

Use SOPS-backed secrets

These docs describe Kix's main branch, which can include changes not yet released. Read the docs for the latest release.

Use a SOPS-backed Secret when Kix should own the Kubernetes Secret but its values must remain encrypted in the repository and build output. Kix evaluates the encrypted file into the cluster artifact, then runs sops --decrypt immediately before it applies the Secret.

This guide uses an Age identity. Install SOPS and Age on the workstation where you create and encrypt the secret file, and make sure the machine that runs kix deploy can access the decryption identity (if you use PGP keys instead of Age or cloud KMS, gpg must also be on PATH).

Generate an identity and print its recipient:

❱ age-keygen -o age-key.txt
❱ age-keygen -y age-key.txt

Keep age-key.txt outside the repository. Your deployment environment needs the identity, while developers only need the recipient to encrypt changes.

Create the file with the keys your package expects:

credentials.enc.yaml
username: application-user
password: replace-with-a-generated-password

Encrypt it in place. The shell variable keeps the full recipient out of later commands:

❱ recipient=$(age-keygen -y age-key.txt)
❱ sops encrypt --age "$recipient" --in-place credentials.enc.yaml

Open the file before committing it and confirm that the values have become ENC[...] entries. Commit the encrypted file, but not age-key.txt.

Flux repositories often encrypt a full Kubernetes Secret manifest (apiVersion, kind, metadata, stringData, and type). Kix expects a flat key-value map where each top-level key is a Secret key, and refuses a full manifest with error [K6746].

First, check .sops.yaml. Flux rules often set encrypted_regex: ^(data|stringData)$, which leaves every value in a flat file unencrypted because there is no data or stringData key. Make sure the rule matching credentials.enc.yaml has no encrypted_regex.

If the Flux manifest stores plain-text values under stringData, extract stringData and re-encrypt the new file in place:

❱ sops decrypt --extract '["stringData"]' flux-file.yaml > credentials.enc.yaml
❱ sops encrypt --age "$recipient" --in-place credentials.enc.yaml

credentials.enc.yaml contains plain text between those two commands, so run them in a private directory and confirm every value is ENC[...] before committing.

If the Flux manifest stores base64-encoded values under data instead of stringData, decode them to plain text before encrypting. Kix writes the decrypted values into the Secret verbatim.

Pass the encrypted file to scope.mkSecret and declare its keys explicitly:

how-to/platform/sops-secret-app.nix
credentials = scope.mkSecret {
name = "${scope.instanceName}-credentials";
keys = [
"username"
"password"
];
source = ./sops-credentials.enc.yaml;
};

View source on GitHub ↗

Kix cannot derive the key names from encrypted values, so keys is required. It also lets the out helpers catch misspelled keys during evaluation.

This validation checks how the package uses the declared keys, not whether the encrypted file contains them. If the file omits a declared key, Kix can apply the Secret successfully and the workload fails when it tries to read the key. By contrast, the deploy-time readiness check for scope.mkSecretRef verifies that every declared key exists in the live Secret.

Kix records the SHA-256 of the encrypted file in the kix.run/sops-digest annotation. Before it decrypts the file, kix deploy checks the file against that digest and stops if they differ. You do not write a hash yourself. When you change the encrypted file, the Secret changes in the next build, and the pods that read it restart.

Consume the Secret in the same way as any other Kix-managed Secret:

how-to/platform/sops-secret-app.nix
deployment = scope.mkDeployment {
name = scope.instanceName;
spec = {
replicas = 1;
selector.matchLabels = scope.selectorLabels;
template.spec.containers = [
{
name = "app";
image = "busybox:1.36";
command = [
"sh"
"-c"
"sleep 3600"
];
env = self.credentials.out.mkEnv {
APP_USERNAME = "username";
APP_PASSWORD = "password";
};
}
];
};
};

View source on GitHub ↗

The reference from deployment to credentials also gives Kix the deploy ordering between them.

Render the cluster before deploying:

kix-examples/ output excerpt
❱ kix build how-to-platform-sops --output json
[
  {
    "kind": "Secret",
    "namespace": "sops-example",
    "name": "app-credentials",
    "declaredKeys": [
      "username",
      "password"
    ],
    "plaintextIncluded": false
  }
]

The output excerpt reports the declared keys and confirms that the rendered Secret has neither data nor stringData. The artifact contains a reference to the encrypted source, not the decrypted values.

Point SOPS at the identity on the deployment machine, then deploy normally:

kix-examples/
❱ export SOPS_AGE_KEY_FILE=/secure/path/age-key.txt
❱ kix deploy how-to-platform-sops

With kix deploy --operator, the Kix operator decrypts SOPS files inside the cluster, so the operator pod must have access to the decryption key. See Secrets from SOPS files for how to configure it.

When a Secret is new, changed, or re-applied with --reconcile, kix plan, kix deploy, and kix rollback validate its SOPS file during their cluster-connected dry run (unchanged Secrets and offline or read-only commands skip this check; kix plan --bundle checks the encrypted file without decrypting it). Kix stops before applying anything if SOPS is unavailable, the identity cannot decrypt the file, or any top-level value is a map, a list, or null ([K6746]). Use a flat YAML or JSON map where every value is a string, a number, true, or false, and store JSON meant for one key as a string (local deploys also accept dotenv; bundles do not, and INI files fail with [K6746] because sops groups keys into section maps).

Pass kix deploy --on-error continue if you need to apply unrelated changes while leaving the refused Secret unchanged and skipping resources that depend on it. kix rollback always stops on [K6746] when rolling back to a build created before the file was converted: pick a newer deployment record, or check out the target commit, convert its file, and run kix deploy.

When the Secret also sets plain keys in stringData (such as username or host, listed in plainKeys), kix deploy keeps those keys alongside the keys decrypted from the SOPS file.

Check that Kubernetes received the two declared keys without printing their values:

kix-examples/
❱ kubectl get secret app-credentials -n sops-example
NAME              TYPE     DATA   AGE
app-credentials   Opaque   2      14s

Give the deployment identity to CI through its secret store or workload identity mechanism. If another controller should own the Kubernetes Secret, use an externally managed Secret instead.