Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

build-helm-chart.nix

Nix tooling for Helm chart repositories: a buildHelmChart function, and a flake-parts module that packages, checks and releases any number of charts from one flake.

Status: Beta

Features

  • buildHelmChart: lints and packages a chart
  • Multiple charts per flake: each chart under helmCharts.charts.<name> gets its own package, checks and release scripts.
  • Checks: lint and package build, a helm install --dry-run=client render, an optional app-version comparison against nixpkgs, and more.
  • Release helpers: towncrier changelog builds, release commits and git tags per chart.
  • Dev shell and an optional treefmt setup.
  • Template to start a new chart repository in one command.

Quick start

mkdir my-charts && cd my-charts
nix flake init -t git+https://git.bartoostveen.nl/bart/build-helm-chart.nix
git init && git add .
nix flake lock
nix develop

Usage

See the template.

Outputs

For every chart <name>:

OutputDescription
packages.defaultThe chart named by helmCharts.defaultChart, or the only chart if there is exactly one
packages.<name>The packaged chart
packages.towncrier-build-<name>Builds the changelog with towncrier for the chart’s version
packages.get-changelog-<name>Prints the lines the last commit added to the changelog
packages.tag-release-<name>Runs the changelog build, commits and creates the git tag
checks.<name>The package build (including helm lint)
checks.<name>-renderhelm install --dry-run=client of the chart
checks.<name>-versionChecks the app version against the appPackages version (only if appPackage is set)
checks.<name>-<k>Each entry of extraChecks
devShells.helm-charts/devShells.defaultA devshell with handy utilities

Options

All options live under perSystem.helmCharts. See full documentation at https://build-helm-chart.bartoostveen.nl.

Releasing

  1. Add news fragments with towncrier create.
  2. Bump version in Chart.yaml.
  3. Run nix run .#tag-release-<name> (or tag-release-<name> in the devshell).

The script runs towncrier build for the chart’s version, executes git add ., commits as chore: Release <name> <version> and tags <tagPrefix><version>. Extra arguments are passed to git tag.

To release several charts independently, give each its own towncrier.toml, changelog.d/ and CHANGELOG.md, and also set:

charts.my-chart.release.directory = "my-chart";

Formatting

flakeModules.treefmt configures treefmt-nix with nixfmt, statix, deadnix, shellcheck, keep-sorted, prettier, yamlfmt and markdownlint. The programs are enabled with mkDefault, so you can switch any of them off. Run it with nix fmt.

Using buildHelmChart

overlays.default adds buildHelmChart to your package set, which is an extension of stdenv.mkDerivation:

{
  buildHelmChart,
  lib,
}:

buildHelmChart {
  pname = "git-pages-chart";
  version = "1.0.0";

  src = ./git-pages;

  meta = {
    description = "git-pages Helm chart";
    homepage = "https://git.bartoostveen.nl/bart/git-pages-chart.git";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ bartoostveen ];
  };
}

Hooks

HookPhaseDescriptionTurn off
helmCheckHookcheckPhaseRuns helm lint for each chartdontHelmCheck = true;
helmBuildHookbuildPhasehelm package into dist/dontHelmBuild = true;
helmInstallHookinstallPhaseInstalls the resulting chartdontHelmInstall = true;
buildHelmChart {
  # ...
  helmLintFlags = [ "--strict" ]; # default
  helmPackageFlags = [ "--dependency-update" ]; # needs network or vendored charts/
  postBuild = "ls dist";
}

License

This utility is provided under the MIT License.

Flake module options

These options are provided by flakeModules.default and live under perSystem.

perSystem.helmCharts.charts

Helm charts to package, keyed by name.

Type: attribute set of (submodule)

Default:

{ }

Declared by:

perSystem.helmCharts.charts.<name>.package

The built chart package

Type: package

Default: runs helm package

Declared by:

perSystem.helmCharts.charts.<name>.appPackage

Package providing the application, e.g. from nixpkgs. When set, a <name>-version check fails if its version is newer than appVersion and warns if it is older.

Type: null or package

Default:

null

Example:

pkgs.git-pages

Declared by:

perSystem.helmCharts.charts.<name>.appVersion

Application version.

Type: string

Default: appVersion from Chart.yaml

Declared by:

perSystem.helmCharts.charts.<name>.extraChecks

Extra checks, exposed as checks.<name>-<attr>.

Type: attribute set of package

Default:

{ }

Example:

{ e2e = pkgs.callPackage ./vm-test.nix { }; }

Declared by:

perSystem.helmCharts.charts.<name>.path

Directory containing Chart.yaml.

Type: absolute path

Example:

./charts/my-chart

Declared by:

perSystem.helmCharts.charts.<name>.release.enable

Generate towncrier/changelog/tag helper scripts for this chart.

Type: boolean

Default:

true

Declared by:

perSystem.helmCharts.charts.<name>.release.changelog

Repo-relative path of the changelog file.

Type: string

Default:

"${release.directory}/CHANGELOG.md"

Declared by:

perSystem.helmCharts.charts.<name>.release.directory

Repo-relative directory holding towncrier config and news fragments.

Type: string

Default:

"."

Example:

"charts/my-chart"

Declared by:

perSystem.helmCharts.charts.<name>.release.tagPrefix

Prefix for git tags created by the tag-release script.

Type: string

Default:

"${name}-v"

Example:

"v"

Declared by:

perSystem.helmCharts.charts.<name>.release.towncrier

towncrier package.

Type: package

Default:

pkgs.python3Packages.towncrier

Declared by:

perSystem.helmCharts.charts.<name>.render.enable

Add a check that renders the chart with helm install --dry-run=client.

Type: boolean

Default:

true

Declared by:

perSystem.helmCharts.charts.<name>.render.extraArgs

Extra arguments for the render check’s helm install.

Type: list of string

Default:

[ ]

Example:

[
  "--values"
  "ci/values.yaml"
]

Declared by:

perSystem.helmCharts.charts.<name>.version

Chart version.

Type: string

Default: version from Chart.yaml

Declared by:

perSystem.helmCharts.defaultChart

Chart exposed as packages.default. If null and exactly one chart is defined, that chart is used.

Type: null or string

Default:

null

Declared by:

perSystem.helmCharts.devShell.enable

Whether to enable the default devshell for helm charts.

Type: boolean

Default:

false

Example:

true

Declared by:

perSystem.helmCharts.devShell.extraPackages

Extra packages for the dev shell.

Type: list of package

Default:

[ ]

Declared by: