Use overlays to extend pkgs instead of passing Helm specialArgs

Move writeMultipleFiles to pkgs
Add empty lib overlay
This commit is contained in:
Carl Andersson 2025-10-15 02:45:41 +02:00
commit 9bf2cbec32
9 changed files with 149 additions and 53 deletions

View file

@ -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;
};

View file

@ -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";

View file

@ -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

View file

@ -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" = {

3
lib/default.nix Normal file
View file

@ -0,0 +1,3 @@
_: lib: {
}

45
pkgs/chart2json.nix Normal file
View file

@ -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
''

6
pkgs/default.nix Normal file
View file

@ -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 { };
}

48
pkgs/fetchHelm.nix Normal file
View file

@ -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 ];
}

View file

@ -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)