2016-08-02 20:24:45 +02:00
|
|
|
let
|
2016-10-20 20:21:59 +02:00
|
|
|
nixpkgs-lib = import <nixpkgs/lib>;
|
|
|
|
lib = with lib; nixpkgs-lib // builtins // {
|
2017-07-06 21:47:47 +02:00
|
|
|
|
|
|
|
evalSource = import ./eval-source.nix;
|
|
|
|
|
2016-10-20 20:21:59 +02:00
|
|
|
git = import ./git.nix { inherit lib; };
|
2018-12-07 13:16:41 +01:00
|
|
|
krebs = import ./krebs lib;
|
2018-11-30 13:42:44 +01:00
|
|
|
krops = import ../submodules/krops/lib;
|
2016-08-02 20:24:45 +02:00
|
|
|
shell = import ./shell.nix { inherit lib; };
|
2016-10-20 20:21:59 +02:00
|
|
|
types = nixpkgs-lib.types // import ./types.nix { inherit lib; };
|
2016-10-13 21:18:40 +02:00
|
|
|
|
|
|
|
eq = x: y: x == y;
|
|
|
|
ne = x: y: x != y;
|
|
|
|
mod = x: y: x - y * (x / y);
|
2016-10-20 20:21:59 +02:00
|
|
|
|
2018-12-03 07:45:20 +01:00
|
|
|
genid = lib.genid_uint32; # TODO remove
|
|
|
|
genid_uint31 = x: ((lib.genid_uint32 x) + 16777216) / 2;
|
|
|
|
genid_uint32 = import ./genid.nix { inherit lib; };
|
2016-10-20 20:21:59 +02:00
|
|
|
|
|
|
|
lpad = n: c: s:
|
|
|
|
if lib.stringLength s < n
|
|
|
|
then lib.lpad n c (c + s)
|
|
|
|
else s;
|
|
|
|
|
|
|
|
genAttrs' = names: f: listToAttrs (map f names);
|
|
|
|
|
|
|
|
getAttrs = names: set:
|
|
|
|
listToAttrs (map (name: nameValuePair name set.${name})
|
|
|
|
(filter (flip hasAttr set) names));
|
|
|
|
|
2017-06-18 15:36:18 +02:00
|
|
|
test = re: x: isString x && testString re x;
|
|
|
|
|
|
|
|
testString = re: x: match re x != null;
|
|
|
|
|
2016-10-20 20:21:59 +02:00
|
|
|
toC = x: let
|
|
|
|
type = typeOf x;
|
|
|
|
reject = throw "cannot convert ${type}";
|
|
|
|
in {
|
|
|
|
list = "{ ${concatStringsSep ", " (map toC x)} }";
|
|
|
|
null = "NULL";
|
|
|
|
set = if isDerivation x then toJSON x else reject;
|
|
|
|
string = toJSON x; # close enough
|
|
|
|
}.${type} or reject;
|
|
|
|
|
2017-01-21 23:26:48 +01:00
|
|
|
indent = replaceChars ["\n"] ["\n "];
|
|
|
|
|
2018-11-30 09:40:53 +01:00
|
|
|
mapNixDir = f: x: {
|
|
|
|
list = foldl' mergeAttrs {} (map (mapNixDir1 f) x);
|
|
|
|
path = mapNixDir1 f x;
|
|
|
|
}.${typeOf x};
|
|
|
|
|
|
|
|
mapNixDir1 = f: dirPath:
|
|
|
|
listToAttrs
|
|
|
|
(map
|
|
|
|
(relPath: let
|
|
|
|
name = removeSuffix ".nix" relPath;
|
|
|
|
path = dirPath + "/${relPath}";
|
|
|
|
in
|
|
|
|
nameValuePair name (f path))
|
|
|
|
(filter
|
|
|
|
(name: name != "default.nix" && !hasPrefix "." name)
|
|
|
|
(attrNames (readDir dirPath))));
|
|
|
|
|
2017-04-13 11:12:55 +02:00
|
|
|
# https://tools.ietf.org/html/rfc5952
|
|
|
|
normalize-ip6-addr =
|
|
|
|
let
|
|
|
|
max-run-0 =
|
|
|
|
let
|
|
|
|
both = v: { off = v; pos = v; };
|
|
|
|
gt = a: b: a.pos - a.off > b.pos - b.off;
|
|
|
|
|
|
|
|
chkmax = ctx: {
|
|
|
|
cur = both (ctx.cur.pos + 1);
|
|
|
|
max = if gt ctx.cur ctx.max then ctx.cur else ctx.max;
|
|
|
|
};
|
|
|
|
|
|
|
|
incpos = ctx: recursiveUpdate ctx {
|
|
|
|
cur.pos = ctx.cur.pos + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
f = ctx: blk: (if blk == "0" then incpos else chkmax) ctx;
|
|
|
|
z = { cur = both 0; max = both 0; };
|
|
|
|
in
|
|
|
|
blks: (chkmax (foldl' f z blks)).max;
|
|
|
|
|
|
|
|
group-zeros = a:
|
|
|
|
let
|
|
|
|
blks = splitString ":" a;
|
|
|
|
max = max-run-0 blks;
|
|
|
|
lhs = take max.off blks;
|
|
|
|
rhs = drop max.pos blks;
|
|
|
|
in
|
|
|
|
if max.pos == 0
|
|
|
|
then a
|
|
|
|
else "${concatStringsSep ":" lhs}::${concatStringsSep ":" rhs}";
|
|
|
|
|
|
|
|
drop-leading-zeros =
|
|
|
|
let
|
|
|
|
f = block:
|
|
|
|
let
|
|
|
|
res = match "0*(.+)" block;
|
|
|
|
in
|
|
|
|
if res == null
|
|
|
|
then block # empty block
|
|
|
|
else elemAt res 0;
|
|
|
|
in
|
|
|
|
a: concatStringsSep ":" (map f (splitString ":" a));
|
|
|
|
in
|
|
|
|
a: toLower (group-zeros (drop-leading-zeros a));
|
2016-08-02 20:24:45 +02:00
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
lib
|