@@ -12,13 +12,13 @@ Nix configuration.
1212-}
1313
1414module Stack.Config.Nix
15- ( ConfigNixException
15+ ( ConfigNixPrettyException
1616 , nixCompiler
1717 , nixCompilerVersion
1818 , nixOptsFromMonoid
1919 ) where
2020
21- import Control.Monad.Extra ( ifM )
21+ import Control.Monad.Extra ( ifM , whenJust )
2222import qualified Data.Text as T
2323import qualified Data.Text.IO as TIO
2424import Distribution.System ( OS (.. ) )
@@ -28,26 +28,47 @@ import Stack.Types.Runner ( HasRunner )
2828import Stack.Types.Nix ( NixOpts (.. ), NixOptsMonoid (.. ) )
2929import System.Directory ( doesFileExist )
3030
31- -- | Type representing exceptions thrown by functions exported by the
31+ -- | Type representing \'pretty\' exceptions thrown by functions exported by the
3232-- "Stack.Config.Nix" module.
33- data ConfigNixException
34- = NixCannotUseShellFileAndPackagesException
33+ data ConfigNixPrettyException
34+ = NixCannotUseShellFileAndPackagesException ! FilePath ! [ Text ]
3535 -- ^ Nix can't be given packages and a shell file at the same time
3636 | GHCMajorVersionUnspecified
3737 | OnlyGHCSupported
3838 deriving Show
3939
40- instance Exception ConfigNixException where
41- displayException NixCannotUseShellFileAndPackagesException =
42- " Error: [S-2726]\n "
43- ++ " You cannot have packages and a shell-file filled at the same time \
44- \in your nix-shell configuration."
45- displayException GHCMajorVersionUnspecified =
46- " Error: [S-9317]\n "
47- ++ " GHC major version not specified."
48- displayException OnlyGHCSupported =
49- " Error: [S-8605]\n "
50- ++ " Only GHC is supported by 'stack --nix'."
40+ instance Pretty ConfigNixPrettyException where
41+ pretty (NixCannotUseShellFileAndPackagesException initFile packages) =
42+ " [S-2726]"
43+ <> line
44+ <> flow " The configuration of Stack's Nix integration cannot specify both \
45+ \a Nix shell file and Nix packages. You have specified:"
46+ <> blankLine
47+ <> spacedBulletedList
48+ [ fillSep
49+ [ flow " Shell file:"
50+ , style File (fromString initFile) <> " ;"
51+ , " and"
52+ ]
53+ , fillSep $
54+ flow " Nix packages:"
55+ : mkNarrativeList (Just Shell ) False prettyPackages
56+ ]
57+ where
58+ prettyPackages :: [StyleDoc ]
59+ prettyPackages = map (fromString . T. unpack) packages
60+ pretty GHCMajorVersionUnspecified =
61+ " [S-9317]"
62+ <> line
63+ <> flow " Stack's Nix integration requires at least a major version of GHC \
64+ \to be specified. No major version is specified."
65+ pretty OnlyGHCSupported =
66+ " [S-8605]"
67+ <> line
68+ <> flow " Stack's Nix integration supports only GHC binary distributions as \
69+ \compiler."
70+
71+ instance Exception ConfigNixPrettyException
5172
5273-- | Interprets NixOptsMonoid options.
5374nixOptsFromMonoid ::
@@ -79,8 +100,9 @@ nixOptsFromMonoid nixMonoid os = do
79100 pure False
80101 else pure nixEnable0
81102
82- when (not (null packages) && isJust initFile) $
83- throwIO NixCannotUseShellFileAndPackagesException
103+ unless (null packages) $ whenJust initFile $ \ fp ->
104+ prettyThrowIO $ NixCannotUseShellFileAndPackagesException fp packages
105+
84106 pure NixOpts
85107 { enable
86108 , pureShell
@@ -93,7 +115,7 @@ nixOptsFromMonoid nixMonoid os = do
93115 prefixAll p (x: xs) = p : x : prefixAll p xs
94116 prefixAll _ _ = []
95117
96- nixCompiler :: WantedCompiler -> Either ConfigNixException T. Text
118+ nixCompiler :: WantedCompiler -> Either ConfigNixPrettyException T. Text
97119nixCompiler compilerVersion =
98120 case compilerVersion of
99121 WCGhc version ->
@@ -119,7 +141,7 @@ nixCompiler compilerVersion =
119141 WCGhcjs {} -> Left OnlyGHCSupported
120142 WCGhcGit {} -> Left OnlyGHCSupported
121143
122- nixCompilerVersion :: WantedCompiler -> Either ConfigNixException T. Text
144+ nixCompilerVersion :: WantedCompiler -> Either ConfigNixPrettyException T. Text
123145nixCompilerVersion compilerVersion =
124146 case compilerVersion of
125147 WCGhc version ->
0 commit comments