Pod

The simplest, but not incredibly useful, example is likely deploying a bare pod.

Which we can do with the kubernetes.resources.pods option:

    default.nix
    
# let's creata a function whose only input is the kubenix package
{ kubenix ? import ../../../.. }:
# evalModules is our main entrypoint
kubenix.evalModules.${builtins.currentSystem} {
  # to it, we pass a module that accepts a (different) kubenix object
  module = { kubenix, ... }: {
    # in order to define options, we need to import their definitions
    imports = [ kubenix.modules.k8s ];
    # now we have full access to define Kubernetes resources
    kubernetes.resources.pods = {
      # "example" is the name of our pod
      example.spec.containers = {
        # "ex" is the name of the container in our pod
        ex.image = "nginx";
      };
    };
  };
}

Here, example is an arbitrary string which identifies the pod (just as ex identifies a container within the pod).

NOTE

The format under kubernetes.resources largely mirrors that of the Kubernetes API which can generally be explored with kubectl; e.g.

kubectl explain pod.spec.containers

However, our format uses the plural form and injects resource names where appropriate.

Create a json manifest with:

nix eval -f . --json config.kubernetes.generated

which should output something like this:

{
  "apiVersion": "v1",
  "items": [
    {
      "apiVersion": "v1",
      "kind": "Pod",
      "metadata": {
        "annotations": {
          "kubenix/k8s-version": "1.24",
          "kubenix/project-name": "kubenix"
        },
        "labels": {
          "kubenix/hash": "6e6ccbb6787f9b600737f8882d2487eeef84af9f"
        },
        "name": "example"
      },
      "spec": {
        "containers": [
          {
            "image": "nginx",
            "name": "ex"
          }
        ]
      }
    }
  ],
  "kind": "List",
  "labels": {
    "kubenix/hash": "6e6ccbb6787f9b600737f8882d2487eeef84af9f",
    "kubenix/k8s-version": "1.24",
    "kubenix/project-name": "kubenix"
  }
}