Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdenv.mkDerivation: Support argument being a fixed-point function (self: { … }) instead of rec { … } #94198

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkgs/applications/misc/hello/default.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{ stdenv, fetchurl }:

stdenv.mkDerivation rec {
stdenv.mkDerivation (self: {
pname = "hello";
version = "2.10";

src = fetchurl {
url = "mirror://gnu/hello/${pname}-${version}.tar.gz";
url = "mirror://gnu/hello/${self.pname}-${self.version}.tar.gz";
sha256 = "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i";
};

Expand All @@ -18,9 +18,9 @@ stdenv.mkDerivation rec {
It is fully customizable.
'';
homepage = "https://www.gnu.org/software/hello/manual/";
changelog = "https://git.savannah.gnu.org/cgit/hello.git/plain/NEWS?h=v${version}";
changelog = "https://git.savannah.gnu.org/cgit/hello.git/plain/NEWS?h=v${self.version}";
license = licenses.gpl3Plus;
maintainers = [ maintainers.eelco ];
platforms = platforms.all;
};
}
})
32 changes: 16 additions & 16 deletions pkgs/stdenv/adapters.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ rec {
{ mkDerivation = args:
if stdenv'.hostPlatform.isDarwin
then throw "Cannot build fully static binaries on Darwin/macOS"
else stdenv'.mkDerivation (args // {
NIX_CFLAGS_LINK = toString (args.NIX_CFLAGS_LINK or "") + " -static";
configureFlags = (args.configureFlags or []) ++ [
else (stdenv'.mkDerivation args).overrideAttrs (super: {
NIX_CFLAGS_LINK = toString (super.NIX_CFLAGS_LINK or "") + " -static";
configureFlags = (super.configureFlags or []) ++ [
"--disable-shared" # brrr...
];
});
Expand All @@ -54,14 +54,14 @@ rec {
# Return a modified stdenv that builds static libraries instead of
# shared libraries.
makeStaticLibraries = stdenv: stdenv //
{ mkDerivation = args: stdenv.mkDerivation (args // {
{ mkDerivation = args: (stdenv.mkDerivation args).overrideAttrs (super: {
dontDisableStatic = true;
configureFlags = (args.configureFlags or []) ++ [
configureFlags = (super.configureFlags or []) ++ [
"--enable-static"
"--disable-shared"
];
cmakeFlags = (args.cmakeFlags or []) ++ [ "-DBUILD_SHARED_LIBS:BOOL=OFF" ];
mesonFlags = (args.mesonFlags or []) ++ [ "-Ddefault_library=static" ];
cmakeFlags = (super.cmakeFlags or []) ++ [ "-DBUILD_SHARED_LIBS:BOOL=OFF" ];
mesonFlags = (super.mesonFlags or []) ++ [ "-Ddefault_library=static" ];
});
};

Expand All @@ -70,8 +70,8 @@ rec {
consuming derivations
*/
propagateBuildInputs = stdenv: stdenv //
{ mkDerivation = args: stdenv.mkDerivation (args // {
propagatedBuildInputs = (args.propagatedBuildInputs or []) ++ (args.buildInputs or []);
{ mkDerivation = args: (stdenv.mkDerivation args).overrideAttrs (super: {
propagatedBuildInputs = (super.propagatedBuildInputs or []) ++ (super.buildInputs or []);
buildInputs = [];
});
};
Expand All @@ -87,7 +87,7 @@ rec {
stdenv;
*/
addAttrsToDerivation = extraAttrs: stdenv: stdenv //
{ mkDerivation = args: stdenv.mkDerivation (args // extraAttrs); };
{ mkDerivation = args: (stdenv.mkDerivation args).overrideAttrs (_: extraAttrs); };


/* Return a modified stdenv that builds packages with GCC's coverage
Expand Down Expand Up @@ -181,17 +181,17 @@ rec {
binaries have debug info, and compiler optimisations are
disabled. */
keepDebugInfo = stdenv: stdenv //
{ mkDerivation = args: stdenv.mkDerivation (args // {
{ mkDerivation = args: (stdenv.mkDerivation args).overrideAttrs (super: {
dontStrip = true;
NIX_CFLAGS_COMPILE = toString (args.NIX_CFLAGS_COMPILE or "") + " -ggdb -Og";
NIX_CFLAGS_COMPILE = toString (super.NIX_CFLAGS_COMPILE or "") + " -ggdb -Og";
});
};


/* Modify a stdenv so that it uses the Gold linker. */
useGoldLinker = stdenv: stdenv //
{ mkDerivation = args: stdenv.mkDerivation (args // {
NIX_CFLAGS_LINK = toString (args.NIX_CFLAGS_LINK or "") + " -fuse-ld=gold";
{ mkDerivation = args: (stdenv.mkDerivation args).overrideAttrs (super: {
NIX_CFLAGS_LINK = toString (super.NIX_CFLAGS_LINK or "") + " -fuse-ld=gold";
});
};

Expand All @@ -201,8 +201,8 @@ rec {

WARNING: this breaks purity! */
impureUseNativeOptimizations = stdenv: stdenv //
{ mkDerivation = args: stdenv.mkDerivation (args // {
NIX_CFLAGS_COMPILE = toString (args.NIX_CFLAGS_COMPILE or "") + " -march=native";
{ mkDerivation = args: (stdenv.mkDerivation args).overrideAttrs (super: {
NIX_CFLAGS_COMPILE = toString (super.NIX_CFLAGS_COMPILE or "") + " -march=native";
NIX_ENFORCE_NO_NATIVE = false;

preferLocalBuild = true;
Expand Down
26 changes: 22 additions & 4 deletions pkgs/stdenv/generic/make-derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let
# to build it. This is a bit confusing for cross compilation.
inherit (stdenv) hostPlatform;
};
in rec {

# `mkDerivation` wraps the builtin `derivation` function to
# produce derivations that use this stdenv and its shell.
#
Expand All @@ -18,7 +18,7 @@ in rec {
#
# * https://nixos.org/nix/manual/#ssec-derivation
# Explanation about derivations in general
mkDerivation =
mkDerivation' =
{

# These types of dependencies are all exhaustively documented in
Expand Down Expand Up @@ -90,6 +90,9 @@ in rec {
, patches ? []

, ... } @ attrs:
# overrideAttrs needs access to the pre-evaluation attrs arg if it's a fixed-point function.
# since our caller evaluates that for us, it gets to provide overrideAttrs too.
overrideAttrs:

let
# TODO(@oxij, @Ericson2314): This is here to keep the old semantics, remove when
Expand Down Expand Up @@ -336,13 +339,28 @@ in rec {
lib.extendDerivation
validity.handled
({
overrideAttrs = f: mkDerivation (attrs // (f attrs));
inherit meta passthru;
inherit meta overrideAttrs passthru;
} //
# Pass through extra attributes that are not inputs, but
# should be made available to Nix expressions using the
# derivation (e.g., in assertions).
passthru)
(derivation derivationArg);

in rec {
# Allow `mkDerivation` to take either an attrset directly, or a fixed-point function that has a
# `self:` argument. The latter avoids the issues with using `rec { … }` and `overrideAttrs`.
mkDerivation =
attrs:
let
attrFun = if lib.isFunction attrs then attrs else (_: attrs);
overrideAttrs = f: mkDerivation (self:
let
super = attrFun self;
f' = f super;
feval = if lib.isFunction f'
then f self super # it's a new-style `self: super:` function
else f'; # it's an old-style `super:` function
in super // feval);
in mkDerivation' (lib.fix attrFun) overrideAttrs;
}