git.iko.soy/typeable/nixpkgs
on master help sign in
lib: Add composeManyExtensions
commitc3b35f21f78a3d23aaf40b70fe8865598ddc6729 authorJoe Hermaszewski <git@monoid.al> dateWed 11 Nov 2020, 10:36 (6 years ago) parentbacdeff
3 files changed+32−3
lib/default.nix+2−2 lib/fixed-points.nix+10−1 lib/tests/misc.nix+20
lib/default.nix+2−2
@@ -5,7 +5,7 @@
55 */
66let
77
8− inherit (import ./fixed-points.nix {}) makeExtensible;
8+ inherit (import ./fixed-points.nix { inherit lib; }) makeExtensible;
99
1010 lib = makeExtensible (self: let
1111 callLibs = file: import file { lib = self; };
@@ -69,7 +69,7 @@ let
6969 importJSON importTOML warn info showWarnings nixpkgsVersion version mod compare
7070 splitByAndCompare functionArgs setFunctionArgs isFunction toHexString toBaseDigits;
7171 inherit (self.fixedPoints) fix fix' converge extends composeExtensions
72− makeExtensible makeExtensibleWithCustomName;
72+ composeManyExtensions makeExtensible makeExtensibleWithCustomName;
7373 inherit (self.attrsets) attrByPath hasAttrByPath setAttrByPath
7474 getAttrFromPath attrVals attrValues getAttrs catAttrs filterAttrs
7575 filterAttrsRecursive foldAttrs collect nameValuePair mapAttrs
lib/fixed-points.nix+10−1
@@ -1,4 +1,4 @@
1−{ ... }:
1+{ lib, ... }:
22rec {
33 # Compute the fixed point of the given function `f`, which is usually an
44 # attribute set that expects its final, non-recursive representation as an
@@ -77,6 +77,15 @@ rec {
7777 super' = super // fApplied;
7878 in fApplied // g self super';
7979
80+ # Compose several extending functions of the type expected by 'extends' into
81+ # one where changes made in preceding functions are made available to
82+ # subsequent ones.
83+ #
84+ # composeManyExtensions : [packageSet -> packageSet -> packageSet] -> packageSet -> packageSet -> packageSet
85+ # ^final ^prev ^overrides ^final ^prev ^overrides
86+ composeManyExtensions =
87+ lib.foldr (x: y: composeExtensions x y) (self: super: {});
88+
8089 # Create an overridable, recursive attribute set. For example:
8190 #
8291 # nix-repl> obj = makeExtensible (self: { })
lib/tests/misc.nix+20
@@ -87,6 +87,26 @@ runTests {
8787 expected = true;
8888 };
8989
90+ testComposeManyExtensions0 = {
91+ expr = let obj = makeExtensible (self: { foo = true; });
92+ emptyComposition = composeManyExtensions [];
93+ composed = obj.extend emptyComposition;
94+ in composed.foo;
95+ expected = true;
96+ };
97+
98+ testComposeManyExtensions =
99+ let f = self: super: { bar = false; baz = true; };
100+ g = self: super: { bar = super.baz or false; };
101+ h = self: super: { qux = super.bar or false; };
102+ obj = makeExtensible (self: { foo = self.qux; });
103+ in {
104+ expr = let composition = composeManyExtensions [f g h];
105+ composed = obj.extend composition;
106+ in composed.foo;
107+ expected = (obj.extend (composeExtensions f (composeExtensions g h))).foo;
108+ };
109+
90110 testBitAnd = {
91111 expr = (bitAnd 3 10);
92112 expected = 2;