about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/haskell-modules/with-packages-wrapper.nix
blob: 03f59302a0d31f02286f4132a87e2fc4e3845d7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{ lib, stdenv, haskellPackages, symlinkJoin, makeWrapper
# GHC will have LLVM available if necessary for the respective target,
# so useLLVM only needs to be changed if -fllvm is to be used for a
# platform that has NCG support
, useLLVM ? false
, withHoogle ? false
# Whether to install `doc` outputs for GHC and all included libraries.
, installDocumentation ? true
, hoogleWithPackages
, postBuild ? ""
, ghcLibdir ? null # only used by ghcjs, when resolving plugins
}:

# This argument is a function which selects a list of Haskell packages from any
# passed Haskell package set.
#
# Example:
#   (hpkgs: [ hpkgs.mtl hpkgs.lens ])
selectPackages:

# It's probably a good idea to include the library "ghc-paths" in the
# compiler environment, because we have a specially patched version of
# that package in Nix that honors these environment variables
#
#   NIX_GHC
#   NIX_GHCPKG
#   NIX_GHC_DOCDIR
#   NIX_GHC_LIBDIR
#
# instead of hard-coding the paths. The wrapper sets these variables
# appropriately to configure ghc-paths to point back to the wrapper
# instead of to the pristine GHC package, which doesn't know any of the
# additional libraries.
#
# A good way to import the environment set by the wrapper below into
# your shell is to add the following snippet to your ~/.bashrc:
#
#   if [ -e ~/.nix-profile/bin/ghc ]; then
#     eval $(grep export ~/.nix-profile/bin/ghc)
#   fi

let
  inherit (haskellPackages) llvmPackages ghc;

  packages      = selectPackages haskellPackages
                  ++ lib.optional withHoogle (hoogleWithPackages selectPackages);

  isGhcjs       = ghc.isGhcjs or false;
  isHaLVM       = ghc.isHaLVM or false;
  ghc761OrLater = isGhcjs || isHaLVM || lib.versionOlder "7.6.1" ghc.version;
  packageDBFlag = if ghc761OrLater then "--global-package-db" else "--global-conf";
  ghcCommand'   = if isGhcjs then "ghcjs" else "ghc";
  ghcCommand    = "${ghc.targetPrefix}${ghcCommand'}";
  ghcCommandCaps= lib.toUpper ghcCommand';
  libDir        = if isHaLVM then "$out/lib/HaLVM-${ghc.version}"
                  else "$out/lib/${ghc.targetPrefix}${ghc.haskellCompilerName}"
                    + lib.optionalString (ghc ? hadrian) "/lib";
  # Boot libraries for GHC are present in a separate directory.
  bootLibDir    = let arch = if stdenv.targetPlatform.isAarch64
                             then "aarch64"
                             else "x86_64";
                      platform = if stdenv.targetPlatform.isDarwin then "osx" else "linux";
                  in "${ghc}/lib/${ghc.haskellCompilerName}/lib/${arch}-${platform}-${ghc.haskellCompilerName}";
  docDir        = "$out/share/doc/ghc/html";
  packageCfgDir = "${libDir}/package.conf.d";
  paths         = lib.concatLists (
                    builtins.map
                      (pkg: [ pkg ] ++ lib.optionals installDocumentation [ (lib.getOutput "doc" pkg) ])
                      (lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages))
                  );
  hasLibraries  = lib.any (x: x.isHaskellLibrary) paths;
  # CLang is needed on Darwin for -fllvm to work:
  # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
  llvm          = lib.makeBinPath
                  ([ llvmPackages.llvm ]
                   ++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang);
in

assert ghcLibdir != null -> (ghc.isGhcjs or false);

