Skip to content

Commit 3a9932c

Browse files
committed
Nix: support passing ghcVersion to shell.nix
This is handy for two reasons: - ghc currently passed relies on importing nixpkgs from $NIX_PATH that is against best practices - one might want to refer to a compiler outside nixpkgs, for example haskell.nix
1 parent 30224e3 commit 3a9932c

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Behavior changes:
2323

2424
Other enhancements:
2525

26+
* Nix integration now passes `ghcVersion` (in addition to existing `ghc`) to
27+
`shell-file` as an identifier that can be looked up in a compiler attribute set.
28+
2629
* `stack list` is a new command to list package versions in a snapshot.
2730
See [#5431](https://github.com/commercialhaskell/stack/pull/5431)
2831

src/Stack/Config/Nix.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module Stack.Config.Nix
66
(nixOptsFromMonoid
77
,nixCompiler
8+
,nixCompilerVersion
89
,StackNixException(..)
910
) where
1011

@@ -77,6 +78,16 @@ nixCompiler compilerVersion =
7778
WCGhcjs{} -> Left $ stringException "Only GHC is supported by stack --nix"
7879
WCGhcGit{} -> Left $ stringException "Only GHC is supported by stack --nix"
7980

81+
nixCompilerVersion :: WantedCompiler -> Either StringException T.Text
82+
nixCompilerVersion compilerVersion =
83+
case compilerVersion of
84+
WCGhc version ->
85+
case T.split (== '.') (fromString $ versionString version) of
86+
x : y : minor -> Right $ "ghc" <> T.concat (x : y : minor)
87+
_ -> Left $ stringException "GHC major version not specified"
88+
WCGhcjs{} -> Left $ stringException "Only GHC is supported by stack --nix"
89+
WCGhcGit{} -> Left $ stringException "Only GHC is supported by stack --nix"
90+
8091
-- Exceptions thown specifically by Stack.Nix
8192
data StackNixException
8293
= NixCannotUseShellFileAndPackagesException

src/Stack/Nix.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Data.Version (showVersion)
1818
import Path.IO
1919
import qualified Paths_stack as Meta
2020
import Stack.Config (getInContainer, withBuildConfig)
21-
import Stack.Config.Nix (nixCompiler)
21+
import Stack.Config.Nix (nixCompiler, nixCompilerVersion)
2222
import Stack.Constants (platformVariantEnvVar,inNixShellEnvVar,inContainerEnvVar)
2323
import Stack.Types.Config
2424
import Stack.Types.Docker
@@ -56,14 +56,16 @@ runShellAndExit = do
5656
compilerVersion <- withBuildConfig $ view wantedCompilerVersionL
5757

5858
ghc <- either throwIO return $ nixCompiler compilerVersion
59+
ghcVersion <- either throwIO return $ nixCompilerVersion compilerVersion
5960
let pkgsInConfig = nixPackages (configNix config)
6061
pkgs = pkgsInConfig ++ [ghc, "git", "gcc", "gmp"]
6162
pkgsStr = "[" <> T.intercalate " " pkgs <> "]"
6263
pureShell = nixPureShell (configNix config)
6364
addGCRoots = nixAddGCRoots (configNix config)
6465
nixopts = case mshellFile of
65-
Just fp -> [toFilePath fp, "--arg", "ghc"
66-
,"with (import <nixpkgs> {}); " ++ T.unpack ghc]
66+
Just fp -> [toFilePath fp
67+
,"--arg", "ghc", "with (import <nixpkgs> {}); " ++ T.unpack ghc
68+
,"--argstr", "ghcVersion", T.unpack ghcVersion]
6769
Nothing -> ["-E", T.unpack $ T.concat
6870
["with (import <nixpkgs> {}); "
6971
,"let inputs = ",pkgsStr,"; "

0 commit comments

Comments
 (0)