feat: interface and lib refactoring

This commit is contained in:
Jaka Hudoklin 2019-03-07 18:02:26 +01:00
parent e260ad9bb4
commit 5d8b66f8a0
No known key found for this signature in database
GPG key ID: 6A08896BFD32BD95
10 changed files with 111 additions and 88 deletions

6
lib/default.nix Normal file
View file

@ -0,0 +1,6 @@
{ lib, pkgs }:
(import ./extra.nix { inherit pkgs lib; }) // {
k8s = import ./k8s.nix { inherit lib; };
docker = import ./docker.nix { inherit lib pkgs; };
}

15
lib/docker.nix Normal file
View file

@ -0,0 +1,15 @@
{ lib, pkgs }:
with lib;
{
copyDockerImages = { images, dest, args ? "" }:
pkgs.writeScriptBin "copy-docker-images" (concatMapStrings (image: ''
#!${pkgs.bash}/bin/bash
set -e
echo "copying ${image.imageName}:${image.imageTag}"
${pkgs.skopeo}/bin/skopeo copy ${args} $@ docker-archive:${image} ${dest}/${image.imageName}:${image.imageTag}
'') images);
}

45
lib/extra.nix Normal file
View file

@ -0,0 +1,45 @@
{ lib, pkgs }:
with lib;
rec {
moduleToAttrs = value:
if isAttrs value
then mapAttrs (n: v: moduleToAttrs v) (filterAttrs (n: v: !(hasPrefix "_" n) && v != null) value)
else if isList value
then map (v: moduleToAttrs v) value
else value;
mkOptionDefault = mkOverride 1001;
mkAllDefault = value: priority:
if isAttrs value
then mapAttrs (n: v: mkAllDefault v priority) value
else if isList value
then map (v: mkAllDefault v priority) value
else mkOverride priority value;
loadYAML = path: importJSON (pkgs.runCommand "yaml-to-json" {
} "${pkgs.remarshal}/bin/remarshal -i ${path} -if yaml -of json > $out");
toYAML = config: pkgs.runCommand "to-yaml" {
buildInputs = [pkgs.remarshal];
} ''
remarshal -i ${pkgs.writeText "to-json" (builtins.toJSON config)} -if json -of yaml > $out
'';
toBase64 = value:
builtins.readFile
(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);
octalToDecimal = value: (foldr (char: acc: {
i = acc.i + 1;
value = acc.value + (toInt char) * (exp 8 acc.i);
}) {i = 0; value = 0;} (stringToCharacters value)).value;
}

54
lib/k8s.nix Normal file
View file

@ -0,0 +1,54 @@
{ lib }:
with lib;
rec {
mkSecretOption = {description ? "", default ? null}: mkOption {
inherit description;
type = types.nullOr (types.submodule {
options = {
name = mkOption {
description = "Name of the secret where secret is stored";
type = types.str;
};
key = mkOption {
description = "Name of the key where secret is stored";
type = types.str;
};
};
config = mkDefault (if default == null then {} else default);
});
default = {};
};
secretToEnv = value: {
valueFrom.secretKeyRef = {
inherit (value) name key;
};
};
# Creates kubernetes list from a list of kubernetes objects
mkList = { items, labels ? {} }: {
kind = "List";
apiVersion = "v1";
inherit items labels;
};
# Creates hashed kubernetes list from a list of kubernetes objects
mkHashedList = { items, labels ? {} }: let
hash = builtins.hashString "sha1" (builtins.toJSON items);
labeledItems = map (item: recursiveUpdate item {
metadata.labels."kubenix/hash" = hash;
}) items;
in mkList {
items = labeledItems;
labels = {
"kubenix/hash" = hash;
} // labels;
};
}