haskellPackages: support Template Haskell when cross compiling

This makes TH work when cross-compiling between aarch64 and x86_64 for
both glibc and musl.

pkgsStatic does not work, yet and neither does darwin.
This commit is contained in:
Alexandre Esteves
2025-09-22 05:30:31 +01:00
committed by Wolfgang Walther
parent 2c39ee5c0c
commit 20df4a1dbe
5 changed files with 117 additions and 3 deletions
@@ -2141,6 +2141,21 @@ builtins.intersectAttrs super {
cpython = doJailbreak super.cpython;
botan-bindings = super.botan-bindings.override { botan = pkgs.botan3; };
# Avoids a cycle by disabling use of the external interpreter for the packages that are dependencies of iserv-proxy.
# These in particular can't rely on template haskell for cross-compilation anyway as they can't rely on iserv-proxy.
inherit
(
let
noExternalInterpreter = overrideCabal {
enableExternalInterpreter = false;
};
in
lib.mapAttrs (_: noExternalInterpreter) { inherit (super) iserv-proxy network; }
)
iserv-proxy
network
;
}
// lib.optionalAttrs pkgs.config.allowAliases (
@@ -14,6 +14,23 @@ with haskellLib;
else
super.network;
# Avoids a cycle by disabling use of the external interpreter for the packages that are dependencies of iserv-proxy.
# See configuration-nix.nix, where iserv-proxy and network are handled.
# On Windows, network depends on temporary (see above), which depends on random, which depends on splitmix.
inherit
(
let
noExternalInterpreter = overrideCabal {
enableExternalInterpreter = false;
};
in
lib.mapAttrs (_: noExternalInterpreter) { inherit (super) random splitmix temporary; }
)
random
splitmix
temporary
;
# https://github.com/fpco/streaming-commons/pull/84
streaming-commons = appendPatch (fetchpatch {
name = "fix-headers-case.patch";
@@ -10,12 +10,60 @@
runCommandCC,
ghcWithHoogle,
ghcWithPackages,
haskellLib,
iserv-proxy,
nodejs,
writeShellScriptBin,
}:
let
isCross = stdenv.buildPlatform != stdenv.hostPlatform;
crossSupport = rec {
emulator = stdenv.hostPlatform.emulator buildPackages;
canProxyTH =
# iserv-proxy currently does not build on GHC 9.6
lib.versionAtLeast ghc.version "9.8" && stdenv.hostPlatform.emulatorAvailable buildPackages;
iservWrapper =
let
wrapperScript =
enableProfiling:
let
overrides = haskellLib.overrideCabal {
enableLibraryProfiling = enableProfiling;
enableExecutableProfiling = enableProfiling;
};
buildProxy = lib.getExe' iserv-proxy.build "iserv-proxy";
hostProxy = lib.getExe' (overrides iserv-proxy.host) "iserv-proxy-interpreter";
in
buildPackages.writeShellScriptBin ("iserv-wrapper" + lib.optionalString enableProfiling "-prof") ''
set -euo pipefail
PORT=$((5000 + $RANDOM % 5000))
(>&2 echo "---> Starting interpreter on port $PORT")
${emulator} ${hostProxy} tmp $PORT &
RISERV_PID="$!"
trap "kill $RISERV_PID" EXIT # Needs cleanup when building without sandbox
${buildProxy} $@ 127.0.0.1 "$PORT"
(>&2 echo "---> killing interpreter...")
'';
# GHC will add `-prof` to the external interpreter when doing a profiled build.
# Since a single derivation can build with both profiling and non-profiling versions
# we need both versions made available
both = buildPackages.symlinkJoin {
name = "iserv-wrapper-both";
paths = map wrapperScript [
false
true
];
};
in
"${both}/bin/iserv-wrapper";
};
# Pass the "wrong" C compiler rather than none at all so packages that just
# use the C preproccessor still work, see
# https://github.com/haskell/cabal/issues/6466 for details.
@@ -205,10 +253,15 @@ in
# of `meta.pkgConfigModules`. This option defaults to false for now, since
# this metadata is far from complete in nixpkgs.
__onlyPropagateKnownPkgConfigModules ? false,
enableExternalInterpreter ? isCross && crossSupport.canProxyTH,
}@args:
assert editedCabalFile != null -> revision != null;
# We only use iserv-proxy for the external interpreter
assert enableExternalInterpreter -> crossSupport.canProxyTH;
# --enable-static does not work on windows. This is a bug in GHC.
# --enable-static will pass -staticlib to ghc, which only works for mach-o and elf.
assert stdenv.hostPlatform.isWindows -> enableStaticLibraries == false;
@@ -298,7 +351,15 @@ let
"--hsc2hs-option=--cross-compile"
(optionalString enableHsc2hsViaAsm "--hsc2hs-option=--via-asm")
]
++ optional (allPkgconfigDepends != [ ]) "--with-pkg-config=${pkg-config.targetPrefix}pkg-config";
++ optional (allPkgconfigDepends != [ ]) "--with-pkg-config=${pkg-config.targetPrefix}pkg-config"
++ optionals enableExternalInterpreter (
map (opt: "--ghc-option=${opt}") [
"-fexternal-interpreter"
"-pgmi"
crossSupport.iservWrapper
]
);
makeGhcOptions = opts: lib.concatStringsSep " " (map (opt: "--ghc-option=${opt}") opts);
@@ -49,7 +49,7 @@ let
inherit (haskellLib) overrideCabal;
mkDerivationImpl = pkgs.callPackage ./generic-builder.nix {
inherit stdenv;
inherit stdenv haskellLib;
nodejs = buildPackages.nodejs-slim;
inherit (self)
buildHaskellPackages
@@ -57,6 +57,10 @@ let
ghcWithHoogle
ghcWithPackages
;
iserv-proxy = {
build = buildHaskellPackages.iserv-proxy;
host = self.iserv-proxy;
};
inherit (self.buildHaskellPackages) jailbreak-cabal;
hscolour = overrideCabal (drv: {
isLibrary = false;
+18 -1
View File
@@ -520,7 +520,24 @@ let
ghc948
;
};
};
}
//
removePlatforms
[
# Testing cross from x86_64-linux
"aarch64-darwin"
"aarch64-linux"
"x86_64-darwin"
]
{
haskellPackages = {
inherit (packagePlatforms pkgs.pkgsCross.aarch64-multiplatform.haskellPackages)
ghc
hello
th-orphans
;
};
};
};
})
(versionedCompilerJobs {