From 9bf2cbec329b9caa17cdf130369ece43128c03aa Mon Sep 17 00:00:00 2001 From: Carl Andersson Date: Wed, 15 Oct 2025 02:45:41 +0200 Subject: [PATCH] Use overlays to extend pkgs instead of passing Helm specialArgs Move writeMultipleFiles to pkgs Add empty lib overlay --- default.nix | 14 ++++------- demo/helm.nix | 4 ++-- helm.nix | 3 +-- kluctl.nix | 41 +------------------------------ lib/default.nix | 3 +++ pkgs/chart2json.nix | 45 ++++++++++++++++++++++++++++++++++ pkgs/default.nix | 6 +++++ pkgs/fetchHelm.nix | 48 +++++++++++++++++++++++++++++++++++++ pkgs/writeMultipleFiles.nix | 38 +++++++++++++++++++++++++++++ 9 files changed, 149 insertions(+), 53 deletions(-) create mode 100644 lib/default.nix create mode 100644 pkgs/chart2json.nix create mode 100644 pkgs/default.nix create mode 100644 pkgs/fetchHelm.nix create mode 100644 pkgs/writeMultipleFiles.nix diff --git a/default.nix b/default.nix index 55affdb..c44007d 100644 --- a/default.nix +++ b/default.nix @@ -6,16 +6,13 @@ debug ? true, }: let - inherit (pkgs) lib; + pkgs' = pkgs.extend (import ./pkgs/default.nix); +in +let + pkgs = pkgs'; + lib = pkgs.lib; attrIf = condition: content: if condition then content else { }; - kubenix = builtins.fetchTree { - type = "github"; - owner = "hall"; - repo = "kubenix"; - ref = "e8577e661f3286624f79ce5ca3240a0ffcea9a7d"; - }; - eval = lib.evalModules { modules = [ ./internal.nix @@ -29,7 +26,6 @@ let specialArgs = { inherit pkgs; inherit (pkgs) lib; - helm = import "${kubenix}/lib/helm" { inherit pkgs; }; } // specialArgs; }; diff --git a/demo/helm.nix b/demo/helm.nix index 4d383a0..51a3bc0 100644 --- a/demo/helm.nix +++ b/demo/helm.nix @@ -1,5 +1,5 @@ # Helm example -{ helm, ... }: +{ pkgs, ... }: { config = { # Since we're only setting one final subattribute this is really the entire @@ -8,7 +8,7 @@ helm.releases.hccm = { namespace = "kube-system"; - chart = helm.fetch { + chart = pkgs.fetchHelm { repo = "https://charts.hetzner.cloud"; chart = "hcloud-cloud-controller-manager"; version = "1.27.0"; diff --git a/helm.nix b/helm.nix index a6da083..b492780 100644 --- a/helm.nix +++ b/helm.nix @@ -4,7 +4,6 @@ config, lib, pkgs, - helm, ... }: with lib; @@ -112,7 +111,7 @@ in ]; config.objects = importJSON ( - helm.chart2json { + pkgs.chart2json { inherit (config) chart name diff --git a/kluctl.nix b/kluctl.nix index 985b032..b6b498c 100644 --- a/kluctl.nix +++ b/kluctl.nix @@ -7,45 +7,6 @@ let cfg = config.kluctl; settingsFormat = pkgs.formats.json { }; - - writeMultipleFiles = - { - name, - files, - extraCommands ? "", - }: - let - fileList = lib.mapAttrsToList (path: file: { - inherit path; - content = file.content or file; - mode = if file.executable or false then "755" else file.mode or "644"; - }) files; - - # Create attribute names for passAsFile - passAsFileAttrs = builtins.listToAttrs ( - lib.imap0 (i: file: { - name = "file${toString i}"; - value = file.content; - }) fileList - ); - - passAsFileNames = builtins.attrNames passAsFileAttrs; - - commands = - (lib.imap0 (i: file: '' - mkdir -p $out/$(dirname "${file.path}") - cp "$file${toString i}Path" $out/${file.path} - chmod ${file.mode} $out/${file.path} - '') fileList) - ++ (lib.toList extraCommands); - - in - pkgs.runCommand name ( - passAsFileAttrs - // { - passAsFile = passAsFileNames; - } - ) (builtins.concatStringsSep "\n" commands); in { options = { @@ -113,7 +74,7 @@ in } ]; }; - kluctl.projectDir = writeMultipleFiles { + kluctl.projectDir = pkgs.writeMultipleFiles { name = "kluctlProject"; files = { ".kluctl.yaml" = { diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..131555a --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,3 @@ +_: lib: { + +} diff --git a/pkgs/chart2json.nix b/pkgs/chart2json.nix new file mode 100644 index 0000000..bd9a5a0 --- /dev/null +++ b/pkgs/chart2json.nix @@ -0,0 +1,45 @@ +{ runCommand, lib, kubernetes-helm, yq }: +with lib; +{ + # chart to template + chart + # release name +, name + # namespace to install release into +, namespace ? null + # values to pass to chart +, values ? { } + # kubernetes version to template chart for +, kubeVersion ? null + # whether to include CRD +, includeCRDs ? false + # whether to include hooks +, noHooks ? false + # Kubernetes api versions used for Capabilities.APIVersions (--api-versions) +, apiVersions ? null +}: +let + valuesJsonFile = builtins.toFile "${name}-values.json" (builtins.toJSON values); + # The `helm template` and YAML -> JSON steps are separate `runCommand` derivations for easier debuggability + resourcesYaml = runCommand "${name}.yaml" { nativeBuildInputs = [ kubernetes-helm ]; } '' + helm template "${name}" \ + ${optionalString (apiVersions != null && apiVersions != []) "--api-versions ${lib.strings.concatStringsSep "," apiVersions}"} \ + ${optionalString (kubeVersion != null) "--kube-version ${kubeVersion}"} \ + ${optionalString (namespace != null) "--namespace ${namespace}"} \ + ${optionalString (values != {}) "-f ${valuesJsonFile}"} \ + ${optionalString includeCRDs "--include-crds"} \ + ${optionalString noHooks "--no-hooks"} \ + ${chart} >$out + ''; +in +runCommand "${name}.json" { } '' + # Remove null values + ${yq}/bin/yq -Scs 'walk( + if type == "object" then + with_entries(select(.value != null)) + elif type == "array" then + map(select(. != null)) + else + . + end)' ${resourcesYaml} >$out +'' diff --git a/pkgs/default.nix b/pkgs/default.nix new file mode 100644 index 0000000..7d99e18 --- /dev/null +++ b/pkgs/default.nix @@ -0,0 +1,6 @@ +_: pkgs: { + lib = pkgs.lib.extend (import ../lib); + writeMultipleFiles = pkgs.callPackage ./writeMultipleFiles.nix { }; + fetchHelm = pkgs.callPackage ./fetchHelm.nix { }; + chart2json = pkgs.callPackage ./chart2json.nix { }; +} diff --git a/pkgs/fetchHelm.nix b/pkgs/fetchHelm.nix new file mode 100644 index 0000000..93b1c03 --- /dev/null +++ b/pkgs/fetchHelm.nix @@ -0,0 +1,48 @@ +{ stdenvNoCC, lib, kubernetes-helm, cacert }: +let + cleanName = lib.replaceStrings [ "/" ] [ "-" ]; +in +{ + # name of the chart + chart +, # chart url to fetch from custom location + chartUrl ? null +, # version of the chart + version ? null +, # chart hash + sha256 +, # whether to extract chart + untar ? true +, # use custom charts repo + repo ? null +, # pass --verify to helm chart + verify ? false +, # pass --devel to helm chart + devel ? false +, +}: +stdenvNoCC.mkDerivation { + name = "${cleanName chart}-${ if version == null then "dev" else version }"; + + buildCommand = '' + export HOME="$PWD" + echo "adding helm repo" + ${ if repo == null then "" else "helm repo add repository ${repo}" } + echo "fetching helm chart" + helm fetch -d ./chart \ + ${ if untar then "--untar" else ""} \ + ${ if version == null then "" else "--version ${version}" } \ + ${ if devel then "--devel" else "" } \ + ${ if verify then "--verify" else "" } \ + ${ if chartUrl == null then ( + if repo == null + then chart + else "repository/${chart}" + ) else chartUrl} + cp -r chart/*/ $out + ''; + outputHashMode = "recursive"; + outputHashAlgo = "sha256"; + outputHash = sha256; + nativeBuildInputs = [ kubernetes-helm cacert ]; +} diff --git a/pkgs/writeMultipleFiles.nix b/pkgs/writeMultipleFiles.nix new file mode 100644 index 0000000..c9bcc5f --- /dev/null +++ b/pkgs/writeMultipleFiles.nix @@ -0,0 +1,38 @@ +{ runCommand, lib }: +{ + name, + files, + extraCommands ? "", +}: +let + fileList = lib.mapAttrsToList (path: file: { + inherit path; + content = file.content or file; + mode = if file.executable or false then "755" else file.mode or "644"; + }) files; + + # Create attribute names for passAsFile + passAsFileAttrs = builtins.listToAttrs ( + lib.imap0 (i: file: { + name = "file${toString i}"; + value = file.content; + }) fileList + ); + + passAsFileNames = builtins.attrNames passAsFileAttrs; + + commands = + (lib.imap0 (i: file: '' + mkdir -p $out/$(dirname "${file.path}") + cp "$file${toString i}Path" $out/${file.path} + chmod ${file.mode} $out/${file.path} + '') fileList) + ++ (lib.toList extraCommands); + +in +runCommand name ( + passAsFileAttrs + // { + passAsFile = passAsFileNames; + } +) (builtins.concatStringsSep "\n" commands)