@@ -31,6 +31,9 @@ let self =
3131, # If enabled, GHC will be built with the GPL-free but slower integer-simple
3232 # library instead of the faster but GPLed integer-gmp library.
3333 enableIntegerSimple ? !(lib.any (lib.meta.platformMatch stdenv.hostPlatform) gmp.meta.platforms), gmp
34+, # If enabled, GHC will be built with the GPL-free native backend of the
35+ # bignum library that is nearly as fast as GMP
36+ enableNativeBignum ? !((lib.any (lib.meta.platformMatch stdenv.hostPlatform) gmp.meta.platforms) || enableIntegerSimple)
3437
3538, # If enabled, use -fPIC when compiling static libs.
3639 enableRelocatedStaticLibs ? stdenv.targetPlatform != stdenv.hostPlatform && !stdenv.targetPlatform.isAarch32
@@ -71,7 +74,11 @@ let self =
7174, extra-passthru ? {}
7275}@args:
7376
74−assert !enableIntegerSimple -> gmp != null;
77+assert !(enableIntegerSimple || enableNativeBignum) -> gmp != null;
78+
79+# Early check to make sure only one of these is enabled
80+assert enableNativeBignum -> !enableIntegerSimple;
81+assert enableIntegerSimple -> !enableNativeBignum;
7582
7683let
7784 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
@@ -79,6 +86,17 @@ let
7986
8087 inherit (bootPkgs) ghc;
8188
89+ ghcHasNativeBignum = builtins.compareVersions ghc-version "9.0" >= 0;
90+
91+ bignumSpec =
92+ assert ghcHasNativeBignum -> !enableIntegerSimple;
93+ assert !ghcHasNativeBignum -> !enableNativeBignum;
94+ if ghcHasNativeBignum then ''
95+ BIGNUM_BACKEND = ${if enableNativeBignum then "native" else "gmp"}
96+ '' else ''
97+ INTEGER_LIBRARY = ${if enableIntegerSimple then "integer-simple" else "integer-gmp"}
98+ '';
99+
82100 # TODO check if this possible fix for segfaults works or not.
83101 targetLibffi =
84102 # on native platforms targetPlatform.{libffi, gmp} do not exist; thus fall back
@@ -106,7 +124,7 @@ let
106124 include mk/flavours/\$(BuildFlavour).mk
107125 endif
108126 DYNAMIC_GHC_PROGRAMS = ${if enableShared then "YES" else "NO"}
109− INTEGER_LIBRARY = ${if enableIntegerSimple then "integer-simple" else "integer-gmp"}
127+ '' + bignumSpec + ''
110128 EXTRA_HADDOCK_OPTS += --quickjump --hyperlinked-source
111129 '' + lib.optionalString (targetPlatform != hostPlatform) ''
112130 CrossCompilePrefix = ${targetPrefix}
@@ -121,6 +139,10 @@ let
121139 '' + lib.optionalString enableRelocatedStaticLibs ''
122140 GhcLibHcOpts += -fPIC
123141 GhcRtsHcOpts += -fPIC
142+ GhcRtsCcOpts += -fPIC
143+ '' + lib.optionalString (enableRelocatedStaticLibs && targetPlatform.isx86_64 && !targetPlatform.isWindows) ''
144+ GhcLibHcOpts += -fexternal-dynamic-refs
145+ GhcRtsHcOpts += -fexternal-dynamic-refs
124146 '' + lib.optionalString enableDWARF ''
125147 GhcLibHcOpts += -g3
126148 GhcRtsHcOpts += -g3