git.iko.soy/typeable/stackage2nix
on master help sign in
Add: generate complete build derivation
commit7275fb1fdb410287caac282fc20ef0728699cd6e authorDmitry Bushev <bushevdv@gmail.com> dateMon 29 May 2017, 21:21 (9 years ago) parent0374c6c
6 files changed+94−23
src/Distribution/Nixpkgs/Haskell/Stack/PrettyPrinting.hs+55−12 src/Language/Nix/FilePath.hs+1−1 src/Runner.hs+7−3 src/Runner/Cli.hs+6−6 stack2nix.cabal+4−1 test/Language/Nix/FilePathSpec.hs+21
src/Distribution/Nixpkgs/Haskell/Stack/PrettyPrinting.hs+55−12
@@ -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
src/Language/Nix/FilePath.hs+1−1
@@ -26,7 +26,7 @@ instance NFData FilePath where
2626 rnf = rnf . unFilePath
2727
2828instance Arbitrary FilePath where
29− arbitrary = FilePath <$> arbitrary
29+ arbitrary = FilePath <$> QC.suchThat arbitrary Posix.isValid
3030 shrink = fmap FilePath . shrink . unFilePath
3131
3232instance Text FilePath where
src/Runner.hs+7−3
@@ -3,7 +3,6 @@ module Runner where
33import Control.Lens
44import Distribution.Nixpkgs.Haskell.Stack
55import Distribution.Nixpkgs.Haskell.Stack.PrettyPrinting as PP
6−import Language.Nix.PrettyPrinting as PP
76import Options.Applicative
87import Runner.Cli
98import Stack.Config
@@ -15,7 +14,12 @@ run = do
1514 stackYaml <- either fail pure =<< readStackConfig (opts ^. optStackYaml)
1615 let
1716 pkgConfig = mkPackageConfig (opts ^. optPlatform) (opts ^. optCompilerId)
17+ overrideConfig = OverrideConfig
18+ { _ocGhc = "ghc802"
19+ , _ocStackagePackages = unStackagePackages <$> opts ^. optStackagePackages
20+ , _ocStackageConfig = unStackageConfig <$> opts ^. optStackageConfig }
1821 packages <- traverse (packageDerivation pkgConfig (opts ^. optHackageDb))
1922 $ stackYaml ^. scPackages
20− let out = PP.overrideHaskellPackages (PP.text "ghc802") packages
21− print (opts ^. optStackagePackages, opts ^. optStackageConfig)
23+ let
24+ out = PP.overrideHaskellPackages overrideConfig packages
25+ print out
src/Runner/Cli.hs+6−6
@@ -15,20 +15,20 @@ import Stack.Config
1515import Stack.Types
1616
1717
18−newtype StackagePackages = StackagePackages { fromStackagePackages :: FilePath }
18+newtype StackagePackages = StackagePackages { unStackagePackages :: FilePath }
1919 deriving (Show)
2020
2121makePrisms ''StackagePackages
2222
23−newtype StackageConfig = StackageConfig { fromStackageConfig :: FilePath }
23+newtype StackageConfig = StackageConfig { unStackageConfig :: FilePath }
2424 deriving (Show)
2525
2626makePrisms ''StackageConfig
2727
2828data Options = Options
2929 { _optHackageDb :: Maybe HackageDb
30− , _optStackagePackages :: StackagePackages
31− , _optStackageConfig :: StackageConfig
30+ , _optStackagePackages :: Maybe StackagePackages
31+ , _optStackageConfig :: Maybe StackageConfig
3232 , _optCompilerId :: CompilerId
3333 , _optPlatform :: Platform
3434 , _optStackYaml :: FilePath
@@ -39,8 +39,8 @@ makeLenses ''Options
3939options :: Parser Options
4040options = Options
4141 <$> optional hackageDb
42− <*> stackagePackages
43− <*> stackageConfig
42+ <*> optional stackagePackages
43+ <*> optional stackageConfig
4444 <*> compilerId
4545 <*> platform
4646 <*> stackYamlArg
stack2nix.cabal+4−1
@@ -53,14 +53,17 @@ test-suite spec
5353 hs-source-dirs: test
5454 main-is: Spec.hs
5555 build-depends: base
56+ , Cabal
5657 , bytestring > 0.10 && < 0.11
5758 , hspec
59+ , pretty
5860 , shakespeare
5961 , stack2nix
6062 , text
6163 , yaml
6264 default-extensions: OverloadedStrings
63− other-modules: PrettyPrintingSpec
65+ other-modules: Language.Nix.FilePathSpec
66+ , PrettyPrintingSpec
6467 , Stack.Config.YamlSpec
6568 ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
6669 default-language: Haskell2010
test/Language/Nix/FilePathSpec.hsnew file+21
@@ -0,0 +1,21 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module Language.Nix.FilePathSpec where
4+
5+import Data.String
6+import Distribution.Text
7+import qualified Language.Nix.FilePath as Nix
8+import Test.Hspec
9+import Text.PrettyPrint as PP
10+
11+displayPath :: String -> Doc -> SpecWith ()
12+displayPath path result = specify path
13+ $ disp (fromString path :: Nix.FilePath) `shouldBe` result
14+
15+spec :: Spec
16+spec = describe "valid FilePath" $ do
17+ displayPath "foo" "./foo"
18+ displayPath "./foo" "./foo"
19+ displayPath "../foo" "./../foo"
20+ displayPath "/foo" "/foo"
21+ displayPath "." "./."