labs/lib/src/default.nix

83 lines
2 KiB
Nix
Raw Permalink Normal View History

2024-06-01 11:00:53 +00:00
let
files = [
./attrs
./bools
./dag
2024-06-01 11:00:53 +00:00
./errors
./fp
./generators
./importers
2024-06-05 02:04:40 +00:00
./licenses
2024-06-01 11:00:53 +00:00
./lists
./math
./modules
./numbers
./options
./packages
./paths
./points
./strings
./types
./versions
];
libs = builtins.map (f: import f) files;
## Calculate the fixed point of a function. This will evaluate the function `f`
## until its result settles (or Nix's recursion limit is reached). This allows
## us to define recursive functions without worrying about the order of their
## definitions.
##
## @type (a -> a) -> a
2024-06-22 17:58:44 +00:00
fix =
f:
let
x = f x;
in
2024-06-01 11:00:53 +00:00
x;
## Merge two attribute sets recursively until a given predicate returns true.
## Any values that are _not_ attribute sets will be overridden with the value
## from `y` first if it exists and then `x` otherwise.
##
## @type Attrs a b c => (String -> Any -> Any -> Bool) -> a -> b -> c
2024-06-22 17:58:44 +00:00
mergeAttrsRecursiveUntil =
predicate: x: y:
let
process =
path:
builtins.zipAttrsWith (
name: values:
let
currentPath = path ++ [ name ];
isSingleValue = builtins.length values == 1;
isComplete = predicate currentPath (builtins.elemAt values 1) (builtins.elemAt values 0);
in
if isSingleValue || isComplete then builtins.elemAt values 0 else process currentPath values
);
in
process [ ] [
x
y
];
2024-06-01 11:00:53 +00:00
## Merge two attribute sets recursively. Any values that are _not_ attribute sets
## will be overridden with the value from `y` first if it exists and then `x`
## otherwise.
##
## @type Attrs a b c => a -> b -> c
2024-06-22 17:58:44 +00:00
mergeAttrsRecursive = mergeAttrsRecursiveUntil (
path: x: y:
!(builtins.isAttrs x && builtins.isAttrs y)
);
2024-06-01 11:00:53 +00:00
lib = fix (
2024-06-22 17:58:44 +00:00
self:
let
merge = acc: create: mergeAttrsRecursive acc (create self);
2024-06-01 11:00:53 +00:00
in
2024-06-22 17:58:44 +00:00
builtins.foldl' merge { } libs
2024-06-01 11:00:53 +00:00
);
in
2024-06-22 17:58:44 +00:00
lib.points.withExtend (lib.fp.const lib)