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

ghcWithPackages: fix dylib path issues of the boot libraries for GHC 9.6. #224542

Merged
merged 2 commits into from
Jun 28, 2023

Conversation

wavewave
Copy link
Contributor

@wavewave wavewave commented Apr 3, 2023

Dynamic libraries for GHC boot libraries (such as ghc-prim) are found missing in ghcWithPackages env since those libraries were not symlinked due to the change in the directory scheme.

Description of changes
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 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.

@ofborg ofborg bot added 10.rebuild-darwin: 11-100 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux labels Apr 3, 2023
@cdepillabout
Copy link
Member

@wavewave Thanks for putting this together.

Do you have an example of some command that will fail without this PR? I'm interested in how I can test that this PR is working.

@wavewave
Copy link
Contributor Author

wavewave commented Apr 4, 2023

Yes. On my laptop (Apple M1 macos 13.2.1), with the current master, when entering into a shell with ghcWithPackages env and run ghci:

% nix-shell -E 'let pkgs = import ./. {}; hsenv = pkgs.haskell.packages.ghc961.ghcWithPackages (p: [ p.acme-missiles ]);  in pkgs.mkShell { buildInputs = [ hsenv ]; }'

[nix-shell:~/repo/srcc/nixpkgs]$ ghc --interactive 
GHCi, version 9.6.1: https://www.haskell.org/ghc/  :? for help

<no location info>: warning: [-Wmissed-extra-shared-lib]
    dlopen(libHSghc-prim-0.10.0.dylib, 0x0005): tried: 'libHSghc-prim-0.10.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibHSghc-prim-0.10.0.dylib' (no such file), '/nix/store/s48ax821qnav0mlp4k59b1i23pisyj3r-ghc-9.6.1/lib/ghc-9.6.1/lib/aarch64-osx-ghc-9.6.1/libHSghc-prim-0.10.0.dylib' (no such file), '/nix/store/21fn46279sjpqqqqsy2cnkxr3rqzbwq6-apple-framework-CoreFoundation-11.0.0/Library/Frameworks/libHSghc-prim-0.10.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/nix/store/21fn46279sjpqqqqsy2cnkxr3rqzbwq6-apple-framework-CoreFoundation-11.0.0/Library/Frameworks/libHSghc-prim-0.10.0.dylib' (no such file), '/nix/store/s48ax821qnav0mlp4k59b1i23pisyj3r-ghc-9.6.1/lib/ghc-9.6.1/bin/../lib/aarch64-osx-ghc-9.6.1/libHSghc-prim-0.10.0.dylib' (no such file), '$ORIGIN/../../../lib/aarch64-osx-ghc-9.6.1/libHSghc-prim-0.10.0.dylib' (no such file), '/nix/store/21fn46279sjpqqqqsy2cnkxr3rqzbwq6-apple-framework-CoreFoundation-11.0.0/Library/Frameworks/libHSghc-prim-0.10.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/nix/store/21fn46279sjpqqqqsy2cnkxr3rqzbwq6-apple-framework-CoreFoundation-11.0.0/Library/Frameworks/libHSghc-prim-0.10.0.dylib' (no such file), '/usr/lib/libHSghc-prim-0.10.0.dylib' (no such file, not in dyld cache), 'libHSghc-prim-0.10.0.dylib' (no such file), '/usr/local/lib/libHSghc-prim-0.10.0.dylib' (no such file), '/usr/lib/libHSghc-prim-0.10.0.dylib' (no such file, not in dyld cache)
    It's OK if you don't want to use symbols from it directly.
    (the package DLL is loaded by the system linker
     which manages dependencies by itself).

