From b88b7b9623a06b1b6e5a3701c5923544d8eef190 Mon Sep 17 00:00:00 2001 From: lillecarl Date: Wed, 21 Jan 2026 17:28:08 +0000 Subject: [PATCH] derive apiMappings from CRDs in importers instead of kubernetes.nix Move CRD-to-apiMapping extraction into importyaml.nix and helm.nix where raw objects are available before module evaluation, breaking the infinite recursion cycle that occurred when kubernetes.nix read CRD data from config.kubernetes.objects (which triggered submodule evaluation that depended on apiMappings). Drop the apiVersions default in helm.nix that referenced apiMappings, since it created a dependency from helm objects back to apiMappings. Users can still pass a hardcoded list if a chart needs it. --- helm.nix | 66 ++++++++++++++++++++++++++++++++------------------ importyaml.nix | 45 +++++++++++++++++++++++++--------- kubernetes.nix | 1 - 3 files changed, 76 insertions(+), 36 deletions(-) diff --git a/helm.nix b/helm.nix index 8dc9505..7b445bc 100644 --- a/helm.nix +++ b/helm.nix @@ -89,12 +89,11 @@ in apiVersions = mkOption { description = '' - Inform Helm about which CRDs are available in the cluster (`--api-versions` option). + Inform Helm about which API versions are available in the cluster (`--api-versions` option). This is useful for charts which contain `.Capabilities.APIVersions.Has` checks. - If you use `kubernetes.customTypes` to make kubenix aware of CRDs, it will include those as well by default. ''; type = types.listOf types.str; - default = lib.attrValues globalConfig.kubernetes.apiMappings; + default = [ ]; }; objects = mkOption { @@ -143,24 +142,45 @@ in }; }; - config = { - kubernetes.objects = lib.pipe cfg.releases [ - (lib.mapAttrsToList ( - _: release: lib.map (object: lib.pipe object release.overrides) release.objects - )) - lib.flatten - (lib.map ( - object: - let - kind = object.kind or (throw "no kind for ${object}"); - name = object.metadata.name or (throw "no name for ${object}"); - namespace = object.metadata.namespace or "none"; - in - { - ${namespace}.${kind}.${name} = object; - } - )) - lib.mkMerge - ]; - }; + config = + let + allObjects = lib.pipe cfg.releases [ + (lib.mapAttrsToList ( + _: release: lib.map (object: lib.pipe object release.overrides) release.objects + )) + lib.flatten + ]; + in + { + kubernetes.objects = lib.pipe allObjects [ + (lib.map ( + object: + let + kind = object.kind or (throw "no kind for ${object}"); + name = object.metadata.name or (throw "no name for ${object}"); + namespace = object.metadata.namespace or "none"; + in + { + ${namespace}.${kind}.${name} = object; + } + )) + lib.mkMerge + ]; + kubernetes.apiMappings = lib.pipe allObjects [ + (lib.filter (object: object.kind or null == "CustomResourceDefinition")) + (map (crd: { + name = crd.spec.names.kind; + value = + let + version = lib.pipe crd.spec.versions [ + (lib.filter (x: x.storage or false == true)) + lib.head + (x: x.name) + ]; + in + "${crd.spec.group}/${version}"; + })) + lib.listToAttrs + ]; + }; } diff --git a/importyaml.nix b/importyaml.nix index 218496a..ce6ba2a 100644 --- a/importyaml.nix +++ b/importyaml.nix @@ -76,16 +76,37 @@ in type = types.attrsOf importyaml; default = { }; }; - config = { - kubernetes.objects = lib.pipe cfg [ - (lib.mapAttrsToList ( - _: importspec: lib.map (object: lib.pipe object importspec.overrides) importspec.objects - )) - lib.flatten - (lib.map (object: { - ${object.metadata.namespace or "none"}.${object.kind}.${object.metadata.name} = object; - })) - lib.mkMerge - ]; - }; + config = + let + allObjects = lib.pipe cfg [ + (lib.mapAttrsToList ( + _: importspec: lib.map (object: lib.pipe object importspec.overrides) importspec.objects + )) + lib.flatten + ]; + in + { + kubernetes.objects = lib.pipe allObjects [ + (lib.map (object: { + ${object.metadata.namespace or "none"}.${object.kind}.${object.metadata.name} = object; + })) + lib.mkMerge + ]; + kubernetes.apiMappings = lib.pipe allObjects [ + (lib.filter (object: object.kind or null == "CustomResourceDefinition")) + (map (crd: { + name = crd.spec.names.kind; + value = + let + version = lib.pipe crd.spec.versions [ + (lib.filter (x: x.storage or false == true)) + lib.head + (x: x.name) + ]; + in + "${crd.spec.group}/${version}"; + })) + lib.listToAttrs + ]; + }; } diff --git a/kubernetes.nix b/kubernetes.nix index 17aa514..5bc0b69 100644 --- a/kubernetes.nix +++ b/kubernetes.nix @@ -223,7 +223,6 @@ in in lib.listToAttrs (map objectToAttr data.resources); - generated = lib.pipe cfg.objects [ # Convert kubernetes.objects.namespace.kind.name into a list of objects (lib.collect (x: x ? apiVersion && x ? kind && x ? metadata))