Skip to content

Commit 788e198

Browse files
hamishmackangerman
andauthored
Add compiler-nix-name to cabalProject (#610)
* Add compiler-nix-name to cabalProject Setting the compiler explicitly does not make it easy for cross compilation to also choose the correct compiler when compiling build tools for the build platform. Also it is easy to pass the wrong compiler type. Specifying just the nix-name of the compiler makes that less likely. Co-authored-by: Moritz Angermann <[email protected]>
1 parent fac07f3 commit 788e198

File tree

9 files changed

+93
-11
lines changed

9 files changed

+93
-11
lines changed

docs/user-guide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ If your project has a `cabal.project` you can add a `default.nix` like this:
9494

9595
```nix
9696
{ pkgs ? import <nixpkgs> (import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {}).nixpkgsArgs
97-
, haskellCompiler ? "ghc865"
97+
, compiler-nix-name ? "ghc865"
9898
}:
9999
pkgs.haskell-nix.cabalProject {
100100
src = pkgs.haskell-nix.haskellLib.cleanGit { src = ./.; };
101-
ghc = pkgs.buildPackages.pkgs.haskell-nix.compiler.${haskellCompiler};
101+
inherit compiler-nix-name;
102102
# pkg-def-extras = [
103103
# # Additional packages ontop of all those listed in `cabal.project`
104104
# ];

lib/call-cabal-project-to-nix.nix

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
, materialized ? null # Location of a materialized copy of the nix files
88
, checkMaterialization ? null # If true the nix files will be generated used to check plan-sha256 and material
99
, cabalProject ? null # Cabal project file (when null uses "${src}/cabal.project")
10-
, ghc ? defaults.ghc
10+
, compiler-nix-name ? null # Nix name of the ghc compiler as a string eg. "ghc883"
11+
, ghc ? null # Deprecated in favour of `compiler-nix-name`
12+
, ghcOverride ? null # Used when we need to set ghc explicitly during bootstrapping
1113
, nix-tools ? defaults.nix-tools
1214
, hpack ? defaults.hpack
1315
, cabal-install ? defaults.cabal-install
@@ -31,11 +33,31 @@ assert (if (builtins.compareVersions cabal-install.version "2.4.0.0") < 0
3133
then throw "cabal-install (current version: ${cabal-install.version}) needs to be at least 2.4 for plan-to-nix to work without cabal-to-nix"
3234
else true);
3335

34-
assert (if ghc.isHaskellNixCompiler or false then true
35-
else throw ("It is likely you used `haskell.compiler.X` instead of `haskell-nix.compiler.X`"
36-
+ pkgs.lib.optionalString (name != null) (" for " + name)));
36+
let
37+
forName = pkgs.lib.optionalString (name != null) (" for " + name);
38+
39+
ghc' =
40+
if ghcOverride != null
41+
then ghcOverride
42+
else
43+
if ghc != null
44+
then __trace ("WARNING: A `ghc` argument was passed" + forName
45+
+ " this has been deprecated in favour of `compiler-nix-name`. "
46+
+ "Using `ghc` will break cross compilation setups, as haskell.nix can not"
47+
+ "pick the correct `ghc` package from the respective buildPackages. "
48+
+ "For example use `compiler-nix-name = \"ghc865\";` for ghc 8.6.5") ghc
49+
else
50+
if compiler-nix-name != null
51+
then pkgs.buildPackages.haskell-nix.compiler."${compiler-nix-name}"
52+
else defaults.ghc;
53+
54+
in
55+
assert (if ghc'.isHaskellNixCompiler or false then true
56+
else throw ("It is likely you used `haskell.compiler.X` instead of `haskell-nix.compiler.X`"
57+
+ forName));
3758

