git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #18660 from aneeshusa/add-override-attrs

mkDerivation: add overrideAttrs function

commit62edf873aad11ffd4acc95026a416eaa37fc42ec authorDomen Kožar <domen@dev.si> dateSun 30 Oct 2016, 11:32 (10 years ago) parent9ca052139b64b5
3 files changed+74−10
doc/functions.xml+61 lib/customisation.nix+12−10 pkgs/stdenv/generic/default.nix+1
doc/functions.xml+61
@@ -99,9 +99,70 @@ in ...</programlisting>
9999
100100</section>
101101
102+<section xml:id="sec-pkg-overrideAttrs">
103+ <title>&lt;pkg&gt;.overrideAttrs</title>
104+
105+ <para>
106+ The function <varname>overrideAttrs</varname> allows overriding the
107+ attribute set passed to a <varname>stdenv.mkDerivation</varname> call,
108+ producing a new derivation based on the original one.
109+ This function is available on all derivations produced by the
110+ <varname>stdenv.mkDerivation</varname> function, which is most packages
111+ in the nixpkgs expression <varname>pkgs</varname>.
112+ </para>
113+
114+ <para>
115+ Example usage:
116+
117+ <programlisting>helloWithDebug = pkgs.hello.overrideAttrs (oldAttrs: rec {
118+ separateDebugInfo = true;
119+});</programlisting>
120+ </para>
121+
122+ <para>
123+ In the above example, the <varname>separateDebugInfo</varname> attribute is
124+ overriden to be true, thus building debug info for
125+ <varname>helloWithDebug</varname>, while all other attributes will be
126+ retained from the original <varname>hello</varname> package.
127+ </para>
128+
129+ <para>
130+ The argument <varname>oldAttrs</varname> is conventionally used to refer to
131+ the attr set originally passed to <varname>stdenv.mkDerivation</varname>.
132+ </para>
133+
134+ <note>
135+ <para>
136+ Note that <varname>separateDebugInfo</varname> is processed only by the
137+ <varname>stdenv.mkDerivation</varname> function, not the generated, raw
138+ Nix derivation. Thus, using <varname>overrideDerivation</varname> will
139+ not work in this case, as it overrides only the attributes of the final
140+ derivation. It is for this reason that <varname>overrideAttrs</varname>
141+ should be preferred in (almost) all cases to
142+ <varname>overrideDerivation</varname>, i.e. to allow using
143+ <varname>sdenv.mkDerivation</varname> to process input arguments, as well
144+ as the fact that it is easier to use (you can use the same attribute
145+ names you see in your Nix code, instead of the ones generated (e.g.
146+ <varname>buildInputs</varname> vs <varname>nativeBuildInputs</varname>,
147+ and involves less typing.
148+ </para>
149+ </note>
150+
151+</section>
152+
153+
102154<section xml:id="sec-pkg-overrideDerivation">
103155 <title>&lt;pkg&gt;.overrideDerivation</title>
104156
157+ <warning>
158+ <para>You should prefer <varname>overrideAttrs</varname> in almost all
159+ cases, see its documentation for the reasons why.
160+ <varname>overrideDerivation</varname> is not deprecated and will continue
161+ to work, but is less nice to use and does not have as many abilities as
162+ <varname>overrideAttrs</varname>.
163+ </para>
164+ </warning>
165+
105166 <warning>
106167 <para>Do not use this function in Nixpkgs as it evaluates a Derivation
107168 before modifying it, which breaks package abstraction and removes
lib/customisation.nix+12−10
@@ -56,16 +56,18 @@ rec {
5656 ff = f origArgs;
5757 overrideWith = newArgs: origArgs // (if builtins.isFunction newArgs then newArgs origArgs else newArgs);
5858 in
59− if builtins.isAttrs ff then (ff //
60− { override = newArgs: makeOverridable f (overrideWith newArgs);
61− overrideDerivation = fdrv:
62− makeOverridable (args: overrideDerivation (f args) fdrv) origArgs;
63− })
64− else if builtins.isFunction ff then
65− { override = newArgs: makeOverridable f (overrideWith newArgs);
66− __functor = self: ff;
67− overrideDerivation = throw "overrideDerivation not yet supported for functors";
68− }
59+ if builtins.isAttrs ff then (ff // {
60+ override = newArgs: makeOverridable f (overrideWith newArgs);
61+ overrideDerivation = fdrv:
62+ makeOverridable (args: overrideDerivation (f args) fdrv) origArgs;
63+ ${if ff ? overrideAttrs then "overrideAttrs" else null} = fdrv:
64+ makeOverridable (args: (f args).overrideAttrs fdrv) origArgs;
65+ })
66+ else if builtins.isFunction ff then {
67+ override = newArgs: makeOverridable f (overrideWith newArgs);
68+ __functor = self: ff;
69+ overrideDerivation = throw "overrideDerivation not yet supported for functors";
70+ }
6971 else ff;
7072
7173
pkgs/stdenv/generic/default.nix+1
@@ -234,6 +234,7 @@ let
234234 outputs = outputs';
235235 } else { })))) (
236236 {
237+ overrideAttrs = f: mkDerivation (attrs // (f attrs));
237238 # The meta attribute is passed in the resulting attribute set,
238239 # but it's not part of the actual derivation, i.e., it's not
239240 # passed to the builder and is not a dependency. But since we