<no location info>: warning: [-Wmissed-extra-shared-lib]
    dlopen(libHSbase-4.18.0.0.dylib, 0x0005): tried: 'libHSbase-4.18.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibHSbase-4.18.0.0.dylib' (no such file), '/nix/store/s48ax821qnav0mlp4k59b1i23pisyj3r-ghc-9.6.1/lib/ghc-9.6.1/lib/aarch64-osx-ghc-9.6.1/libHSbase-4.18.0.0.dylib' (no such file), '/nix/store/21fn46279sjpqqqqsy2cnkxr3rqzbwq6-apple-framework-CoreFoundation-11.0.0/Library/Frameworks/libHSbase-4.18.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/nix/store/21fn46279sjpqqqqsy2cnkxr3rqzbwq6-apple-framework-CoreFoundation-11.0.0/Library/Frameworks/libHSbase-4.18.0.0.dylib' (no such file), '/nix/store/s48ax821qnav0mlp4k59b1i23pisyj3r-ghc-9.6.1/lib/ghc-9.6.1/bin/../lib/aarch64-osx-ghc-9.6.1/libHSbase-4.18.0.0.dylib' (no such file), '$ORIGIN/../../../lib/aarch64-osx-ghc-9.6.1/libHSbase-4.18.0.0.dylib' (no such file), '/nix/store/21fn46279sjpqqqqsy2cnkxr3rqzbwq6-apple-framework-CoreFoundation-11.0.0/Library/Frameworks/libHSbase-4.18.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/nix/store/21fn46279sjpqqqqsy2cnkxr3rqzbwq6-apple-framework-CoreFoundation-11.0.0/Library/Frameworks/libHSbase-4.18.0.0.dylib' (no such file), '/usr/lib/libHSbase-4.18.0.0.dylib' (no such file, not in dyld cache), 'libHSbase-4.18.0.0.dylib' (no such file), '/usr/local/lib/libHSbase-4.18.0.0.dylib' (no such file), '/usr/lib/libHSbase-4.18.0.0.dylib' (no such file, not in dyld cache)
    It's OK if you don't want to use symbols from it directly.
    (the package DLL is loaded by the system linker
     which manages dependencies by itself).
ghci> :q
Leaving GHCi.

So you see errors on missing dynamic libraries.

With this branch,

% nix-shell -E 'let pkgs = import ./. {}; hsenv = pkgs.haskell.packages.ghc961.ghcWithPackages (p: [ p.acme-missiles ]);  in pkgs.mkShell { buildInputs = [ hsenv ]; }'

[nix-shell:~/repo/srcc/nixpkgs]$ ghc --interactive 
GHCi, version 9.6.1: https://www.haskell.org/ghc/  :? for help
ghci> :q
Leaving GHCi.

no such errors.

@sternenseemann
Copy link
Member

Can you test the following patch for me and report if it solves the problem:

