diff --git a/ChangeLog.md b/ChangeLog.md index d03061acd1..0bc9408a4d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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 diff --git a/src/Stack/Config.hs b/src/Stack/Config.hs index 151da4d682..094875c509 100644 --- a/src/Stack/Config.hs +++ b/src/Stack/Config.hs @@ -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) diff --git a/src/Stack/Config/Nix.hs b/src/Stack/Config/Nix.hs index ebea98dcce..2aa1bbf545 100644 --- a/src/Stack/Config/Nix.hs +++ b/src/Stack/Config/Nix.hs @@ -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 diff --git a/src/Stack/Nix.hs b/src/Stack/Nix.hs index 2cfb185a38..5513140222 100644 --- a/src/Stack/Nix.hs +++ b/src/Stack/Nix.hs @@ -35,6 +35,7 @@ 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) @@ -42,6 +43,7 @@ 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. @@ -49,14 +51,15 @@ import System.Exit (exitSuccess, exitWith) 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 @@ -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 {});" ,"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=''"] diff --git a/src/main/Main.hs b/src/main/Main.hs index 4a440a33be..f56214fde1 100644 --- a/src/main/Main.hs +++ b/src/main/Main.hs @@ -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 @@ -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 @@ -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