summary refs log tree commit diff
path: root/doc
diff options
context:
space:
mode:
authorArtturin <Artturin@artturin.com>2023-06-21 17:30:32 +0300
committerArtturin <Artturin@artturin.com>2023-06-30 23:28:00 +0300
commit0fdae315315686d180d924a7c33066fbc5b4c14d (patch)
treec2d1f96710e183a3aa35e7a22bf8461196b51a72 /doc
parent3cd360a2de66c1f17099b2edffdf1842c1266abd (diff)
downloadnixpkgs-0fdae315315686d180d924a7c33066fbc5b4c14d.tar
nixpkgs-0fdae315315686d180d924a7c33066fbc5b4c14d.tar.gz
nixpkgs-0fdae315315686d180d924a7c33066fbc5b4c14d.tar.bz2
nixpkgs-0fdae315315686d180d924a7c33066fbc5b4c14d.tar.lz
nixpkgs-0fdae315315686d180d924a7c33066fbc5b4c14d.tar.xz
nixpkgs-0fdae315315686d180d924a7c33066fbc5b4c14d.tar.zst
nixpkgs-0fdae315315686d180d924a7c33066fbc5b4c14d.zip
stdenv: let overrideAttrs accept attrset OR function
Makes overrideAttrs usable in the same way that `override` can be used.
It allows the first argument of `overrideAttrs` to be either a function
or an attrset, instead of only a function:

hello.overrideAttrs (old: { postBuild = "echo hello"; })
hello.overrideAttrs { postBuild = "echo hello"; }

Previously only the first example was possible.

Co-authored-by: adisbladis <adisbladis@gmail.com>
Co-authored-by: matthewcroughan <matt@croughan.sh>
Diffstat (limited to 'doc')
-rw-r--r--doc/using/overrides.chapter.md16
1 files changed, 12 insertions, 4 deletions
diff --git a/doc/using/overrides.chapter.md b/doc/using/overrides.chapter.md
index 198b4504197..b251cce4f4d 100644
--- a/doc/using/overrides.chapter.md
+++ b/doc/using/overrides.chapter.md
@@ -36,15 +36,15 @@ In the first example, `pkgs.foo` is the result of a function call with some defa
 
 The function `overrideAttrs` allows overriding the attribute set passed to a `stdenv.mkDerivation` call, producing a new derivation based on the original one. This function is available on all derivations produced by the `stdenv.mkDerivation` function, which is most packages in the nixpkgs expression `pkgs`.
 
-Example usage:
+Example usages:
 
 ```nix
-helloWithDebug = pkgs.hello.overrideAttrs (finalAttrs: previousAttrs: {
-  separateDebugInfo = true;
+helloBar = pkgs.hello.overrideAttrs (finalAttrs: previousAttrs: {
+  pname = previousAttrs.pname + "-bar";
 });
 ```
 
-In the above example, the `separateDebugInfo` attribute is overridden to be true, thus building debug info for `helloWithDebug`, while all other attributes will be retained from the original `hello` package.
+In the above example, "-bar" is appended to the pname attribute, while all other attributes will be retained from the original `hello` package.
 
 The argument `previousAttrs` is conventionally used to refer to the attr set originally passed to `stdenv.mkDerivation`.
 
@@ -52,6 +52,14 @@ The argument `finalAttrs` refers to the final attributes passed to `mkDerivation
 
 If only a one-argument function is written, the argument has the meaning of `previousAttrs`.
 
+```nix
+helloWithDebug = pkgs.hello.overrideAttrs {
+  separateDebugInfo = true;
+};
+```
+
+In the above example, the `separateDebugInfo` attribute is overridden to be true, thus building debug info for `helloWithDebug`. Function arguments can be omitted if there is no need to access previousAttrs or finalAttrs.
+
 ::: {.note}
 Note that `separateDebugInfo` is processed only by the `stdenv.mkDerivation` function, not the generated, raw Nix derivation. Thus, using `overrideDerivation` will not work in this case, as it overrides only the attributes of the final derivation. It is for this reason that `overrideAttrs` should be preferred in (almost) all cases to `overrideDerivation`, i.e. to allow using `stdenv.mkDerivation` to process input arguments, as well as the fact that it is easier to use (you can use the same attribute names you see in your Nix code, instead of the ones generated (e.g. `buildInputs` vs `nativeBuildInputs`), and it involves less typing).
 :::