3859
let
60+
ghc = ghc';
3961
maybeCleanedSource =
4062
if haskellLib.canCleanSource src
4163
then haskellLib.cleanSourceWith {

overlays/bootstrap.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ in {
384384
# to build ghcs from source.
385385
alex-project = hackage-project {
386386
# Only a boot compiler is suitable here
387-
ghc = ghc // { isHaskellNixCompiler = ghc.isHaskellNixBootCompiler; };
387+
ghcOverride = ghc // { isHaskellNixCompiler = ghc.isHaskellNixBootCompiler; };
388388
inherit (bootstrap.packages) cabal-install nix-tools hpack;
389389
name = "alex"; version = "3.2.4";
390390
index-state = final.haskell-nix.internalHackageIndexState;
@@ -393,7 +393,7 @@ in {
393393
alex = bootstrap.packages.alex-project.hsPkgs.alex.components.exes.alex;
394394
happy-project = hackage-project {
395395
# Only a boot compiler is suitable here
396-
ghc = ghc // { isHaskellNixCompiler = ghc.isHaskellNixBootCompiler; };
396+
ghcOverride = ghc // { isHaskellNixCompiler = ghc.isHaskellNixBootCompiler; };
397397
inherit (bootstrap.packages) cabal-install nix-tools hpack;
398398
name = "happy"; version = "1.19.11";
399399
index-state = final.haskell-nix.internalHackageIndexState;
@@ -402,7 +402,7 @@ in {
402402
happy = bootstrap.packages.happy-project.hsPkgs.happy.components.exes.happy;
403403
hscolour-project = hackage-project {
404404
# Only a boot compiler is suitable here
405-
ghc = ghc // { isHaskellNixCompiler = ghc.isHaskellNixBootCompiler; };
405+
ghcOverride = ghc // { isHaskellNixCompiler = ghc.isHaskellNixBootCompiler; };
406406
inherit (bootstrap.packages) cabal-install nix-tools hpack;
407407
name = "hscolour"; version = "1.24.4";
408408
index-state = final.haskell-nix.internalHackageIndexState;

overlays/ghc-packages.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ in rec {
172172
if __pathExists materializedPath
173173
then materializedPath
174174
else null;
175-
ghc = final.buildPackages.haskell-nix.compiler.${ghcName};
175+
ghcOverride = final.buildPackages.haskell-nix.compiler.${ghcName};
176176
configureArgs = "--disable-tests"; # avoid failures satisfying bytestring package tests dependencies
177177
})
178178
ghc-extra-pkgs-cabal-projects;

overlays/haskell.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,10 @@ final: prev: {
463463
{ plan-pkgs = plan.pkgs;
464464
pkg-def-extras = args.pkg-def-extras or [];
465465
modules = (args.modules or [])
466-
++ final.lib.optional (args ? ghc) { ghc.package = args.ghc; };
466+
++ final.lib.optional (args ? ghcOverride || args ? ghc)
467+
{ ghc.package = args.ghcOverride or args.ghc; }
468+
++ final.lib.optional (args ? compiler-nix-name)
469+
{ compiler.nix-name = args.compiler-nix-name; };
467470
extra-hackages = args.extra-hackages or [];
468471
};
469472
in { inherit (pkg-set.config) hsPkgs; inherit pkg-set; plan-nix = plan.nix; };

test/compiler-nix-name/Main.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE CPP #-}
2+
module Main where
3+
4+
main :: IO ()
5+
main = print __GLASGOW_HASKELL__
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cabal-version: >=1.10
2+
name: compiler-nix-name
3+
version: 0.1.0.0
4+
license: PublicDomain
5+
author: Hamish Mackenzie
6+
build-type: Simple
7+
8+
executable compiler-nix-name
9+
main-is: Main.hs
10+
build-depends: base >=4.13 && <4.14
11+
default-language: Haskell2010

test/compiler-nix-name/default.nix

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{ stdenv, haskell-nix, recurseIntoAttrs, testSrc }:
2+
3+
with stdenv.lib;
4+
5+
let
6+
project = haskell-nix.cabalProject' {
7+
src = testSrc "compiler-nix-name";
8+
compiler-nix-name = "ghc883";
9+
};
10+
11+
packages = project.hsPkgs;
12+
compiler-nix-name =
13+
packages.compiler-nix-name.components.exes.compiler-nix-name;
14+
15+
in recurseIntoAttrs {
16+
ifdInputs = {
17+
inherit (project) plan-nix;
18+
};
19+
20+
run = stdenv.mkDerivation {
21+
name = "compiler-nix-name-test";
22+
23+
buildCommand = ''
24+
exe="${compiler-nix-name}/bin/compiler-nix-name${stdenv.hostPlatform.extensions.executable}"
25+
if [[ "$(${toString compiler-nix-name.config.testWrapper} $exe)" != "808" ]]; then
26+
echo "Unexpected GHC version" >& 2
27+
false
28+
else
29+
touch $out
30+
fi
31+
'';
32+
33+
meta.platforms = platforms.all;
34+
35+
passthru = {
36+
# Used for debugging with nix repl
37+
inherit project packages;
38+
};
39+
};
40+
}

test/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ let
176176
stack-source-repo = callTest ./stack-source-repo {};
177177
lookup-sha256 = callTest ./lookup-sha256 {};
178178
extra-hackage = callTest ./extra-hackage {};
179+
compiler-nix-name = callTest ./compiler-nix-name {};
179180

180181
unit = unitTests;
181182
};

0 commit comments

Comments
 (0)