git.iko.soy/typeable/stackage2nix
on master help sign in
Upd make all-cabal-hashes and lts-haskell optional
commit6d71d97c3d0242f86b64b0799d3a155396bf203d authorDmitry Bushev <bushevdv@gmail.com> dateThu 4 Jan 2018, 09:22 (9 years ago) parent40216b8
2 files changed+102−60
src/Runner.hs+67−44 src/Runner/Cli.hs+35−16
src/Runner.hs+67−44
@@ -3,6 +3,8 @@ module Runner ( run ) where
33import Control.Lens
44import Data.Foldable as F
55import Data.List as L
6+import Data.Maybe (fromMaybe)
7+import Data.Semigroup ((<>))
68import Distribution.Nixpkgs.Haskell.FromStack
79import Distribution.Nixpkgs.Haskell.FromStack.Package
810import Distribution.Nixpkgs.Haskell.Stack
@@ -25,44 +27,43 @@ import qualified Data.Set as Set
2527import qualified Data.Map as Map
2628import qualified LtsHaskell as LH
2729
28−
2930run :: IO ()
3031run = do
3132 opts <- execParser pinfo
33+ let
34+ -- With optparse-applicative it seems there's no way to make
35+ -- 'StackageOptions' optional for stackage override and required in
36+ -- other cases
37+ missingErr = error . ("Missing required parameter: " <>)
38+ sopts = StackageOptions
39+ (fromMaybe (missingErr "--all-cabal-hashes") (opts ^. optAllCabalHashesRepo) )
40+ (fromMaybe (missingErr "--lts-haskell") (opts ^. optLtsHaskellRepo))
41+
3242 case opts ^. optConfigOrigin of
33− -- Generate build derivation from stack.yaml file
34− OriginStackYaml stackYaml -> do
43+ -- Generate Stackage packages from stack.yaml resolver
44+ OriginStackYaml stackYaml buildTarget -> do
3545 stackConf <- either fail pure =<< readStackConfig stackYaml
3646 let
37− generateStackage = opts ^. optWithStackage || opts ^. optWithStackageClosure
47+ stackResolver = stackConf ^. scResolver
3848 stackPackagesConfig = mkStackPackagesConfig opts
39− stackConfPackages <- traverse (packageDerivation stackPackagesConfig (opts ^. optHackageDb)) $ stackConf ^. scPackages
49+ stackConfPackages <- traverse (packageDerivation stackPackagesConfig (opts ^. optHackageDb))
50+ $ stackConf ^. scPackages
51+ let
52+ reachable = Set.map mkPackageName
53+ $ F.foldr1 Set.union
54+ $ nodeDepends . mkNode <$> stackConfPackages
4055
41− case generateStackage of
42− -- Generate Stackage packages from stack.yaml resolver
43− True -> do
44− let buildPlanFile = LH.buildPlanFilePath (opts ^. optLtsHaskellRepo) (stackConf ^. scResolver)
45− buildPlan <- LH.loadBuildPlan buildPlanFile
46− packageSetConfig <- LH.buildPackageSetConfig
47− (opts ^. optHackageDb)
48− (opts ^. optAllCabalHashesRepo)
49− (opts ^. optNixpkgsRepository)
50− buildPlan
56+ case buildTarget of
57+ TargetStackageClosure -> do
58+ (buildPlanFile, buildPlan, packageSetConfig) <- loadStackageBuildPlan stackResolver opts sopts
5159 let
5260 overrideConfig = mkOverrideConfig opts (siGhcVersion $ bpSystemInfo buildPlan)
53− reachable = Set.map mkPackageName
54− $ F.foldr1 Set.union
55− $ nodeDepends . mkNode <$> stackConfPackages
56− s2nLoader mHash pkgId =
61+ stackageLoader mHash pkgId =
5762 if pkgName pkgId `Set.member` reachable
5863 then packageLoader packageSetConfig Nothing pkgId
5964 else packageLoader packageSetConfig mHash pkgId
60− s2nPackageSetConfig = packageSetConfig { packageLoader = s2nLoader }
61− s2nPackageConfig = PackageConfig
62− { enableCheck = True
63− , enableHaddock = True }
64− stackagePackages <- traverse (uncurry (buildNode s2nPackageSetConfig s2nPackageConfig))
65− $ Map.toAscList (bpPackages buildPlan)
65+ stackageSetConfig = packageSetConfig { packageLoader = stackageLoader }
66+ stackagePackages <- buildStackagePackages stackageSetConfig buildPlan
6667 let
6768 -- Nixpkgs generic-builder puts hscolour on path for all libraries
6869 withHscolour pkgs =
@@ -75,13 +76,11 @@ run = do
7576 -- does: pruning only after generating full set of packages allows
7677 -- us to make sure all those extra dependencies are explicitly
7778 -- listed as well.
78− nodes = case opts ^. optWithStackageClosure of
79− True -> Set.toAscList
79+ nodes = Set.toAscList
8080 $ withHscolour
8181 $ flip reachableDependencies stackagePackages
8282 -- Originally reachable nodes are root nodes
8383 $ L.filter (\n -> mkPackageName (nodeName n) `Set.member` reachable) stackagePackages
84− False -> stackagePackages
8584 writeOutFile buildPlanFile (opts ^. optOutStackagePackages)
8685 $ pPrintOutPackages (view nodeDerivation <$> nodes)
8786 writeOutFile buildPlanFile (opts ^. optOutStackageConfig)
@@ -89,28 +88,32 @@ run = do
8988 writeOutFile (stackYaml ^. syFilePath) (opts ^. optOutDerivation)
9089 $ PP.overrideHaskellPackages overrideConfig stackConfPackages
9190
92− -- Generate Stackage override only
93− False ->
91+ TargetStackagePackages -> do
92+ (buildPlanFile, buildPlan, packageSetConfig) <- loadStackageBuildPlan stackResolver opts sopts
93+ let
94+ overrideConfig = mkOverrideConfig opts (siGhcVersion $ bpSystemInfo buildPlan)
95+ stackageLoader mHash pkgId =
96+ if pkgName pkgId `Set.member` reachable
97+ then packageLoader packageSetConfig Nothing pkgId
98+ else packageLoader packageSetConfig mHash pkgId
99+ stackageSetConfig = packageSetConfig { packageLoader = stackageLoader }
100+ nodes <- buildStackagePackages stackageSetConfig buildPlan
101+ writeOutFile buildPlanFile (opts ^. optOutStackagePackages)
102+ $ pPrintOutPackages (view nodeDerivation <$> nodes)
103+ writeOutFile buildPlanFile (opts ^. optOutStackageConfig)
104+ $ pPrintOutConfig (bpSystemInfo buildPlan) nodes
105+ writeOutFile (stackYaml ^. syFilePath) (opts ^. optOutDerivation)
106+ $ PP.overrideHaskellPackages overrideConfig stackConfPackages
107+
108+ TargetStackageOverride ->
94109 writeOutFile (stackYaml ^. syFilePath) (opts ^. optOutDerivation)
95110 $ PP.overrideStackage (stackConf ^. scResolver) (opts ^. optNixpkgsRepository) stackConfPackages
96111
97112 -- Generate Stackage packages from resolver
98113 OriginResolver stackResolver -> do
99− let
100− buildPlanFile = LH.buildPlanFilePath (opts ^. optLtsHaskellRepo) stackResolver
101− packageConfig = PackageConfig
102− { enableCheck = True
103− , enableHaddock = True }
104− buildPlan <- LH.loadBuildPlan buildPlanFile
105− packageSetConfig <- LH.buildPackageSetConfig
106− (opts ^. optHackageDb)
107− (opts ^. optAllCabalHashesRepo)
108− (opts ^. optNixpkgsRepository)
109− buildPlan
110− nodes <- traverse (uncurry (buildNode packageSetConfig packageConfig))
111− $ Map.toAscList (bpPackages buildPlan)
114+ (buildPlanFile, buildPlan, packageSetConfig) <- loadStackageBuildPlan stackResolver opts sopts
115+ nodes <- buildStackagePackages packageSetConfig buildPlan
112116 let overrideConfig = mkOverrideConfig opts (siGhcVersion $ bpSystemInfo buildPlan)
113−
114117 writeOutFile buildPlanFile (opts ^. optOutStackagePackages)
115118 $ pPrintOutPackages (view nodeDerivation <$> nodes)
116119 writeOutFile buildPlanFile (opts ^. optOutStackageConfig)
@@ -118,6 +121,26 @@ run = do
118121 writeOutFile buildPlanFile (opts ^. optOutDerivation)
119122 $ PP.pPrintHaskellPackages overrideConfig
120123
124+loadStackageBuildPlan :: StackResolver -> Options -> StackageOptions -> IO (FilePath, BuildPlan, PackageSetConfig)
125+loadStackageBuildPlan stackResolver opts sopts = do
126+ let buildPlanFile = LH.buildPlanFilePath (sopts ^. soptLtsHaskellRepo) stackResolver
127+ buildPlan <- LH.loadBuildPlan buildPlanFile
128+ packageSetConfig <- LH.buildPackageSetConfig
129+ (opts ^. optHackageDb)
130+ (sopts ^. soptAllCabalHashesRepo)
131+ (opts ^. optNixpkgsRepository)
132+ buildPlan
133+ return (buildPlanFile, buildPlan, packageSetConfig)
134+
135+buildStackagePackages :: PackageSetConfig -> BuildPlan -> IO [Node]
136+buildStackagePackages packageSetConfig buildPlan = do
137+ let
138+ stackagePackageConfig = PackageConfig
139+ { enableCheck = True
140+ , enableHaddock = True }
141+ traverse (uncurry (buildNode packageSetConfig stackagePackageConfig))
142+ $ Map.toAscList (bpPackages buildPlan)
143+
121144writeOutFile :: Show source => source -> FilePath -> Doc -> IO ()
122145writeOutFile source filePath contents =
123146 withFile filePath WriteMode $ \h -> do
src/Runner/Cli.hs+35−16
@@ -15,26 +15,40 @@ import Stack.Types
1515import System.Environment
1616import System.FilePath
1717
18+-- | What will be generated
19+data Target
20+ = TargetStackagePackages
21+ | TargetStackageClosure
22+ | TargetStackageOverride
23+ deriving (Show)
24+
25+makePrisms ''Target
26+
1827-- | Source that would be used to produce target derivation
1928data ConfigOrigin
2029 = OriginResolver StackResolver
2130 -- ^ Resolver to generate Stackage LTS
22− | OriginStackYaml StackYaml
31+ | OriginStackYaml StackYaml Target
2332 -- ^ Stack config to generate build derivation
2433 deriving (Show)
2534
2635makePrisms ''ConfigOrigin
2736
37+data StackageOptions = StackageOptions
38+ { _soptAllCabalHashesRepo :: FilePath
39+ , _soptLtsHaskellRepo :: FilePath
40+ } deriving (Show)
41+
42+makeLenses ''StackageOptions
43+
2844data Options = Options
29− { _optAllCabalHashesRepo :: FilePath
30− , _optLtsHaskellRepo :: FilePath
45+ { _optAllCabalHashesRepo :: Maybe FilePath
46+ , _optLtsHaskellRepo :: Maybe FilePath
3147 , _optOutStackagePackages :: FilePath
3248 , _optOutStackageConfig :: FilePath
3349 , _optOutDerivation :: FilePath
3450 , _optDoCheckPackages :: Bool
3551 , _optDoHaddockPackages :: Bool
36− , _optWithStackage :: Bool
37− , _optWithStackageClosure :: Bool
3852 , _optHackageDb :: Maybe HackageDb
3953 , _optNixpkgsRepository :: FilePath
4054 , _optCompilerId :: CompilerId
@@ -56,15 +70,13 @@ mkStackYaml p = case splitFileName p of
5670
5771options :: Parser Options
5872options = Options
59− <$> allCabalHashesRepo
60− <*> ltsHaskellRepo
73+ <$> optional allCabalHashesRepo
74+ <*> optional ltsHaskellRepo
6175 <*> outStackagePackages
6276 <*> outStackageConfig
6377 <*> outDerivation
6478 <*> doCheckPackages
6579 <*> doHaddockPackages
66− <*> withStackage
67− <*> withStackageClosure
6880 <*> optional hackageDb
6981 <*> nixpkgsRepository
7082 <*> compilerId
@@ -133,13 +145,13 @@ doHaddockPackages = flag True False
133145 ( long "no-haddock"
134146 <> help "disable haddock for project packages")
135147
136−withStackage :: Parser Bool
137−withStackage = switch
148+withStackage :: Parser ()
149+withStackage = flag' ()
138150 ( long "with-stackage"
139151 <> help "generate full Stackage" )
140152
141−withStackageClosure :: Parser Bool
142−withStackageClosure = switch
153+withStackageClosure :: Parser ()
154+withStackageClosure = flag' ()
143155 ( long "with-stackage-closure"
144156 <> help "generate Stackage subset containing only build dependencies" )
145157
@@ -153,11 +165,18 @@ stackYamlArg = mkStackYaml <$> Opts.argument str
153165 ( metavar "STACK_YAML"
154166 <> help "path to stack.yaml file or directory" )
155167
168+target :: Parser Target
169+target =
170+ withStackage *> pure TargetStackagePackages <|>
171+ withStackageClosure *> pure TargetStackageClosure <|>
172+ pure TargetStackageOverride
173+
156174configOrigin :: Parser ConfigOrigin
157−configOrigin = OriginResolver <$> resolver
158− <|> OriginStackYaml <$> stackYamlArg
175+configOrigin =
176+ OriginStackYaml <$> stackYamlArg <*> target <|>
177+ OriginResolver <$> resolver
159178
160−-- inherited from cabal2nix
179+-- required for cabal2nix
161180
162181hackageDb :: Parser HackageDb
163182hackageDb = HackageDb <$>