···
3
-
# Compute the fixed point of the given function `f`, which is usually an
4
-
# attribute set that expects its final, non-recursive representation as an
7
-
# f = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }
9
-
# Nix evaluates this recursion until all references to `self` have been
10
-
# resolved. At that point, the final result is returned and `f x = x` holds:
13
-
# { bar = "bar"; foo = "foo"; foobar = "foobar"; }
15
-
# Type: fix :: (a -> a) -> a
17
-
# See https://en.wikipedia.org/wiki/Fixed-point_combinator for further
4
+
Compute the fixed point of the given function `f`, which is usually an
5
+
attribute set that expects its final, non-recursive representation as an
9
+
f = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }
12
+
Nix evaluates this recursion until all references to `self` have been
13
+
resolved. At that point, the final result is returned and `f x = x` holds:
17
+
{ bar = "bar"; foo = "foo"; foobar = "foobar"; }
20
+
Type: fix :: (a -> a) -> a
22
+
See https://en.wikipedia.org/wiki/Fixed-point_combinator for further
fix = f: let x = f x; in x;
21
-
# A variant of `fix` that records the original recursive attribute set in the
22
-
# result. This is useful in combination with the `extends` function to
23
-
# implement deep overriding. See pkgs/development/haskell-modules/default.nix
24
-
# for a concrete example.
28
+
A variant of `fix` that records the original recursive attribute set in the
29
+
result, in an attribute named `__unfix__`.
31
+
This is useful in combination with the `extends` function to
32
+
implement deep overriding.
fix' = f: let x = f x // { __unfix__ = f; }; in x;
27
-
# Return the fixpoint that `f` converges to when called recursively, starting
28
-
# with the input `x`.
30
-
# nix-repl> converge (x: x / 2) 16
37
+
Return the fixpoint that `f` converges to when called iteratively, starting
41
+
nix-repl> converge (x: x / 2) 16
45
+
Type: (a -> a) -> a -> a
···
40
-
# Modify the contents of an explicitly recursive attribute set in a way that
41
-
# honors `self`-references. This is accomplished with a function
43
-
# g = self: super: { foo = super.foo + " + "; }
45
-
# that has access to the unmodified input (`super`) as well as the final
46
-
# non-recursive representation of the attribute set (`self`). `extends`
47
-
# differs from the native `//` operator insofar as that it's applied *before*
48
-
# references to `self` are resolved:
50
-
# nix-repl> fix (extends g f)
51
-
# { bar = "bar"; foo = "foo + "; foobar = "foo + bar"; }
53
-
# The name of the function is inspired by object-oriented inheritance, i.e.
54
-
# think of it as an infix operator `g extends f` that mimics the syntax from
55
-
# Java. It may seem counter-intuitive to have the "base class" as the second
56
-
# argument, but it's nice this way if several uses of `extends` are cascaded.
58
-
# To get a better understanding how `extends` turns a function with a fix
59
-
# point (the package set we start with) into a new function with a different fix
60
-
# point (the desired packages set) lets just see, how `extends g f`
61
-
# unfolds with `g` and `f` defined above:
63
-
# extends g f = self: let super = f self; in super // g self super;
64
-
# = self: let super = { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }; in super // g self super
65
-
# = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; } // g self { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }
66
-
# = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; } // { foo = "foo" + " + "; }
67
-
# = self: { foo = "foo + "; bar = "bar"; foobar = self.foo + self.bar; }
56
+
Modify the contents of an explicitly recursive attribute set in a way that
57
+
honors `self`-references. This is accomplished with a function
60
+
g = self: super: { foo = super.foo + " + "; }
63
+
that has access to the unmodified input (`super`) as well as the final
64
+
non-recursive representation of the attribute set (`self`). `extends`
65
+
differs from the native `//` operator insofar as that it's applied *before*
66
+
references to `self` are resolved:
69
+
nix-repl> fix (extends g f)
70
+
{ bar = "bar"; foo = "foo + "; foobar = "foo + bar"; }
73
+
The name of the function is inspired by object-oriented inheritance, i.e.
74
+
think of it as an infix operator `g extends f` that mimics the syntax from
75
+
Java. It may seem counter-intuitive to have the "base class" as the second
76
+
argument, but it's nice this way if several uses of `extends` are cascaded.
78
+
To get a better understanding how `extends` turns a function with a fix
79
+
point (the package set we start with) into a new function with a different fix
80
+
point (the desired packages set) lets just see, how `extends g f`
81
+
unfolds with `g` and `f` defined above:
84
+
extends g f = self: let super = f self; in super // g self super;
85
+
= self: let super = { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }; in super // g self super
86
+
= self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; } // g self { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }
87
+
= self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; } // { foo = "foo" + " + "; }
88
+
= self: { foo = "foo + "; bar = "bar"; foobar = self.foo + self.bar; }
extends = f: rattrs: self: let super = rattrs self; in super // f self super;
71
-
# Compose two extending functions of the type expected by 'extends'
72
-
# into one where changes made in the first are available in the
73
-
# 'super' of the second
94
+
Compose two extending functions of the type expected by 'extends'
95
+
into one where changes made in the first are available in the
96
+
'super' of the second
let fApplied = f final prev;
prev' = prev // fApplied;
in fApplied // g final prev';
80
-
# Compose several extending functions of the type expected by 'extends' into
81
-
# one where changes made in preceding functions are made available to
84
-
# composeManyExtensions : [packageSet -> packageSet -> packageSet] -> packageSet -> packageSet -> packageSet
85
-
# ^final ^prev ^overrides ^final ^prev ^overrides
105
+
Compose several extending functions of the type expected by 'extends' into
106
+
one where changes made in preceding functions are made available to
110
+
composeManyExtensions : [packageSet -> packageSet -> packageSet] -> packageSet -> packageSet -> packageSet
111
+
^final ^prev ^overrides ^final ^prev ^overrides
lib.foldr (x: y: composeExtensions x y) (final: prev: {});
89
-
# Create an overridable, recursive attribute set. For example:
91
-
# nix-repl> obj = makeExtensible (self: { })
94
-
# { __unfix__ = «lambda»; extend = «lambda»; }
96
-
# nix-repl> obj = obj.extend (self: super: { foo = "foo"; })
99
-
# { __unfix__ = «lambda»; extend = «lambda»; foo = "foo"; }
101
-
# nix-repl> obj = obj.extend (self: super: { foo = super.foo + " + "; bar = "bar"; foobar = self.foo + self.bar; })
104
-
# { __unfix__ = «lambda»; bar = "bar"; extend = «lambda»; foo = "foo + "; foobar = "foo + bar"; }
118
+
Create an overridable, recursive attribute set. For example:
121
+
nix-repl> obj = makeExtensible (self: { })
124
+
{ __unfix__ = «lambda»; extend = «lambda»; }
126
+
nix-repl> obj = obj.extend (self: super: { foo = "foo"; })
129
+
{ __unfix__ = «lambda»; extend = «lambda»; foo = "foo"; }
131
+
nix-repl> obj = obj.extend (self: super: { foo = super.foo + " + "; bar = "bar"; foobar = self.foo + self.bar; })
134
+
{ __unfix__ = «lambda»; bar = "bar"; extend = «lambda»; foo = "foo + "; foobar = "foo + bar"; }
makeExtensible = makeExtensibleWithCustomName "extend";
107
-
# Same as `makeExtensible` but the name of the extending attribute is
140
+
Same as `makeExtensible` but the name of the extending attribute is
makeExtensibleWithCustomName = extenderName: rattrs:
fix' (self: (rattrs self) // {
${extenderName} = f: makeExtensibleWithCustomName extenderName (extends f rattrs);