kubenix/lib.nix

72 lines
1.9 KiB
Nix
Raw Normal View History

2017-11-11 11:52:17 +01:00
{lib, pkgs}:
with lib;
rec {
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;
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;
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");
2017-12-01 12:40:28 +01:00
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
'');
2017-11-11 11:52:17 +01:00
toBase64 = value:
builtins.readFile
2017-12-04 12:08:13 +01:00
(pkgs.runCommand "value-to-b64" {} "echo -n '${value}' | ${pkgs.coreutils}/bin/base64 -w0 > $out");
2017-11-11 13:42:15 +01:00
2017-12-01 12:37:10 +01:00
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;
2017-11-11 13:42:15 +01:00
mkValueOrSecretOption = {...}@options: mkOption ({
type = types.nullOr (types.either types.str (types.submodule {
2017-11-11 13:42:15 +01:00
options.secret = mkOption {
description = "Name of the secret where password is stored";
type = types.str;
};
options.key = mkOption {
description = "Name of the key where password is stored";
type = types.str;
default = "password";
};
}));
2017-11-11 13:42:15 +01:00
apply = value:
if isAttrs value
then {
valueFrom.secretKeyRef = {
name = value.secret;
key = value.key;
};
}
else {inherit value;};
} // options);
2017-11-11 11:52:17 +01:00
}