Skip to content

Libary Functions#

The argument lib is passed to each module in nixidy. This is the standard nixpkgs library extended with the following functions.

lib.helm.downloadHelmChart#

Type: downloadHelmChart :: AttrSet -> Derivation

Downloads a helm chart from a helm registry.

This is re-exported directly from farcaller/nix-kube-generators.

lib.helm.buildHelmChart#

Type: buildHelmChart :: AttrSet -> Derivation

Templates a helm chart with provided values and creates a derivation with the output.

This is re-exported directly from farcaller/nix-kube-generators.

lib.helm.getChartValues#

Type: getChartValues :: Derivation -> AttrSet

Parse the default values file shipped with the helm chart.

chart

Derivation containing helm chart. Usually output of lib.helm.downloadHelmChart.

Example:

getChartValues (lib.helm.downloadHelmChart {
    repo = "https://argoproj.github.io/argo-helm/";
    chart = "argo-cd";
    version = "5.51.4";
    chartHash = "sha256-LOEJ5mYaHEA0RztDkgM9DGTA0P5eNd0SzSlwJIgpbWY=";
})
=> {
  server.replicas = 1;
  controller.replicas = 1;
  # ...
}

lib.helm.mkChartAttrs#

Type: mkChartAttrs :: Path -> AttrSet

Walk a directory tree and import all default.nix to download helm charts.

The default.nix needs to have the following format:

./charts/kubernetes-csi/csi-driver-nfs/default.nix
{
  repo = "https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/charts";
  chart = "csi-driver-nfs";
  version = "4.7.0";
  chartHash = "sha256-EU2qaZglUU3vxa41l1p/2yBscksIhYMr8kSgH8t0vL8=";
}

Two optional fields are recognised by the update script:

  • versionConstraint: a semver range (e.g. ">=4.7.0 <5.0.0") passed to helm show chart --version so the updater resolves the latest matching release instead of the absolute latest.
  • freeze: when true, the update script skips this chart entirely.

Each chart derivation carries a passthru.updateScript that resolves the latest upstream version (honouring versionConstraint and freeze) and rewrites the chart's default.nix in place. Use mkChartsUpdateScript to run every chart's update script at once.

rootDir

Path to a directory containing the correct directory structure described above.

Example:

mkChartAttrs ./charts
=> {
  kubernetes-csi = {
    csi-driver-nfs = lib.helm.downloadHelmChart {
      repo = "https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/charts";
      chart = "csi-driver-nfs";
      version = "4.7.0";
      chartHash = "sha256-EU2qaZglUU3vxa41l1p/2yBscksIhYMr8kSgH8t0vL8=";
    };
  };
}

lib.helm.mkChartsUpdateScript#

Type: mkChartsUpdateScript :: AttrSet -> Derivation

Build a single shell script that runs every chart's updateScript sequentially. The script keeps going when an individual chart fails and exits non-zero at the end if any chart failed.

tree

Tree of helm chart derivations, as produced by lib.helm.mkChartAttrs.

Example:

mkChartsUpdateScript (mkChartAttrs ./charts)
=> <derivation update-all-charts>

lib.kustomize.buildKustomization#

Type: buildKustomization :: AttrSet -> Derivation

Builds a kustomization and creates a derivation with the output.

structured function argument

name

Name is only used for derivation name.

src

Derivation containing the kustomization entrypoint and all relative bases that it might reference.

path

Relative path from the base of src to the kustomization folder to render.

namespace

Override namespace in kustomization.yaml.

Example:

buildKustomization {
  name = "argocd";
  src = pkgs.fetchFromGitHub {
    owner = "argoproj";
    repo = "argo-cd";
    rev = "v2.9.3";
    hash = "sha256-GaY4Cw/LlSwy35umbB4epXt6ev8ya19UjHRwhDwilqU=";
  };
  path = "manifests/cluster-install";
  namespace = "argocd";
}
=> /nix/store/7i52...7pww-kustomize-argocd

lib.kube.fromYAML#

Type: fromYAML :: String -> [AttrSet]

Parses a YAML document string into a list of attribute sets.

This is re-exported directly from farcaller/nix-kube-generators.

yaml

String with a yaml document.

Example:

fromYAML ''
  apiVersion: v1
  kind: Namespace
  metadata:
    name: default
  ---
  apiVersion: v1
  kind: Namespace
  metadata:
    name: kube-system
''
=> [
  {
    apiVersion = "v1";
    kind = "Namespace";
    metadata.name = "default";
  }
  {
    apiVersion = "v1";
    kind = "Namespace";
    metadata.name = "kube-system";
  }
]

lib.kube.fromOctal#

Type: fromOctal :: String -> Integer

Parse an octal representation of a number and convert into a decimal number. This can be useful when having to represent permission bits in a resource as nix has no support for representing octal numbers.

octal

String representation of the octal number to parse.

Example:

fromOctal "0555"
=> 365

lib.kube.removeLabels#

Type: removeLabels :: [String] -> AttrSet -> AttrSet

Removes labels from a Kubernetes manifest.

labels

List of labels that should be removed

manifest

Kubernetes manifest

Example:

removeLabels ["helm.sh/chart"] {
  apiVersion = "v1";
  kind = "ConfigMap";
  metadata = {
    name = "argocd-cm";
    labels = {
      "app.kubernetes.io/name" = "argocd-cm";
      "helm.sh/chart" = "argo-cd-5.51.6";
    };
  };
}
=> {
  apiVersion = "v1";
  kind = "ConfigMap";
  metadata = {
    name = "argocd-cm";
    labels = {
      "app.kubernetes.io/name" = "argocd-cm";
    };
  };
}