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: Fix overriding + overrideAttrs #134463

Merged
merged 2 commits into from
Aug 18, 2021

Conversation

Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented Aug 17, 2021

Motivation for this change

The old stdenv adapters were subtly wrong in two ways:

  • overrideAttrs leaked the original, unoverridden mkDerivation.

  • stdenv.override would throw away any manually-set mkDerivation
    from a stdenv reverting to the original.

Now, mkDerivation is controlled (nearly directly) via an argument, and
always correctly closes over the final ("self") stdenv. This means the
adapters can work entirely via .override without any manual stdenv // ..., and both those issues are fixed.

Note hashes are changed, because stdenvs no previously overridden like
stdenvNoCC and crossLibcStdenv now are. I had to add some
dontDisableStatic = true accordingly. The flip side however is that
since the overrides compose, we no longer need to override anything but
the default stdenv from which all the others are created.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

pkgs/stdenv/adapters.nix Outdated Show resolved Hide resolved
pkgs/stdenv/adapters.nix Outdated Show resolved Hide resolved
pkgs/stdenv/generic/default.nix Show resolved Hide resolved
pkgs/stdenv/generic/make-derivation.nix Outdated Show resolved Hide resolved
Copy link
Contributor

@Mindavi Mindavi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about the stdenv, but added some nits about typos :)

Edit: pamplemousse was faster

