switch formatting to nixpkgs-fmt

This commit is contained in:
Bryton Hall 2023-07-07 22:01:34 -04:00
parent 3598716c73
commit 2712e89716
65 changed files with 4839 additions and 5222 deletions

View file

@ -1,8 +1,5 @@
{
lib,
pkgs,
}: {
k8s = import ./k8s {inherit lib;};
docker = import ./docker {inherit lib pkgs;};
helm = import ./helm {inherit pkgs;};
{ lib, pkgs }: {
k8s = import ./k8s { inherit lib; };
docker = import ./docker { inherit lib pkgs; };
helm = import ./helm { inherit pkgs; };
}

View file

@ -1,13 +1,6 @@
{
lib,
pkgs,
}:
{ lib, pkgs }:
with lib; {
copyDockerImages = {
images,
dest,
args ? "",
}:
copyDockerImages = { images, dest, args ? "" }:
pkgs.writeScript "copy-docker-images.sh" (concatMapStrings
(image: ''
#!${pkgs.runtimeShell}

View file

@ -1,46 +1,42 @@
{
runCommand,
lib,
kubernetes-helm,
yq,
}:
{ 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,
}: 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 (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
''
{
# 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
}:
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 (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
''

View file

@ -1,4 +1,4 @@
{pkgs}: {
chart2json = pkgs.callPackage ./chart2json.nix {};
fetch = pkgs.callPackage ./fetchhelm.nix {};
{ pkgs }: {
chart2json = pkgs.callPackage ./chart2json.nix { };
fetch = pkgs.callPackage ./fetchhelm.nix { };
}

View file

@ -1,80 +1,48 @@
{
stdenvNoCC,
lib,
kubernetes-helm,
cacert,
}: let
cleanName = lib.replaceStrings ["/"] ["-"];
{ 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
}";
{
# 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];
}
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

@ -1,7 +1,9 @@
{pkgs ? import <nixpkgs> {}}: let
fetchhelm = pkgs.callPackage ./fetchhelm.nix {};
chart2json = pkgs.callPackage ./chart2json.nix {};
in rec {
{ pkgs ? import <nixpkgs> { } }:
let
fetchhelm = pkgs.callPackage ./fetchhelm.nix { };
chart2json = pkgs.callPackage ./chart2json.nix { };
in
rec {
postgresql-chart = fetchhelm {
chart = "stable/postgresql";
version = "0.18.1";

View file

@ -1,42 +1,32 @@
{lib}:
{ lib }:
with lib; rec {
# TODO: refactor with mkOptionType
mkSecretOption = {
description ? "",
default ? {},
allowNull ? true,
}:
mkSecretOption = { description ? "", default ? { }, allowNull ? true }:
mkOption {
inherit description;
type =
(
if allowNull
then types.nullOr
else id
) (types.submodule {
options = {
name = mkOption ({
description = "Name of the secret where secret is stored";
type = types.str;
default = default.name;
}
// (optionalAttrs (default ? "name") {
default = default.name;
}));
type = (
if allowNull
then types.nullOr
else id
) (types.submodule {
options = {
name = mkOption ({
description = "Name of the secret where secret is stored";
type = types.str;
default = default.name;
} // (optionalAttrs (default ? "name") {
default = default.name;
}));
key = mkOption ({
description = "Name of the key where secret is stored";
type = types.str;
}
// (optionalAttrs (default ? "key") {
default = default.key;
}));
};
});
default =
if default == null
then null
else {};
key = mkOption ({
description = "Name of the key where secret is stored";
type = types.str;
} // (optionalAttrs (default ? "key") {
default = default.key;
}));
};
});
default = if default == null then null else { };
};
secretToEnv = value: {
@ -46,10 +36,7 @@ with lib; rec {
};
# Creates kubernetes list from a list of kubernetes objects
mkList = {
items,
labels ? {},
}: {
mkList = { items, labels ? { } }: {
kind = "List";
apiVersion = "v1";
@ -57,27 +44,22 @@ with lib; rec {
};
# Creates hashed kubernetes list from a list of kubernetes objects
mkHashedList = {
items,
labels ? {},
}: let
hash = builtins.hashString "sha1" (builtins.toJSON items);
mkHashedList = { items, labels ? { } }:
let
hash = builtins.hashString "sha1" (builtins.toJSON items);
labeledItems =
map
(item:
recursiveUpdate item {
metadata.labels."kubenix/hash" = hash;
})
items;
in
labeledItems = map
(item:
recursiveUpdate item {
metadata.labels."kubenix/hash" = hash;
})
items;
in
mkList {
items = labeledItems;
labels =
{
"kubenix/hash" = hash;
}
// labels;
labels = {
"kubenix/hash" = hash;
} // labels;
};
inherit (lib) toBase64;

View file

@ -1,30 +1,27 @@
{
lib,
pkgs,
}:
{ lib, pkgs }:
with lib; let
self = {
importYAML = path:
importJSON (pkgs.runCommand "yaml-to-json" {} ''
importJSON (pkgs.runCommand "yaml-to-json" { } ''
${pkgs.yq}/bin/yq -c . ${path} > $out
'');
toYAML = config:
builtins.readFile (pkgs.runCommand "to-yaml" {} ''
builtins.readFile (pkgs.runCommand "to-yaml" { } ''
${pkgs.yq}/bin/yq -y . ${pkgs.writeText "to-json" (builtins.toJSON config)} > $out
'');
toMultiDocumentYaml = name: documents:
pkgs.runCommand name {}
(concatMapStringsSep "\necho --- >> $out\n"
(
d: "${pkgs.yq}/bin/yq -y . ${pkgs.writeText "to-json" (builtins.toJSON config)} >> $out"
)
documents);
pkgs.runCommand name { }
(concatMapStringsSep "\necho --- >> $out\n"
(
d: "${pkgs.yq}/bin/yq -y . ${pkgs.writeText "to-json" (builtins.toJSON config)} >> $out"
)
documents);
toBase64 = value:
builtins.readFile
(pkgs.runCommand "value-to-b64" {} "echo -n '${value}' | ${pkgs.coreutils}/bin/base64 -w0 > $out");
(pkgs.runCommand "value-to-b64" { } "echo -n '${value}' | ${pkgs.coreutils}/bin/base64 -w0 > $out");
exp = base: exp: foldr (_value: acc: acc * base) 1 (range 1 exp);
@ -38,8 +35,7 @@ with lib; let
i = 0;
value = 0;
}
(stringToCharacters value))
.value;
(stringToCharacters value)).value;
};
in
self
self