if paths == [] && !useLLVM then ghc else
symlinkJoin {
  # this makes computing paths from the name attribute impossible;
  # if such a feature is needed, the real compiler name should be saved
  # as a dedicated drv attribute, like `compiler-name`
  name = ghc.name + "-with-packages";
  paths = paths
          ++ [ ghc ]
          ++ lib.optionals installDocumentation [ (lib.getOutput "doc" ghc) ];
  nativeBuildInputs = [ makeWrapper ];
  postBuild = ''
    # wrap compiler executables with correct env variables

    for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do
      if [[ -x "${ghc}/bin/$prg" ]]; then
        rm -f $out/bin/$prg
        makeWrapper ${ghc}/bin/$prg $out/bin/$prg                           \
          --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"'                   \
          --set "NIX_${ghcCommandCaps}"        "$out/bin/${ghcCommand}"     \
          --set "NIX_${ghcCommandCaps}PKG"     "$out/bin/${ghcCommand}-pkg" \
          --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}"                  \
          --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"                  \
          ${lib.optionalString (ghc.isGhcjs or false)
            ''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"''
          } \
          ${lib.optionalString useLLVM ''--prefix "PATH" ":" "${llvm}"''}
      fi
    done

    for prg in runghc runhaskell; do
      if [[ -x "${ghc}/bin/$prg" ]]; then
        rm -f $out/bin/$prg
        makeWrapper ${ghc}/bin/$prg $out/bin/$prg                           \
          --add-flags "-f $out/bin/${ghcCommand}"                           \
          --set "NIX_${ghcCommandCaps}"        "$out/bin/${ghcCommand}"     \
          --set "NIX_${ghcCommandCaps}PKG"     "$out/bin/${ghcCommand}-pkg" \
          --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}"                  \
          --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
      fi
    done

    for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do
      if [[ -x "${ghc}/bin/$prg" ]]; then
        rm -f $out/bin/$prg
        makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "${packageDBFlag}=${packageCfgDir}"
      fi
    done

    # haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976
    if [[ -x "${ghc}/bin/haddock" ]]; then
      rm -f $out/bin/haddock
      makeWrapper ${ghc}/bin/haddock $out/bin/haddock    \
        --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"'  \
        --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
    fi

  '' + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !stdenv.targetPlatform.isiOS) ''
    # Work around a linker limit in macOS Sierra (see generic-builder.nix):
    local packageConfDir="${packageCfgDir}";
    local dynamicLinksDir="$out/lib/links";
    mkdir -p $dynamicLinksDir
    # Clean up the old links that may have been (transitively) included by
    # symlinkJoin:
    rm -f $dynamicLinksDir/*

    # Boot libraries are located differently than other libraries since GHC 9.6, so handle them separately.
    if [[ -x "${bootLibDir}" ]]; then
      ln -s "${bootLibDir}"/*.dylib $dynamicLinksDir
    fi

    for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
      ln -s $d/*.dylib $dynamicLinksDir
    done
    for f in $packageConfDir/*.conf; do
      # Initially, $f is a symlink to a read-only file in one of the inputs
      # (as a result of this symlinkJoin derivation).
      # Replace it with a copy whose dynamic-library-dirs points to
      # $dynamicLinksDir
      cp $f $f-tmp
      rm $f
      sed "N;s,dynamic-library-dirs:\s*.*\n,dynamic-library-dirs: $dynamicLinksDir\n," $f-tmp > $f
      rm $f-tmp
    done
  '') + ''
    ${lib.optionalString hasLibraries ''
     # GHC 8.10 changes.
     # Instead of replacing package.cache[.lock] with the new file,
     # ghc-pkg is now trying to open the file.  These file are symlink
     # to another nix derivation, so they are not writable.  Removing
     # them allow the correct behavior of ghc-pkg recache
     # See: https://github.com/NixOS/nixpkgs/issues/79441
     rm ${packageCfgDir}/package.cache.lock
     rm ${packageCfgDir}/package.cache

     $out/bin/${ghcCommand}-pkg recache
     ''}
    ${# ghcjs will read the ghc_libdir file when resolving plugins.
      lib.optionalString (isGhcjs && ghcLibdir != null) ''
      mkdir -p "${libDir}"
      rm -f "${libDir}/ghc_libdir"
      printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir"
    ''}
    $out/bin/${ghcCommand}-pkg check
  '' + postBuild;
  preferLocalBuild = true;
  passthru = {
    inherit (ghc) version meta;

    # Inform users about backwards incompatibilities with <= 21.05
    override = _: throw ''
      The ghc.withPackages wrapper itself can now be overridden, but no longer
      the result of calling it (as before). Consequently overrides need to be
      adjusted: Instead of

        (ghc.withPackages (p: [ p.my-package ])).override { withLLLVM = true; }

      use

        (ghc.withPackages.override { useLLVM = true; }) (p: [ p.my-package ])

      Also note that withLLVM has been renamed to useLLVM for consistency with
      the GHC Nix expressions.'';
  };
}