From 844d347ce7cf0b7646e9ecba3fbdc0b90e608501 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 20 Oct 2016 20:21:59 +0200 Subject: lib: import bulk of krebs/4lib --- lib/default.nix | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'lib/default.nix') diff --git a/lib/default.nix b/lib/default.nix index 1f501085..2b12fa4b 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,10 +1,44 @@ let - lib = import // builtins // { + nixpkgs-lib = import ; + 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 -- cgit v1.2.3