lib: import bulk of krebs/4lib

This commit is contained in:
tv 2016-10-20 20:21:59 +02:00
parent 91d6bd66f4
commit 844d347ce7
6 changed files with 55 additions and 80 deletions

View File

@ -1,59 +0,0 @@
_:
let
lib = import <stockholm/lib>;
in
with lib;
let out = lib // rec {
guard = spec@{ type, value, ... }:
assert isOptionType type;
if type.check value
then value
else throw (toString (filter isString [
"argument"
(if spec ? name then "${spec.name}" else null)
"is not a ${type.name}"
]));
types = import ./types.nix {
lib = lib // { inherit genid optionalTrace; };
};
genid = import ./genid.nix { lib = lib // out; };
genid_signed = x: ((genid x) + 16777216) / 2;
git = import ./git.nix { lib = lib // out; };
tree = import ./tree.nix { inherit lib; };
lpad = n: c: s:
if stringLength s < n
then lpad n c (c + s)
else s;
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;
subdirsOf = path:
mapAttrs (name: _: path + "/${name}")
(filterAttrs (_: eq "directory") (readDir path));
genAttrs' = names: f: listToAttrs (map f names);
getAttrs = names: set:
listToAttrs (map (name: nameValuePair name set.${name})
(filter (flip hasAttr set) names));
setAttr = name: value: set: set // { ${name} = value; };
optionalTrace = c: msg: x: if c then trace msg x else x;
}; in out

View File

@ -1,13 +0,0 @@
{ lib, ... }:
with lib;
rec {
# tree k v = set k (either v (tree k v))
# get : [k] -> tree k v -> v
get = path: tree:
if length path > 0
then get (tail path) tree.${head path} # TODO check if elem exists
else tree;
}

View File

@ -1,10 +1,44 @@
let
lib = import <nixpkgs/lib> // builtins // {
nixpkgs-lib = import <nixpkgs/lib>;
lib = with lib; nixpkgs-lib // builtins // {
git = import ./git.nix { inherit lib; };
shell = import ./shell.nix { inherit lib; };
types = nixpkgs-lib.types // import ./types.nix { inherit lib; };
eq = x: y: x == y;
ne = x: y: x != y;
mod = x: y: x - y * (x / y);
genid = import ./genid.nix { inherit lib; };
genid_signed = x: ((lib.genid x) + 16777216) / 2;
lpad = n: c: s:
if lib.stringLength s < n
then lib.lpad n c (c + s)
else s;
subdirsOf = path:
lib.mapAttrs (name: _: path + "/${name}")
(filterAttrs (_: eq "directory") (readDir path));
genAttrs' = names: f: listToAttrs (map f names);
getAttrs = names: set:
listToAttrs (map (name: nameValuePair name set.${name})
(filter (flip hasAttr set) names));
setAttr = name: value: set: set // { ${name} = value; };
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;
};
in

View File

@ -1,10 +1,16 @@
{ lib, ... }:
with builtins;
with lib;
with types;
let
inherit (lib)
all any concatMapStringsSep concatStringsSep const filter flip genid
hasSuffix head isInt isString length match mergeOneOption mkOption
mkOptionType optional optionalAttrs optionals range splitString
stringLength tail typeOf;
inherit (lib.types)
attrsOf bool either enum int listOf nullOr path str string submodule;
in
types // rec {
rec {
host = submodule ({ config, ... }: {
options = {
@ -20,6 +26,11 @@ types // rec {
default = {};
};
binary-cache.pubkey = mkOption {
type = nullOr binary-cache-pubkey;
default = null;
};
owner = mkOption {
type = user;
};
@ -27,7 +38,7 @@ types // rec {
extraZones = mkOption {
default = {};
# TODO: string is either MX, NS, A or AAAA
type = with types; attrsOf string;
type = attrsOf string;
};
secure = mkOption {
@ -331,6 +342,8 @@ types // rec {
merge = mergeOneOption;
};
binary-cache-pubkey = str;
pgp-pubkey = str;
ssh-pubkey = str;
@ -356,7 +369,7 @@ types // rec {
tinc-pubkey = str;
krebs.file-location = types.submodule {
krebs.file-location = submodule {
options = {
# TODO user
host = mkOption {
@ -364,7 +377,7 @@ types // rec {
};
# TODO merge with ssl.privkey.path
path = mkOption {
type = types.either types.path types.str;
type = either path str;
apply = x: {
path = toString x;
string = x;