@@ -1,14 +1,29 @@
11{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE RankNTypes #-}
3+{-# LANGUAGE TemplateHaskell #-}
24
35module Distribution.Nixpkgs.Haskell.Stack.PrettyPrinting where
46
5−import Control.Lens
6−import Distribution.Package
7−import Distribution.Text
8−import Distribution.Nixpkgs.Haskell.Derivation
9−import Distribution.Nixpkgs.Haskell.Packages.PrettyPrinting as PP
10−import Language.Nix.PrettyPrinting as PP
7+import Control.Lens
8+import Data.Maybe
9+import Data.String
10+import Distribution.Nixpkgs.Haskell.Derivation
11+import Distribution.Nixpkgs.Haskell.Packages.PrettyPrinting as PP
12+import Distribution.Package
13+import Distribution.Text
14+import qualified Language.Nix.FilePath as Nix
15+import Language.Nix.PrettyPrinting as PP
1116
17+data OverrideConfig = OverrideConfig
18+ { _ocGhc :: String
19+ , _ocStackagePackages :: Maybe FilePath
20+ , _ocStackageConfig :: Maybe FilePath
21+ }
22+
23+makeLenses ''OverrideConfig
24+
25+hasField :: Lens' OverrideConfig (Maybe a) -> OverrideConfig -> Bool
26+hasField p = views p isJust
1227
1328overridePackages :: [Derivation] -> Doc
1429overridePackages deps = PP.packageSetConfig . PP.cat $ callPackage <$> deps
@@ -18,24 +33,52 @@ overridePackages deps = PP.packageSetConfig . PP.cat $ callPackage <$> deps
1833 (drvNameString drv <> " = callPackage") 2
1934 (PP.parens (PP.pPrint drv) <+> "{};")
2035
21−overrideHaskellPackages :: Doc -> [Derivation] -> Doc
22−overrideHaskellPackages ghc packages = vcat
36+importStackagePackages :: FilePath -> Doc
37+importStackagePackages path = hsep
38+ [ funarg "self", "import", disp (fromString path :: Nix.FilePath), "{"
39+ , "inherit pkgs stdenv;"
40+ , "inherit (self) callPackage;"
41+ , "}"
42+ ]
43+
44+importStackageConfig :: FilePath -> Doc
45+importStackageConfig path = hsep
46+ [ "import", disp (fromString path :: Nix.FilePath), "{"
47+ , "inherit pkgs;"
48+ , "}"
49+ ]
50+
51+callStackageConfig :: FilePath -> Doc
52+callStackageConfig path = hsep
53+ [ "callPackage", disp (fromString path :: Nix.FilePath), "{}"]
54+
55+overrideHaskellPackages :: OverrideConfig -> [Derivation] -> Doc
56+overrideHaskellPackages oc packages = vcat
2357 [ funargs ["nixpkgs ? import <nixpkgs> {}"]
2458 , ""
2559 , "with nixpkgs;"
2660 , "let"
61+ , nest 2 "inherit (stdenv.lib) extends;"
62+ , nest 2 . vcat $ catMaybes
63+ [ attr "stackagePackages" . importStackagePackages <$> oc ^. ocStackagePackages
64+ , attr "stackageConfig" . callStackageConfig <$> oc ^. ocStackageConfig ]
2765 , nest 2 $ vcat
28− [ "packageSetConfig ="
66+ [ "stackPackages ="
2967 , nest 2 $ overridePackages packages <> semi
3068 , ""
31− , "pkgOverrides = self: packageSetConfig {"
69+ , "pkgOverrides = self: stackPackages {"
3270 , nest 2 "inherit pkgs stdenv;"
3371 , nest 2 "inherit (self) callPackage;"
3472 , "};"
3573 , ""
3674 ]
37− , "in pkgs.haskell.packages." <> ghc <> ".override {"
75+ , "in callPackage <nixpkgs/pkgs/development/haskell-modules> {"
3876 , nest 2 $ vcat
39− [ "overrides = self: pkgOverrides self;"]
77+ [ attr "ghc" ("pkgs.haskell.compiler." <> ghc)
78+ , onlyIf (hasField ocStackageConfig oc && hasField ocStackagePackages oc)
79+ $ attr "compilerConfig" "self: extends stackageConfig (stackagePackages self)"
80+ , attr "overrides" "self: pkgOverrides self"]
4081 , "}"
4182 ]
83+ where
84+ ghc = oc ^. ocGhc . to text