dontDisableStatic = true;
} // pkgs.lib.optionalAttrs (!(args.dontAddStaticConfigureFlags or false)) {
} // lib.optionalAttrs (!(args.dontAddStaticConfigureFlags or false)) {
configureFlags = (args.configureFlags or []) ++ [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for reference, following a quick chat @Ericson2314 and I had:
For packages that unconditionally add --enable-shared in their configureFlags (for example: musl), this will effectively add both --enable-shared --disable-shared to the compilation command...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this regression has been there since the introduction of dontAddStaticConfigureFlags

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fixing a number of them in #133008

@symphorien
Copy link
Member

This break musl somehow:

./tools/install.sh -D obj/ld.musl-clang /nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/bin/ld.musl-clang
Moving /nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/bin/musl-gcc to /nix/store/iphgzmq6c5fi5pr84lh9sbzmhz8aikwa-musl-static-x86_64-unknown-linux-musl-1.2.2-dev/bin/musl-gcc
rmdir: failed to remove '/nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/bin': Directory not empty
Moving /nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/bin/musl-clang to /nix/store/iphgzmq6c5fi5pr84lh9sbzmhz8aikwa-musl-static-x86_64-unknown-linux-musl-1.2.2-dev/bin/musl-clang
rmdir: failed to remove '/nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/bin': Directory not empty
Moving /nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/bin/ld.musl-clang to /nix/store/iphgzmq6c5fi5pr84lh9sbzmhz8aikwa-musl-static-x86_64-unknown-linux-musl-1.2.2-dev/bin/ld.musl-clang
rmdir: failed to remove '/nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/bin': Directory not empty
Moving /nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/lib/musl-gcc.specs to /nix/store/iphgzmq6c5fi5pr84lh9sbzmhz8aikwa-musl-static-x86_64-unknown-linux-musl-1.2.2-dev/lib/musl-gcc.specs
rmdir: failed to remove '/nix/store/v27r72r03h873j895qg9phq4j6175msq-musl-static-x86_64-unknown-linux-musl-1.2.2/lib': Directory not empty
/nix/store/ak7vllbyx80qz6ifhjvipxgai7axsay9-x86_64-unknown-linux-musl-binutils-2.35.1/bin/x86_64-unknown-linux-musl-ld:INSTALL: file format not recognized; treating as linker script
/nix/store/ak7vllbyx80qz6ifhjvipxgai7axsay9-x86_64-unknown-linux-musl-binutils-2.35.1/bin/x86_64-unknown-linux-musl-ld:INSTALL:2: syntax error
collect2: error: ld returned 1 exit status
builder for '/nix/store/zwppa911qs55y0pv3xs1iyn7rqfa1ydm-musl-static-x86_64-unknown-linux-musl-1.2.2.drv' failed with exit code 1

@Pamplemousse
Copy link
Member

@symphorien I think pkgsStatic.musl was broken before that.
I tried fixing it yesterday, but there are many things seemingly going wrong in the postInstall script...

cc @dtzWill @thoughtpolice (maintainers of musl)

@symphorien
Copy link
Member

well I rebased #133008 on this and got new failures due to that, so there must be a regression somewhere...

@Pamplemousse
Copy link
Member

Pamplemousse commented Aug 18, 2021

@symphorien When trying to locate the "regression", I checked out 20.03, and pkgsStatic.musl failed to build with similar errors...
So, unless I messed up my experiment, I would say the "regression" is not coming from this PR.

@symphorien
Copy link
Member

On the parent commit of this pr, pkgsStatic.hello build, but not on this commit. The failure involves musl but maybe it's a red herring, since as you pointed out pkgsStatic.musl has not been building for a long time.

@Pamplemousse
Copy link
Member

@symphorien My guess is that this PR has just made the failure of pkgsStatic.musl apparent (i.e. happen when building other pkgsStatic packages - as if musl was used before, and now pkgsStatic.musl is).

The old stdenv adapters were subtly wrong in two ways:

 - `overrideAttrs` leaked the original, unoverridden `mkDerivation`.

 - `stdenv.override` would throw away any manually-set `mkDerivation`
   from a stdenv reverting to the original.

Now, `mkDerivation` is controlled (nearly directly) via an argument, and
always correctly closes over the final ("self") stdenv. This means the
adapters can work entirely via `.override` without any manual `stdenv //
...`, and both those issues are fixed.

Note hashes are changed, because stdenvs no previously overridden like
`stdenvNoCC` and `crossLibcStdenv` now are. I had to add some
`dontDisableStatic = true` accordingly. The flip side however is that
since the overrides compose, we no longer need to override anything but
the default `stdenv` from which all the others are created.
We previously make it just be the function, not a single-item attrset,
without deindenting to make a readable diff. No we deindent.
@Ericson2314
Copy link
Member Author

OK fixed the issue. The derivation to check is pkgsStatic.muslCross (which is separate for stupid reasons; hope to finally get rid of that with #132343). Comparing it with nix-diff we see that since stdenvNoCC and crossLibcStdenv now pick up the overrides from stdenv, which means that Musl no longer was building the dynamic linker, which meant that the on-the-fly compilation of iconv was failing.

No, I am not sure why we want the dynamic build of Musl, but I didn't want to make a big behavioral change like that, so I just added dontAddStaticConfigureFlags = true instead.

@ofborg ofborg bot requested review from thoughtpolice and dtzWill August 18, 2021 17:46
@Ericson2314 Ericson2314 merged commit 221ca6f into NixOS:master Aug 18, 2021
@Ericson2314 Ericson2314 deleted the stdenv-adapter-latebind branch August 18, 2021 23:01
@r-burns
Copy link
Contributor

r-burns commented Aug 19, 2021

I think this somehow removed pkgsStatic.stdenv.__bootPackages:

> nix-build . -A pkgsStatic.stdenv.__bootPackages 
error: attribute '__bootPackages' in selection path 'pkgsStatic.stdenv.__bootPackages' not found

@r-burns
Copy link
Contributor

r-burns commented Aug 20, 2021

Oh, I think this is happening because __bootPackages and __hatPackages are added to each stdenv stage using //.

stdenv = args.stdenv // {
# For debugging
__bootPackages = prevStage;
__hatPackages = nextStage;
};

So if stdenv is overridden, these extra attributes are discarded.

@Ericson2314
Copy link
Member Author

Yup that's why. I feel like the cure might be worse than the disease. In any event, I hope we might finally get away from that and the way bootstrapping is done today once #132343 is done.

@andir
Copy link
Member

andir commented Aug 23, 2021

This broke the keepDebugInfo adapter. I used to be able to build pkgs.keepDebugInfo (pkgs.nix.overrideAttrs (_: { })) but now that fails with:

called with unexpected argument 'mkDerivationFromStdenv', at lib/customisation.nix:79:63

Also seeing a case where the override attribute is now missing:

error: while evaluating 'g' at /home/andi/dev/nixos/nixpkgs/lib/attrsets.nix:298:19, called from undefined position:
while evaluating anonymous function at /home/andi/dev/nixos/nixpkgs/lib/modules.nix:140:72, called from /home/andi/dev/nixos/nixpkgs/lib/attrsets.nix:301:20:
while evaluating the attribute 'value' at /home/andi/dev/nixos/nixpkgs/lib/modules.nix:525:9:
while evaluating the option `nix.package':
while evaluating the attribute 'mergedValue' at /home/andi/dev/nixos/nixpkgs/lib/modules.nix:557:5:
while evaluating the attribute 'values' at /home/andi/dev/nixos/nixpkgs/lib/modules.nix:551:9:
while evaluating the attribute 'values' at /home/andi/dev/nixos/nixpkgs/lib/modules.nix:650:7:
while evaluating anonymous function at /home/andi/dev/nixos/nixpkgs/lib/modules.nix:537:28, called from /home/andi/dev/nixos/nixpkgs/lib/modules.nix:537:17:
while evaluating definitions from `/etc/nixos/common.nix':
while evaluating 'dischargeProperties' at /home/andi/dev/nixos/nixpkgs/lib/modules.nix:609:25, called from /home/andi/dev/nixos/nixpkgs/lib/modules.nix:538:137:
while evaluating the attribute 'value' at /home/andi/dev/nixos/nixpkgs/lib/modules.nix:392:44:
while evaluating 'enableDebugging' at /home/andi/dev/nixos/nixpkgs/pkgs/top-level/all-packages.nix:693:21, called from /etc/nixos/common.nix:35:15:
while evaluating anonymous function at /home/andi/dev/nixos/nixpkgs/lib/customisation.nix:77:32, called from /home/andi/dev/nixos/nixpkgs/pkgs/top-level/all-packages.nix:693:26:
while evaluating 'makeOverridable' at /home/andi/dev/nixos/nixpkgs/lib/customisation.nix:67:24, called from /home/andi/dev/nixos/nixpkgs/lib/customisation.nix:77:41:
while evaluating anonymous function at /home/andi/dev/nixos/nixpkgs/lib/customisation.nix:79:54, called from /home/andi/dev/nixos/nixpkgs/lib/customisation.nix:69:16:
while evaluating anonymous function at /home/andi/dev/nixos/nixpkgs/lib/customisation.nix:86:29, called from /home/andi/dev/nixos/nixpkgs/lib/customisation.nix:79:60:
while evaluating 'common' at /home/andi/dev/nixos/nixpkgs/pkgs/tools/package-management/nix/default.nix:12:3, called from /home/andi/dev/nixos/nixpkgs/lib/customisation.nix:79:63:
while evaluating 'keepDebugInfo' at /home/andi/dev/nixos/nixpkgs/pkgs/stdenv/adapters.nix:209:19, called from /home/andi/dev/nixos/nixpkgs/pkgs/top-level/all-packages.nix:693:50:
attribute 'override' missing, at /home/andi/dev/nixos/nixpkgs/pkgs/stdenv/adapters.nix:210:5

relevant nixos config:

    nix.package = pkgs.enableDebugging (pkgs.nix.overrideAttrs ({ patches, ... }: {
      patches = patches ++ [
        ./nix-segfault.patch
        ./nix-segfault-2.patch
        ./nix-remote-build.patch
        ./add-address-sanitizer.patch
      ];
      doInstallCheck = false;
      separateDebugInfo = false;
    }));

foldu added a commit to foldu/nixos-config that referenced this pull request Aug 24, 2021
@teto
Copy link
Member

teto commented Aug 25, 2021

I can confirm this ^ . Breaks enableDebugging too (since it calls keepDebugInfo).

@Ericson2314
Copy link
Member Author

I talked to @andir a bit in chat. keepDebugInfo was only meant to be used with stdenv. I suppose we can make another one at the top level for uses of mkDerivation (i.e. normal Nixpkgs derivations).

teto added a commit to teto/neovim that referenced this pull request Aug 29, 2021
NixOS/nixpkgs#134463 made keepDebugInfo obsolete
for generic packages.
This copies what keepDebugInfo used to do.
teto added a commit to teto/neovim that referenced this pull request Aug 29, 2021
NixOS/nixpkgs#134463 made keepDebugInfo obsolete
for generic packages.
This copies what keepDebugInfo used to do.
teto added a commit to neovim/neovim that referenced this pull request Aug 30, 2021
NixOS/nixpkgs#134463 made keepDebugInfo obsolete
for generic packages.
This copies what keepDebugInfo used to do.
@trofi
Copy link
Contributor

trofi commented Sep 5, 2021

Should enableDebugging still work? Comment says it should:

  # intended to be used like nix-build -E 'with import <nixpkgs> {}; enableDebugging fooPackage'
  enableDebugging = pkg: pkg.override { stdenv = stdenvAdapters.keepDebugInfo pkg.stdenv; };

Filed #136756

muniter pushed a commit to muniter/neovim that referenced this pull request Sep 8, 2021
NixOS/nixpkgs#134463 made keepDebugInfo obsolete
for generic packages.
This copies what keepDebugInfo used to do.
@roberth
Copy link
Member

roberth commented Sep 9, 2021

We should seriously consider simplifying the problem away. #136903 (comment)
I have no confidence that we can use a redundant layer of overriding correctly.
tl;dr: we can remove the fixpoint we don't need by improving the one we do need, makingoverrideAttrs more powerful.

@Ericson2314
Copy link
Member Author

@bluescreen303 See #136756

lewis6991 pushed a commit to lewis6991/neovim that referenced this pull request Dec 12, 2021
NixOS/nixpkgs#134463 made keepDebugInfo obsolete
for generic packages.
This copies what keepDebugInfo used to do.
});
};
}));
} // lib.optionalAttrs (stdenv0.hostPlatform.libc == "libc") {
Copy link

@ghost ghost Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ericson2314 did you mean "glibc" here instead of "libc"?

@picostove reports that changing it fixes #244232.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He did mean glibc #134923 (comment)

since that one had the typo, neither is doing anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.