From 4890dc20e0eb91c1b1f78b640558dc952dd46f56 Mon Sep 17 00:00:00 2001 From: Zuckerberg Date: Fri, 20 Oct 2023 20:13:08 -0600 Subject: [PATCH] Add basic nix utilities --- flake.nix | 3 +++ lib/default.nix | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 lib/default.nix diff --git a/flake.nix b/flake.nix index 19b7a03..e906bce 100644 --- a/flake.nix +++ b/flake.nix @@ -87,6 +87,7 @@ specialArgs = { inherit allModules; + lib = self.lib; }; }; in @@ -130,5 +131,7 @@ machines; checks = builtins.mapAttrs (system: deployLib: deployLib.deployChecks self.deploy) inputs.deploy-rs.lib; + + lib = nixpkgs.lib.extend (final: prev: import ./lib { lib = nixpkgs.lib; }); }; } diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..ddfc106 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,56 @@ +{ lib, ... }: + +with lib; + +{ + # Passthrough trace for debugging + pTrace = v: traceSeq v v; + # find the total sum of a int list + sum = foldr (x: y: x + y) 0; + # splits a list of length two into two params then they're passed to a func + splitPair = f: pair: f (head pair) (last pair); + # Finds the max value in a list + maxList = foldr max 0; + # Sorts a int list. Greatest value first + sortList = sort (x: y: x > y); + # Cuts a list in half and returns the two parts in a list + cutInHalf = l: [ (take (length l / 2) l) (drop (length l / 2) l) ]; + # Splits a list into a list of lists with length cnt + chunksOf = cnt: l: + if length l > 0 then + [ (take cnt l) ] ++ chunksOf cnt (drop cnt l) + else [ ]; + # same as intersectLists but takes an array of lists to intersect instead of just two + intersectManyLists = ll: foldr intersectLists (head ll) ll; + # converts a boolean to a int (c style) + boolToInt = b: if b then 1 else 0; + # drops the last element of a list + dropLast = l: take (length l - 1) l; + # transposes a matrix + transpose = ll: + let + outerSize = length ll; + innerSize = length (elemAt ll 0); + in + genList (i: genList (j: elemAt (elemAt ll j) i) outerSize) innerSize; + # attriset recursiveUpdate but for a list of attrisets + combineAttrs = foldl recursiveUpdate { }; + # visits every single attriset element of an attriset recursively + # and accumulates the result of every visit in a flat list + recurisveVisitAttrs = f: set: + let + visitor = n: v: + if isAttrs v then [ (f n v) ] ++ recurisveVisitAttrs f v + else [ (f n v) ]; + in + concatLists (map (name: visitor name set.${name}) (attrNames set)); + # merges two lists of the same size (similar to map but both lists are inputs per iteration) + mergeLists = f: a: imap0 (i: f (elemAt a i)); + map2D = f: ll: + let + outerSize = length ll; + innerSize = length (elemAt ll 0); + getElem = x: y: elemAt (elemAt ll y) x; + in + genList (y: genList (x: f x y (getElem x y)) innerSize) outerSize; +}