stockholm/lib/default.nix

226 lines
6.3 KiB
Nix
Raw Normal View History

2016-08-02 18:24:45 +00:00
let
2016-10-20 18:21:59 +00:00
nixpkgs-lib = import <nixpkgs/lib>;
lib = with lib; nixpkgs-lib // builtins // {
2022-03-18 12:48:56 +00:00
evalModulesConfig = modules: let
eval = evalModules {
inherit modules;
};
in filterAttrsRecursive (name: _: !hasPrefix "_" name) eval.config;
evalSource = import ./eval-source.nix;
2022-03-18 12:49:07 +00:00
evalSubmodule = submodule: modules: let
prefix = ["evalSubmodule"];
in evalModulesConfig [
{
options = removeAttrs (submodule.getSubOptions prefix) ["_module"];
imports = modules;
}
];
2016-10-20 18:21:59 +00:00
git = import ./git.nix { inherit lib; };
2021-03-15 00:00:53 +00:00
haskell = import ./haskell.nix { inherit lib; };
2018-12-07 12:16:41 +00:00
krebs = import ./krebs lib;
2018-11-30 12:42:44 +00:00
krops = import ../submodules/krops/lib;
2016-08-02 18:24:45 +00:00
shell = import ./shell.nix { inherit lib; };
systemd = {
encodeName = replaceChars ["/"] ["\\x2f"];
};
2016-10-20 18:21:59 +00:00
types = nixpkgs-lib.types // import ./types.nix { inherit lib; };
uri = import ./uri.nix { inherit lib; };
2015-03-20 09:36:12 +00:00
xml = import ./xml.nix { inherit lib; };
2016-10-13 19:18:40 +00:00
2021-03-03 02:06:30 +00:00
# compose a list of functions to be applied from left to right, i.e.
# compose :: [ (xm -> xn) ... (x1 -> x2) (x0 -> x1) ] -> x0 -> xn
compose = foldl' (f: g: x: f (g x)) id;
2016-10-13 19:18:40 +00:00
eq = x: y: x == y;
ne = x: y: x != y;
mod = x: y: x - y * (x / y);
2016-10-20 18:21:59 +00:00
2022-12-20 18:07:51 +00:00
on = b: u: x: y: b (u x) (u y);
2018-12-03 06:45:20 +00: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 18:21:59 +00:00
2023-02-04 23:47:55 +00:00
hexchars = stringToCharacters "0123456789abcdef";
2016-10-20 18:21:59 +00: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));
2021-12-22 23:46:12 +00:00
maybeHead = x: if isList x && length x > 0 then head x else null;
2019-09-04 18:17:02 +00:00
packageName = pkg:
pkg.pname or (parseDrvName pkg.name).name;
2017-06-18 13:36:18 +00:00
test = re: x: isString x && testString re x;
testString = re: x: match re x != null;
2016-10-20 18:21:59 +00:00
toC = x: let
type = typeOf x;
reject = throw "cannot convert ${type}";
in {
2022-01-03 23:55:22 +00:00
int = toJSON x; # close enough
2016-10-20 18:21:59 +00:00
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 22:26:48 +00:00
indent = replaceChars ["\n"] ["\n "];
2020-04-18 16:48:54 +00:00
stripAttr = converge (filterAttrsRecursive (n: v: v != {} && v != null));
2018-11-30 08:40:53 +00:00
mapNixDir = f: x: {
list = foldl' mergeAttrs {} (map (mapNixDir1 f) x);
path = mapNixDir1 f x;
}.${typeOf x};
mapNixDir1 = f: dirPath:
let
toPackageName = name:
if test "^[0-9].*" name then "_${name}" else name;
in
2018-11-30 08:40:53 +00:00
listToAttrs
(map
(relPath: let
name = removeSuffix ".nix" relPath;
path = dirPath + "/${relPath}";
in
nameValuePair (toPackageName name) (f path))
(attrNames
2022-12-08 23:30:08 +00:00
(filterAttrs isNixDirEntry (readDir dirPath))));
isNixDirEntry = name: type:
(type == "regular" && hasSuffix ".nix" name && name != "default.nix") ||
(type == "directory" && !hasPrefix "." name);
2018-11-30 08:40:53 +00:00
2017-04-13 09:12:55 +00: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 let
sep =
if 8 - (length lhs + length rhs) == 1
then ":0:"
else "::";
in
"${concatStringsSep ":" lhs}${sep}${concatStringsSep ":" rhs}";
2017-04-13 09:12:55 +00:00
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
(if test ".*::.*" a
then a
else group-zeros (drop-leading-zeros a));
hashToLength = n: s: substring 0 n (hashString "sha256" s);
dropLast = n: xs: reverseList (drop n (reverseList xs));
takeLast = n: xs: reverseList (take n (reverseList xs));
# Split string into list of chunks where each chunk is at most n chars long.
# The leftmost chunk might shorter.
# Example: stringToGroupsOf "123456" -> ["12" "3456"]
stringToGroupsOf = n: s: let
acc =
foldl'
(acc: c: if stringLength acc.chunk < n then {
chunk = acc.chunk + c;
chunks = acc.chunks;
} else {
chunk = c;
chunks = acc.chunks ++ [acc.chunk];
})
{
chunk = "";
chunks = [];
}
(stringToCharacters s);
in
filter (x: x != []) ([acc.chunk] ++ acc.chunks);
2022-12-20 18:08:42 +00:00
# Filter adjacent duplicate elements.
uniq = uniqBy eq;
# Filter adjacent duplicate elements determined via the given function.
uniqBy = cmp: let
f = a: s:
if length s == 0 then
[]
else let
b = head s;
in
if cmp a b then
f b (tail s)
else
[b] ++ f b (tail s);
in
s:
if length s == 0 then
[]
else let
b = head s;
in
[b] ++ f b (tail s);
2019-02-16 14:39:17 +00:00
warnOldVersion = oldName: newName:
if compareVersions oldName newName != -1 then
trace "Upstream `${oldName}' gets overridden by `${newName}'." newName
else
newName;
2016-08-02 18:24:45 +00:00
};
in
lib
2022-12-09 00:33:39 +00:00
// { inherit lib; }