diff --git a/pkgs/development/haskell-modules/with-packages-wrapper.nix b/pkgs/development/haskell-modules/with-packages-wrapper.nix
index 34eb2b71d75..eef7a060bae 100644
--- a/pkgs/development/haskell-modules/with-packages-wrapper.nix
+++ b/pkgs/development/haskell-modules/with-packages-wrapper.nix
@@ -136,7 +136,8 @@ symlinkJoin {
     # Clean up the old links that may have been (transitively) included by
     # symlinkJoin:
     rm -f $dynamicLinksDir/*
-    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
+    # Starting with GHC 9.6 (due to hadrian?) dynamic-library-dirs for boot libraries uses the ''${pkgroot} variable.
+    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u|sed -e 's|''${pkgroot}|'"${libDir}|"); do
       ln -s $d/*.dylib $dynamicLinksDir
     done
     for f in $packageConfDir/*.conf; do

Don't have access to a mac at the moment, hopefully I haven't messed up the sed call.

@wavewave wavewave force-pushed the ghcWithPackages-ghc96-fix branch from 74cdd5b to 34957cc Compare April 26, 2023 16:19
@wavewave
Copy link
Contributor Author

wavewave commented Apr 26, 2023

@sternenseemann : applied your patch and ran the same nix-shell command as above and I got this error:

% git diff
diff --git a/pkgs/development/haskell-modules/with-packages-wrapper.nix b/pkgs/development/haskell-modules/with-packages-wrapper.nix
index 34eb2b71d75..eef7a060bae 100644
--- a/pkgs/development/haskell-modules/with-packages-wrapper.nix
+++ b/pkgs/development/haskell-modules/with-packages-wrapper.nix
@@ -136,7 +136,8 @@ symlinkJoin {
     # Clean up the old links that may have been (transitively) included by
     # symlinkJoin:
     rm -f $dynamicLinksDir/*
-    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
+    # Starting with GHC 9.6 (due to hadrian?) dynamic-library-dirs for boot libraries uses the ''${pkgroot} variable.
+    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u|sed -e 's|''${pkgroot}|'"${libDir}|"); do
       ln -s $d/*.dylib $dynamicLinksDir
     done
     for f in $packageConfDir/*.conf; do


% nix-shell -E 'let pkgs = import ./. {}; hsenv = pkgs.haskell.packages.ghc961.ghcWithPackages (p: [ p.acme-missiles ]);  in pkgs.mkShell { buildInputs = [ hsenv ]; }'
this derivation will be built:
  /nix/store/1rv8xldr5fm3q505vqw6pbplp1diz3sa-ghc-9.6.1-with-packages.drv
building '/nix/store/1rv8xldr5fm3q505vqw6pbplp1diz3sa-ghc-9.6.1-with-packages.drv'...
ln: failed to create symbolic link '/nix/store/wyq018647wpi2djp8y98v4mfs68b0bqq-ghc-9.6.1-with-packages/lib/links/libHSacme-missiles-0.3-JAFfVisGlkc9dEqSLpYnVw-ghc9.6.1.dylib': File exists
error: builder for '/nix/store/1rv8xldr5fm3q505vqw6pbplp1diz3sa-ghc-9.6.1-with-packages.drv' failed with exit code 1;
       last 1 log lines:
       > ln: failed to create symbolic link '/nix/store/wyq018647wpi2djp8y98v4mfs68b0bqq-ghc-9.6.1-with-packages/lib/links/libHSacme-missiles-0.3-JAFfVisGlkc9dEqSLpYnVw-ghc9.6.1.dylib': File exists
       For full logs, run 'nix log /nix/store/1rv8xldr5fm3q505vqw6pbplp1diz3sa-ghc-9.6.1-with-packages.drv'.
%

@sternenseemann
Copy link
Member

What is the output of the following patch applied?

diff --git a/pkgs/development/haskell-modules/with-packages-wrapper.nix b/pkgs/development/haskell-modules/with-packages-wrapper.nix
index 34eb2b71d75..eef7a060bae 100644
--- a/pkgs/development/haskell-modules/with-packages-wrapper.nix
+++ b/pkgs/development/haskell-modules/with-packages-wrapper.nix
@@ -136,7 +136,8 @@ symlinkJoin {
     # Clean up the old links that may have been (transitively) included by
     # symlinkJoin:
     rm -f $dynamicLinksDir/*
-    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
+    # Starting with GHC 9.6 (due to hadrian?) dynamic-library-dirs for boot libraries uses the ''${pkgroot} variable.
+    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u| tee /dev/stderr | sed -e 's|''${pkgroot}|'"${libDir}|"); do
       ln -s $d/*.dylib $dynamicLinksDir
     done
     for f in $packageConfDir/*.conf; do

@wavewave
Copy link
Contributor Author

I got this:

% nix-shell -E 'let pkgs = import ./. {}; hsenv = pkgs.haskell.packages.ghc961.ghcWithPackages (p: [ p.acme-missiles ]);  in pkgs.mkShell { buildInputs = [ hsenv ]; }'
this derivation will be built:
  /nix/store/0wak33pwv1lri00wd7a6iqba6k01j308-ghc-9.6.1-with-packages.drv
building '/nix/store/0wak33pwv1lri00wd7a6iqba6k01j308-ghc-9.6.1-with-packages.drv'...

${pkgroot}/../lib/aarch64-osx-ghc-9.6.1
/nix/store/1pnx247mrcyyd0pvm7mpgdhf4gkj9w46-acme-missiles-0.3/lib/ghc-9.6.1/lib/aarch64-osx-ghc-9.6.1
ln: failed to create symbolic link '/nix/store/s67hy5kzbjcszq886ri8qyfn0wf5li1b-ghc-9.6.1-with-packages/lib/links/libHSacme-missiles-0.3-JAFfVisGlkc9dEqSLpYnVw-ghc9.6.1.dylib': File exists
error: builder for '/nix/store/0wak33pwv1lri00wd7a6iqba6k01j308-ghc-9.6.1-with-packages.drv' failed with exit code 1;
       last 4 log lines:
       >
       > ${pkgroot}/../lib/aarch64-osx-ghc-9.6.1
       > /nix/store/1pnx247mrcyyd0pvm7mpgdhf4gkj9w46-acme-missiles-0.3/lib/ghc-9.6.1/lib/aarch64-osx-ghc-9.6.1
       > ln: failed to create symbolic link '/nix/store/s67hy5kzbjcszq886ri8qyfn0wf5li1b-ghc-9.6.1-with-packages/lib/links/libHSacme-missiles-0.3-JAFfVisGlkc9dEqSLpYnVw-ghc9.6.1.dylib': File exists
       For full logs, run 'nix log /nix/store/0wak33pwv1lri00wd7a6iqba6k01j308-ghc-9.6.1-with-packages.drv'.

@sternenseemann
Copy link
Member

I suspect the empty line is the problem, so let's add an grep -v '^$':

diff --git a/pkgs/development/haskell-modules/with-packages-wrapper.nix b/pkgs/development/haskell-modules/with-packages-wrapper.nix
index 34eb2b71d75..eef7a060bae 100644
--- a/pkgs/development/haskell-modules/with-packages-wrapper.nix
+++ b/pkgs/development/haskell-modules/with-packages-wrapper.nix
@@ -136,7 +136,8 @@ symlinkJoin {
     # Clean up the old links that may have been (transitively) included by
     # symlinkJoin:
     rm -f $dynamicLinksDir/*
-    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
+    # Starting with GHC 9.6 (due to hadrian?) dynamic-library-dirs for boot libraries uses the ''${pkgroot} variable.
+    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u| grep -v '^$' | sed -e 's|''${pkgroot}|'"${libDir}|"); do
       ln -s $d/*.dylib $dynamicLinksDir
     done
     for f in $packageConfDir/*.conf; do

@wavewave
Copy link
Contributor Author

wavewave commented May 6, 2023

I found another bug was lurking in ghcWithPackages, so I fixed it here: https://github.com/NixOS/nixpkgs/pull/224542/files#diff-2cd72dadd104d58b10c98c5467f9c4eb2f2b2cf431ad8abee5668031c0bf7d0eL149

When that sed line before the patch replaces dynamic-library-dirs in package conf, it swallows the next data-dir: line.
To see that, compare ghc-pkg conf of filepath library between just after a fresh GHC build and wrapped ghcWithPackages env:

(after-ghc-build)$ _build/stage1/bin/ghc-pkg describe filepath

name:                 filepath
version:              1.4.100.1

(...)

import-dirs:          ${pkgroot}/../lib/aarch64-osx-ghc-9.6.1/filepath-1.4.100.1
library-dirs:         ${pkgroot}/../lib/aarch64-osx-ghc-9.6.1/filepath-1.4.100.1
dynamic-library-dirs: ${pkgroot}/../lib/aarch64-osx-ghc-9.6.1
data-dir:
    ${pkgroot}/../share/aarch64-osx-ghc-9.6.1/filepath-1.4.100.1

hs-libraries:         HSfilepath-1.4.100.1
depends:
    base-4.18.0.0 bytestring-0.11.4.0 deepseq-1.4.8.1 exceptions-0.10.7
    template-haskell-2.20.0.0

haddock-interfaces:
    ${pkgroot}/../../doc/html/libraries/filepath-1.4.100.1/filepath.haddock

haddock-html:         ${pkgroot}/../../doc/html/libraries/filepath-1.4.100.1
pkgroot: "/Users/ianwookim/repo/srcc/ghc-9.6/_build/stage1/lib"
(in-wrapped-ghc-shell) $ ghc-pkg describe filepath 
name:                 filepath
version:              1.4.100.1

(...)

import-dirs:          ${pkgroot}/../lib/aarch64-osx-ghc-9.6.1/filepath-1.4.100.1
library-dirs:         ${pkgroot}/../lib/aarch64-osx-ghc-9.6.1/filepath-1.4.100.1
dynamic-library-dirs:
    /nix/store/zxmwvns8zg4ff47vw9hqgazjkiqa26kv-ghc-9.6.1-with-packages/lib/links
    ${pkgroot}/../share/aarch64-osx-ghc-9.6.1/filepath-1.4.100.1

hs-libraries:         HSfilepath-1.4.100.1
depends:
    base-4.18.0.0 bytestring-0.11.4.0 deepseq-1.4.8.1 exceptions-0.10.7
    template-haskell-2.20.0.0

haddock-interfaces:
    ${pkgroot}/../../../../7chf44x96fpr1di5gqqz828m3dznr1yy-ghc-9.6.1-doc/share/doc/ghc/html/libraries/filepath-1.4.100.1/filepath.haddock

haddock-html:
    ${pkgroot}/../../../../7chf44x96fpr1di5gqqz828m3dznr1yy-ghc-9.6.1-doc/share/doc/ghc/html/libraries/filepath-1.4.100.1
pkgroot: "/nix/store/zxmwvns8zg4ff47vw9hqgazjkiqa26kv-ghc-9.6.1-with-packages/lib/ghc-9.6.1/lib"

The last commit fixes this problem.

@cdepillabout
Copy link
Member

@wavewave Are you still waiting for a review from @sternenseemann for this?

Also, could you refactor the commit messages to adhere to https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#submitting-changes

@alexfmpe
Copy link
Member

Also started getting the missing dynamic libraries warnings on mac after bumping to 9.6. Cabal build also spits out some ld warnings.
Can confirm this fixed both issues for me.

@wavewave wavewave changed the title Make dylib links from the GHC boot libraries separately for GHC >= 9.6. ghcWithPackages: make dylib links from the GHC boot libraries separately for GHC >= 9.6. Jun 14, 2023
@wavewave wavewave changed the title ghcWithPackages: make dylib links from the GHC boot libraries separately for GHC >= 9.6. ghcWithPackages: fix dylib path issues of the boot libraries for GHC 9.6. Jun 14, 2023
wavewave added 2 commits June 14, 2023 10:04
From GHC 9.6 on, the boot libraries (dependencies of the ghc library) are
present in a separate directory after the installation, and thus the
wrapper environment provided by ghcWithPackages needs to handle the links
to the boot dynamic libraries separately than other ordinary ones.
When making ghcWithPackages, haskell package db configuration
is modified by sed, but newlines are not handled properly, so
resulting in
```
dynamic-library-dirs: ${pkgroot}/../lib/aarch64-osx-ghc-9.6.1
data-dir:
    ${pkgroot}/../share/aarch64-osx-ghc-9.6.1/filepath-1.4.100.1
```
is replaced like (data-dir: is swallowed)
```
dynamic-library-dirs:
    /nix/store/zxmwvns8zg4ff47vw9hqgazjkiqa26kv-ghc-9.6.1-with-packages/lib/links
    ${pkgroot}/../share/aarch64-osx-ghc-9.6.1/filepath-1.4.100.1
```
This fixes the bug.
@wavewave wavewave force-pushed the ghcWithPackages-ghc96-fix branch from 66593d9 to 77504c6 Compare June 14, 2023 17:17
@siraben
Copy link
Member

siraben commented Jun 27, 2023

Can confirm this fixes the issue on aarch64-darwin as well for me.

@siraben siraben merged commit 6eb5c1b into NixOS:master Jun 28, 2023
@sternenseemann
Copy link
Member

The proper way to solve this would have been #224542 (comment), the problem is that this was never tested by anyone, so the PR was on hold from my side.

@siraben
Copy link
Member

siraben commented Jun 28, 2023

Apologies. I was too hasty in merging without thoroughly reviewing all the previous discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: haskell 10.rebuild-darwin: 11-100 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux
Development

Successfully merging this pull request may close these issues.

5 participants