From 97826c01e4cb49748907aaf7d5870104d69fe0c2 Mon Sep 17 00:00:00 2001 From: Jaka Hudoklin Date: Thu, 11 Jan 2018 12:10:18 +0100 Subject: [PATCH] fix(lib): split k8s functions to separate file --- default.nix | 7 +++++-- k8s.nix | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib.nix | 49 ----------------------------------------------- 3 files changed, 60 insertions(+), 51 deletions(-) create mode 100644 k8s.nix diff --git a/default.nix b/default.nix index 018b8f9..874581d 100644 --- a/default.nix +++ b/default.nix @@ -6,7 +6,7 @@ with pkgs.lib; with import ./lib.nix { inherit pkgs; inherit (pkgs) lib; }; let - evalKubernetesModules = configuration: evalModules { + evalKubernetesModules = configuration: evalModules rec { modules = [ (import ./kubernetes.nix {}) ./modules.nix configuration @@ -14,7 +14,10 @@ let args = { inherit pkgs; name = "default"; - k8s = { inherit loadJSON loadYAML toYAML toBase64 octalToDecimal mkSecretOption secretToEnv; }; + k8s = import ./k8s.nix { + inherit pkgs; + inherit (pkgs) lib; + }; }; }; diff --git a/k8s.nix b/k8s.nix new file mode 100644 index 0000000..2a1e3b8 --- /dev/null +++ b/k8s.nix @@ -0,0 +1,55 @@ +{lib, pkgs}: + +with lib; +with import ./lib.nix {inherit lib pkgs;}; + +{ + loadJSON = path: mkAllDefault (builtins.fromJSON (builtins.readFile path)) 1000; + + loadYAML = path: loadJSON (pkgs.runCommand "yaml-to-json" { + } "${pkgs.remarshal}/bin/remarshal -i ${path} -if yaml -of json > $out"); + + toYAML = config: builtins.readFile (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; + + mkSecretOption = {...}@options: mkOption (options // { + type = types.nullOr (types.submodule { + options = { + name = mkOption { + description = "Name of the secret where secret is stored"; + type = types.str; + } // optionalAttrs (hasAttr "default" options && options.default != null && hasAttr "name" options.default) { + default = options.default.name; + }; + + key = mkOption { + description = "Name of the key where secret is stored"; + type = types.str; + } // optionalAttrs (hasAttr "default" options && options.default != null && hasAttr "key" options.default) { + default = options.default.key; + }; + }; + }); + }); + + secretToEnv = value: { + valueFrom.secretKeyRef = { + inherit (value) name key; + }; + }; +} diff --git a/lib.nix b/lib.nix index 7e662fc..6e92cd2 100644 --- a/lib.nix +++ b/lib.nix @@ -20,53 +20,4 @@ rec { then map (v: moduleToAttrs v) value else value; - - loadJSON = path: mkAllDefault (builtins.fromJSON (builtins.readFile path)) 1000; - - loadYAML = path: loadJSON (pkgs.runCommand "yaml-to-json" { - } "${pkgs.remarshal}/bin/remarshal -i ${path} -if yaml -of json > $out"); - - toYAML = config: builtins.readFile (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; - - mkSecretOption = {...}@options: mkOption (options // { - type = types.nullOr (types.submodule { - options = { - name = mkOption { - description = "Name of the secret where secret is stored"; - type = types.str; - } // optionalAttrs (hasAttr "default" options && options.default != null && hasAttr "name" options.default) { - default = options.default.name; - }; - - key = mkOption { - description = "Name of the key where secret is stored"; - type = types.str; - } // optionalAttrs (hasAttr "default" options && options.default != null && hasAttr "key" options.default) { - default = options.default.key; - }; - }; - }); - }); - - secretToEnv = value: { - valueFrom.secretKeyRef = { - inherit (value) name key; - }; - }; }