From 0fdae315315686d180d924a7c33066fbc5b4c14d Mon Sep 17 00:00:00 2001 From: Artturin Date: Wed, 21 Jun 2023 17:30:32 +0300 Subject: [PATCH 1/3] 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 Co-authored-by: matthewcroughan --- doc/using/overrides.chapter.md | 16 ++++++++++++---- pkgs/stdenv/generic/make-derivation.nix | 2 +- pkgs/test/overriding.nix | 5 +++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/doc/using/overrides.chapter.md b/doc/using/overrides.chapter.md index 198b4504197d..b251cce4f4d4 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). ::: diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 40ad357739b7..d3593b6e8092 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -41,7 +41,7 @@ let else x; in makeDerivationExtensible - (self: let super = rattrs self; in super // f self super); + (self: let super = rattrs self; in super // (if builtins.isFunction f0 || f0?__functor then f self super else f0)); finalPackage = mkDerivationSimple overrideAttrs args; diff --git a/pkgs/test/overriding.nix b/pkgs/test/overriding.nix index edc1b27cf4f1..7838f637f313 100644 --- a/pkgs/test/overriding.nix +++ b/pkgs/test/overriding.nix @@ -21,6 +21,11 @@ let expr = repeatedOverrides.entangled.pname == "a-better-figlet-with-blackjack"; expected = true; }) + ({ + name = "overriding-using-only-attrset"; + expr = (pkgs.hello.overrideAttrs { pname = "hello-overriden"; }).pname == "hello-overriden"; + expected = true; + }) ]; addEntangled = origOverrideAttrs: f: From fb643f3260823fa715cee14c2741dbdbfc522dc0 Mon Sep 17 00:00:00 2001 From: Artturin Date: Wed, 21 Jun 2023 17:45:18 +0300 Subject: [PATCH 2/3] doc/using/overrides: it is possible to use previous arguments in .override --- doc/using/overrides.chapter.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/using/overrides.chapter.md b/doc/using/overrides.chapter.md index b251cce4f4d4..060bf051b929 100644 --- a/doc/using/overrides.chapter.md +++ b/doc/using/overrides.chapter.md @@ -16,6 +16,12 @@ Example usages: pkgs.foo.override { arg1 = val1; arg2 = val2; ... } ``` +It's also possible to access the previous arguments. + +```nix +pkgs.foo.override (previous: { arg1 = previous.arg1; ... }) +``` + ```nix From 0c9fb905cbaf4bbc64ebe098aa42a1dbbc4149fd Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 1 Jul 2023 15:25:17 +0200 Subject: [PATCH 3/3] doc/using/overrides: Relate addition to preceding text --- doc/using/overrides.chapter.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/using/overrides.chapter.md b/doc/using/overrides.chapter.md index 060bf051b929..a1ef9afb0b69 100644 --- a/doc/using/overrides.chapter.md +++ b/doc/using/overrides.chapter.md @@ -58,13 +58,15 @@ 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`. +Function arguments can be omitted entirely if there is no need to access `previousAttrs` or `finalAttrs`. + ```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. +In the above example, the `separateDebugInfo` attribute is overridden to be true, thus building debug info for `helloWithDebug`. ::: {.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).