Skip to content

Commit

Permalink
Nix: Select the correct GHC version in more cases.
Browse files Browse the repository at this point in the history
Even when abstract resolver is provided.

Fixes #1641.
  • Loading branch information
mboes committed Jan 11, 2016
1 parent 67f0f03 commit 12e9bc1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Bug fixes:
[Mailing list discussion](https://groups.google.com/d/msg/haskell-stack/iVGDG5OHYxs/FjUrR5JsDQAJ)
- Gracefully handle invalid paths in error/warning messages
[#1561](https://github.com/commercialhaskell/stack/issues/1561)
- Nix: select the correct GHC version corresponding to the snapshot
even when an abstract resolver is passed via `--resolver` on the
command-line.
[#1641](https://github.com/commercialhaskell/stack/issues/1641)

## 1.0.0

Expand Down
2 changes: 1 addition & 1 deletion src/Stack/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ configFromConfigMonoid configStackRoot configUserConfigPath mresolver mproject c

configDocker <-
dockerOptsFromMonoid (fmap fst mproject) configStackRoot mresolver configMonoidDockerOpts
configNix <- nixOptsFromMonoid (fmap fst mproject) mresolver configMonoidNixOpts os
configNix <- nixOptsFromMonoid configMonoidNixOpts os

rawEnv <- liftIO getEnvironment
pathsEnv <- augmentPathMap (map toFilePath configMonoidExtraPath)
Expand Down
20 changes: 4 additions & 16 deletions src/Stack/Config/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,20 @@ import Control.Monad.Catch (throwM,MonadCatch)
-- | Interprets NixOptsMonoid options.
nixOptsFromMonoid
:: (Monad m, MonadCatch m)
=> Maybe Project
-> Maybe AbstractResolver
-> NixOptsMonoid
=> NixOptsMonoid
-> OS
-> m NixOpts
nixOptsFromMonoid mproject maresolver NixOptsMonoid{..} os = do
nixOptsFromMonoid NixOptsMonoid{..} os = do
let nixEnable = fromMaybe nixMonoidDefaultEnable nixMonoidEnable
defaultPure = case os of
OSX -> False
_ -> True
nixPureShell = fromMaybe defaultPure nixMonoidPureShell
mresolver = case maresolver of
Just (ARResolver resolver) -> Just resolver
Just _ -> Nothing
Nothing -> fmap projectResolver mproject
pkgs = fromMaybe [] nixMonoidPackages
nixPackages = case mproject of
Nothing -> pkgs
Just _ -> pkgs ++ [case mresolver of
Just (ResolverSnapshot (LTS x y)) ->
T.pack ("haskell.packages.lts-" ++ show x ++ "_" ++ show y ++ ".ghc")
_ -> T.pack "ghc"]
nixPackages = fromMaybe [] nixMonoidPackages
nixInitFile = nixMonoidInitFile
nixShellOptions = fromMaybe [] nixMonoidShellOptions
++ prefixAll (T.pack "-I") (fromMaybe [] nixMonoidPath)
when (not (null pkgs) && isJust nixInitFile) $
when (not (null nixPackages) && isJust nixInitFile) $
throwM NixCannotUseShellFileAndPackagesException
return NixOpts{..}
where prefixAll p (x:xs) = p : x : prefixAll p xs
Expand Down
18 changes: 14 additions & 4 deletions src/Stack/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,31 @@ import Path.IO
import qualified Paths_stack as Meta
import Prelude -- Fix redundant import warnings
import Stack.Constants (stackProgName,platformVariantEnvVar)
import Stack.Config (makeConcreteResolver)
import Stack.Docker (reExecArgName)
import Stack.Exec (exec)
import System.Process.Read (getEnvOverride)
import Stack.Types
import Stack.Types.Internal
import System.Environment (lookupEnv,getArgs,getExecutablePath)
import System.Exit (exitSuccess, exitWith)
import Prelude hiding (mapM)


-- | If Nix is enabled, re-runs the currently running OS command in a Nix container.
-- Otherwise, runs the inner action.
reexecWithOptionalShell
:: M env m
=> Maybe (Path Abs Dir)
-> Maybe AbstractResolver
-> IO ()
-> m ()
reexecWithOptionalShell mprojectRoot inner =
reexecWithOptionalShell mprojectRoot maresolver inner =
do config <- asks getConfig
inShell <- getInShell
isReExec <- asks getReExec
if nixEnable (configNix config) && not inShell && not isReExec
then runShellAndExit mprojectRoot getCmdArgs
then runShellAndExit mprojectRoot maresolver getCmdArgs
else liftIO inner
where
getCmdArgs = do
Expand All @@ -70,23 +73,30 @@ reexecWithOptionalShell mprojectRoot inner =
runShellAndExit
:: M env m
=> Maybe (Path Abs Dir)
-> Maybe AbstractResolver
-> m (String, [String])
-> m ()
runShellAndExit mprojectRoot getCmdArgs = do
runShellAndExit mprojectRoot maresolver getCmdArgs = do
config <- asks getConfig
mresolver <- mapM makeConcreteResolver maresolver
envOverride <- getEnvOverride (configPlatform config)
(cmnd,args) <- fmap (escape *** map escape) getCmdArgs
mshellFile <-
traverse (resolveFile (fromMaybeProjectRoot mprojectRoot)) $
nixInitFile (configNix config)
let pkgsInConfig = nixPackages (configNix config)
pkgs =
pkgsInConfig ++ [case mresolver of
Just (ResolverSnapshot (LTS x y)) ->
T.pack ("haskell.packages.lts-" ++ show x ++ "_" ++ show y ++ ".ghc")
_ -> T.pack "ghc"]
pureShell = nixPureShell (configNix config)
nixopts = case mshellFile of
Just fp -> [toFilePath fp]
Nothing -> ["-E", T.unpack $ T.intercalate " " $ concat
[["with (import <nixpkgs> {});"
,"runCommand \"myEnv\" {"
,"buildInputs=lib.optional stdenv.isLinux glibcLocales ++ ["],pkgsInConfig,["];"
,"buildInputs=lib.optional stdenv.isLinux glibcLocales ++ ["],pkgs,["];"
,T.pack platformVariantEnvVar <> "=''nix'';"
,T.pack inShellEnvVar <> "=1;"
,"STACK_IN_NIX_EXTRA_ARGS=''"]
Expand Down
5 changes: 3 additions & 2 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ setupCmd SetupCmdOpts{..} go@GlobalOpts{..} = do
(lcProjectRoot lc)
Nothing
(runStackTGlobal manager (lcConfig lc) go $
Nix.reexecWithOptionalShell (lcProjectRoot lc) $
Nix.reexecWithOptionalShell (lcProjectRoot lc) globalResolver $
runStackLoggingTGlobal manager go $ do
(wantedCompiler, compilerCheck, mstack) <-
case scoCompilerVersion of
Expand Down Expand Up @@ -864,7 +864,7 @@ withBuildConfigExt go@GlobalOpts{..} mbefore inner mafter = do
(lcProjectRoot lc)
mbefore
(runStackTGlobal manager (lcConfig lc) go $
Nix.reexecWithOptionalShell (lcProjectRoot lc) (inner'' lk0))
Nix.reexecWithOptionalShell (lcProjectRoot lc) globalResolver (inner'' lk0))
mafter
(Just $ liftIO $
do lk' <- readIORef curLk
Expand Down Expand Up @@ -1007,6 +1007,7 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
menv <- liftIO $ configEnvOverride config plainEnvSettings
Nix.reexecWithOptionalShell
(lcProjectRoot lc)
globalResolver
(runStackTGlobal manager (lcConfig lc) go $
exec menv cmd args))
Nothing
Expand Down

0 comments on commit 12e9bc1

Please sign in to comment.