Nix to Kubernetes

I was thinking recently about the possibility of a NixOps style tool that would deploy into a Kubernetes cluster. The idea would be that services be build and pushed as containers, and then K8s deployments created for pods, as described in a Nix expression.

What I was wondering is if there’s prior art around this. I know I’ve seen a few articles about building Docker images with Nix, but I can’t recall (and searching Discourse doesn’t reveal) discussion about using Kubernetes with it.


I’ve actually used it a lot recently via GitHub - xtruder/kubenix: Kubernetes resource builder using nix.
While it’s not exactly documented how to use it… it’s not very difficult:

My default.nix:

{ pkgs ? import <nixpkgs> {} }:
  kubenix = import (pkgs.fetchFromGitHub {
    owner = "xtruder";
    repo = "kubenix";
    rev = "7287c4ed9ee833ccbce2185038c068bac9c77e7c";
    sha256 = "1f69h31nfpifa6zmgrxiq72cchb6xmrcsy68ig9n8pmrwdag1lgq";
  }) { inherit pkgs; };
  kubenix.buildResources { configuration = ./configuration.nix; }

Depending on your configuration.nix, building this will give you a JSON file as result, that you can then directly apply via kubectl apply -f $(nix-build)

For many examples of stuff you can put into configuration.nix, look at the kubenix examples and GitHub - xtruder/kubenix-modules: Repository of kubenix services.

If you’ve got more questions, i can probably help you (I struggled for a bit with ejson setup for example).


I wanted to use this project but I had enough trouble trying to deploy a 3 node kubernetes cluster using NixOS and nixops (kept on running into errors). Do you have any advice on deploying a kubernetes cluster using the nixpkgs service?

Can’t help you with that unfortunately, I simply run minikube locally and use a common Kubernetes at work.
Might help if you can show those errors.

Can’t you distribute using kubernetes?
Setting up one machine as a master?

Call to arms! — while I took on the task myself, in the meantime. But a little collaborative momentum would be always great.

1 Like