Skip to content

Commit 6176adb

Browse files
committed
Re #6861 Prettify existing Nix integration exceptions
1 parent 70ad620 commit 6176adb

3 files changed

Lines changed: 57 additions & 32 deletions

File tree

doc/maintainers/stack_errors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
In connection with considering Stack's support of the
66
[Haskell Error Index](https://errors.haskell.org/) initiative, this page seeks
77
to take stock of the errors that Stack itself can raise, by reference to the
8-
`master` branch of the Stack repository. Last updated: 2025-08-16.
8+
`master` branch of the Stack repository. Last updated: 2026-03-15.
99

1010
* `Stack.main`: catches exceptions from action `commandLineHandler`.
1111

@@ -78,7 +78,7 @@ to take stock of the errors that Stack itself can raise, by reference to the
7878
[S-8575] = SnapshotNotSupportedException (Maybe Project) (Maybe AbstractSnapshot)
7979
~~~
8080

81-
- `Stack.Config.Nix.ConfigNixException`
81+
- `Stack.Config.Nix.ConfigNixPrettyException`
8282

8383
~~~haskell
8484
[S-2726] = NixCannotUseShellFileAndPackagesException
@@ -208,7 +208,7 @@ to take stock of the errors that Stack itself can raise, by reference to the
208208
[S-3113] | AttemptedOverwrites [Path Abs File]
209209
~~~
210210

211-
- `Stack.Nix.NixException`
211+
- `Stack.Nix.NixPrettyException`
212212

213213
~~~haskell
214214
[S-7384] = CannotDetermineProjectRoot

src/Stack/Config/Nix.hs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ Nix configuration.
1212
-}
1313

1414
module 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 )
2222
import qualified Data.Text as T
2323
import qualified Data.Text.IO as TIO
2424
import Distribution.System ( OS (..) )
@@ -28,26 +28,47 @@ import Stack.Types.Runner ( HasRunner )
2828
import Stack.Types.Nix ( NixOpts (..), NixOptsMonoid (..) )
2929
import 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.
5374
nixOptsFromMonoid ::
@@ -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
97119
nixCompiler 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
123145
nixCompilerVersion compilerVersion =
124146
case compilerVersion of
125147
WCGhc version ->

src/Stack/Nix.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@ import Stack.Types.Version ( showStackVersion )
3636
import System.Environment ( getArgs, lookupEnv )
3737
import qualified System.FilePath as F
3838

39-
-- | Type representing exceptions thrown by functions exported by the
40-
-- "Stack.Nix" module.
41-
data NixException
39+
-- | Type representing \'pretty\' exceptions thrown by functions exported by
40+
-- the "Stack.Nix" module.
41+
data NixPrettyException
4242
= CannotDetermineProjectRoot
4343
-- ^ Can't determine the project root (location of the shell file if any).
4444
deriving Show
4545

46-
instance Exception NixException where
47-
displayException CannotDetermineProjectRoot =
48-
"Error: [S-7384]\n"
49-
++ "Cannot determine project root directory."
46+
instance Pretty NixPrettyException where
47+
pretty CannotDetermineProjectRoot =
48+
"[S-7384]"
49+
<> line
50+
<> flow "Cannot determine project root directory."
51+
52+
instance Exception NixPrettyException
5053

5154
-- | Execute @nix-shell@, replacing the current process.
5255
runShellAndExit :: RIO Config void
@@ -77,8 +80,8 @@ runShellAndExit = do
7780
-- (thus the void return type)
7881
compilerVersion <- withBuildConfig $ view wantedCompilerVersionL
7982

80-
ghc <- either throwIO pure $ nixCompiler compilerVersion
81-
ghcVersion <- either throwIO pure $ nixCompilerVersion compilerVersion
83+
ghc <- either prettyThrowIO pure $ nixCompiler compilerVersion
84+
ghcVersion <- either prettyThrowIO pure $ nixCompilerVersion compilerVersion
8285
let pkgsInConfig = config.nix.packages
8386
-- It appears that cacert needs to be specified in order for
8487
-- crypton-x509-system >= 1.6.8 to work with Stack's Nix integration:

0 commit comments

Comments
 (0)