git.iko.soy/typeable/stackage2nix
on master help sign in
Get stack.yaml from environment
commit1822c86cb340ca0a3d491205c29265f6fa074a75 authorDmitry Bushev <bushevdv@gmail.com> dateSat 24 Jun 2017, 22:53 (9 years ago) parente1cee4f
3 files changed+73−62
nix-test+4−1 src/Runner.hs+15−11 src/Runner/Cli.hs+54−50
nix-test+4−1
@@ -1,5 +1,7 @@
11#!/usr/bin/env bash
22
3+set -e
4+
35LTS_HASKELL_DIR=${LTS_HASKELL_DIR:-~/lts-haskell}
46ALL_CABAL_HASHES_DIR=${ALL_CABAL_HASHES_DIR:-~/all-cabal-hashes}
57
@@ -14,7 +16,8 @@ fi
1416set -x
1517stack exec -- stack2nix \
1618 --lts-haskell "$LTS_HASKELL_DIR" \
17− --all-cabal-hashes "$ALL_CABAL_HASHES_DIR"
19+ --all-cabal-hashes "$ALL_CABAL_HASHES_DIR" \
20+ .
1821set +x
1922
2023nix-build -A stack2nix "$@"
src/Runner.hs+15−11
@@ -1,6 +1,7 @@
11module Runner ( run ) where
22
33import Control.Lens
4+import Data.Functor
45import Distribution.Nixpkgs.Haskell.FromStack
56import Distribution.Nixpkgs.Haskell.FromStack.Package
67import Distribution.Nixpkgs.Haskell.Stack
@@ -21,7 +22,10 @@ import qualified Stackage2nix as S2n
2122run :: IO ()
2223run = do
2324 opts <- execParser pinfo
24− stackConf <- either fail pure =<< readStackConfig (opts ^. optStackYaml)
25+ stackYaml <- envStackYaml >>= \case
26+ Just p -> putStrLn "Getting project config file from STACK_YAML environment" $> p
27+ Nothing -> pure $ opts ^. optStackYaml
28+ stackConf <- either fail pure =<< readStackConfig stackYaml
2529 let
2630 s2nOpts = mkS2nOptions stackConf opts
2731 (buildPlan, packageSetConfig) <- S2n.run s2nOpts
@@ -33,33 +37,33 @@ run = do
3337 $ stackConf ^. scPackages
3438 let
3539 out = PP.overrideHaskellPackages overrideConfig packages
36− writeFile (opts ^. optOutFile) (show out)
40+ writeFile (opts ^. optOutDerivation) (show out)
3741 -- generate stackage packages
3842 nodes <- traverse (uncurry $ buildNodeM packageSetConfig)
3943 $ Map.toList (bpPackages buildPlan)
4044 -- let nodes = reachableDependencies (mkNode <$> packages) allNodes
41− withFile (opts ^. optStackageOutPackages) WriteMode $ \h -> do
45+ withFile (opts ^. optOutStackagePackages) WriteMode $ \h -> do
4246 hPutStrLn h ("# Generated by stack2nix from " ++ S2n.optBuildPlanFile s2nOpts)
4347 hPutStrLn h $ render $ pPrintOutPackages (nodeDerivation <$> nodes)
44− withFile (opts ^. optStackageOutConfig) WriteMode $ \h -> do
48+ withFile (opts ^. optOutStackageConfig) WriteMode $ \h -> do
4549 hPutStrLn h ("# Generated by stack2nix from " ++ S2n.optBuildPlanFile s2nOpts)
4650 hPutStrLn h $ render $ pPrintOutConfig (bpSystemInfo buildPlan) nodes
47− putStrLn $ "Derivation was written to " ++ opts ^. optOutFile
51+ putStrLn $ "Derivation was written to " ++ opts ^. optOutDerivation
4852
4953mkOverrideConfig :: Options -> Version -> OverrideConfig
5054mkOverrideConfig opts ghcVersion = OverrideConfig
5155 { _ocGhc = ghcVersion
52− , _ocStackagePackages = opts ^. optStackageOutPackages
53− , _ocStackageConfig = opts ^. optStackageOutConfig }
56+ , _ocStackagePackages = opts ^. optOutStackagePackages
57+ , _ocStackageConfig = opts ^. optOutStackageConfig }
5458
5559mkS2nOptions :: StackConfig -> Options -> S2n.Options
5660mkS2nOptions conf opts = S2n.Options
5761 { S2n.optBuildPlanFile =
58− (opts ^. optLtsHaskellPath) Path.</>
62+ (opts ^. optLtsHaskellRepo) Path.</>
5963 (conf ^. scResolver . to unStackResolver) Path.<.>
6064 ".yaml"
61− , S2n.optAllCabalHashes = opts ^. optAllCabalHashesPath
65+ , S2n.optAllCabalHashes = opts ^. optAllCabalHashesRepo
6266 , S2n.optNixpkgsRepository = opts ^. optNixpkgsRepository
6367 , S2n.optNixpkgsMap = Nothing
64− , S2n.optOutPackages = opts ^. optStackageOutPackages
65− , S2n.optOutConfig = opts ^. optStackageOutConfig }
68+ , S2n.optOutPackages = opts ^. optOutStackagePackages
69+ , S2n.optOutConfig = opts ^. optOutStackageConfig }
src/Runner/Cli.hs+54−50
@@ -11,34 +11,47 @@ import Distribution.Nixpkgs.Haskell.Stack
1111import Distribution.System as System
1212import qualified Distribution.Text as Text
1313import Options.Applicative as Opts
14−import Stack.Config
1514import Stack.Types
15+import System.Environment
16+import System.FilePath
1617
1718
1819data Options = Options
19− { _optOutFile :: !FilePath
20+ { _optAllCabalHashesRepo :: !FilePath
21+ , _optLtsHaskellRepo :: !FilePath
22+ , _optOutStackagePackages :: !FilePath
23+ , _optOutStackageConfig :: !FilePath
24+ , _optOutDerivation :: !FilePath
2025 , _optHackageDb :: !(Maybe HackageDb)
2126 , _optNixpkgsRepository :: !FilePath
22− , _optAllCabalHashesPath :: !FilePath
23− , _optLtsHaskellPath :: !FilePath
24− , _optStackageOutPackages :: !FilePath
25− , _optStackageOutConfig :: !FilePath
2627 , _optCompilerId :: !CompilerId
2728 , _optPlatform :: !Platform
28− , _optStackYaml :: !FilePath
29+ , _optStackYamlArg :: !FilePath
2930 } deriving (Show)
3031
3132makeLenses ''Options
3233
34+envStackYaml :: IO (Maybe FilePath)
35+envStackYaml = lookupEnv "STACK_YAML"
36+
37+optStackYaml :: Getter Options FilePath
38+optStackYaml = optStackYamlArg . to stackYamlFile
39+ where
40+ stackYamlFile p = case splitFileName p of
41+ (dir, "") -> dir </> "stack.yaml"
42+ (dir, ".") -> dir </> "stack.yaml"
43+ (_ , "..") -> p </> "stack.yaml"
44+ _ -> p
45+
3346options :: Parser Options
3447options = Options
35− <$> outFile
48+ <$> allCabalHashesRepo
49+ <*> ltsHaskellRepo
50+ <*> outStackagePackages
51+ <*> outStackageConfig
52+ <*> outDerivation
3653 <*> optional hackageDb
3754 <*> nixpkgsRepository
38− <*> allCabalHashesPath
39− <*> ltsHaskellPath
40− <*> stackageOutPackages
41− <*> stackageOutConfig
4255 <*> compilerId
4356 <*> platform
4457 <*> stackYamlArg
@@ -50,14 +63,6 @@ pinfo = info
5063 ( fullDesc
5164 <> header "stack2nix converts Stack files into build instructions for Nix." )
5265
53−outFile :: Parser FilePath
54−outFile = option str
55− ( long "nix-out-file"
56− <> metavar "NIX_OUT_FILE"
57− <> help "path to output derivation"
58− <> value "default.nix"
59− <> showDefaultWith id )
60−
6166nixpkgsRepository :: Parser FilePath
6267nixpkgsRepository = option str
6368 ( long "nixpkgs"
@@ -66,51 +71,48 @@ nixpkgsRepository = option str
6671 <> value "<nixpkgs>"
6772 <> showDefaultWith id )
6873
69−stackageBuildPlan :: Parser FilePath
70−stackageBuildPlan = option str
71− ( long "stackage-build-plan"
72− <> metavar "STACKAGE_BUILD_PLAN"
73− <> help "stackage build plan (YAML)" )
74−
75−allCabalHashesPath :: Parser FilePath
76−allCabalHashesPath = option str
74+allCabalHashesRepo :: Parser FilePath
75+allCabalHashesRepo = option str
7776 ( long "all-cabal-hashes"
78− <> metavar "ALL_CABAL_HASHES"
77+ <> metavar "REPO_DIR"
7978 <> help "path to commercialhaskell/all-cabal-hashes repository" )
8079
81−ltsHaskellPath :: Parser FilePath
82−ltsHaskellPath = option str
80+ltsHaskellRepo :: Parser FilePath
81+ltsHaskellRepo = option str
8382 ( long "lts-haskell"
84− <> metavar "LTS_HASKELL_DIR"
83+ <> metavar "REPO_DIR"
8584 <> help "path to fpco/lts-haskell repository" )
8685
87−stackResolver :: Parser StackResolver
88−stackResolver = StackResolver <$>
89− option text
90− ( long "resolver"
91− <> metavar "RESOLVER"
92− <> help "override stack resolver" )
93−
94−stackageOutPackages :: Parser FilePath
95−stackageOutPackages = option str
96− ( long "nix-stackage-packages"
97− <> metavar "STACKAGE_PACKAGES"
98− <> help "name of the output file for the package set"
86+outStackagePackages :: Parser FilePath
87+outStackagePackages = option str
88+ ( long "out-packages"
89+ <> metavar "NIX_FILE"
90+ <> help "output file of the stackage packages set"
9991 <> value "packages.nix"
10092 <> showDefaultWith id)
10193
102−stackageOutConfig :: Parser FilePath
103−stackageOutConfig = option str
104− ( long "nix-stackage-config"
105− <> metavar "STACKAGE_CONFIG"
106− <> help "name of the output file for the package set configuration"
94+outStackageConfig :: Parser FilePath
95+outStackageConfig = option str
96+ ( long "out-config"
97+ <> metavar "NIX_FILE"
98+ <> help "output file of the stackage packages compiler config"
10799 <> value "configuration-packages.nix"
108100 <> showDefaultWith id)
109101
102+outDerivation :: Parser FilePath
103+outDerivation = option str
104+ ( long "out-derivation"
105+ <> metavar "NIX_FILE"
106+ <> help "path to output derivation"
107+ <> value "default.nix"
108+ <> showDefaultWith id )
109+
110110stackYamlArg :: Parser FilePath
111111stackYamlArg = Opts.argument str
112112 ( metavar "STACK_YAML"
113− <> value "stack.yaml" )
113+ <> help "path to stack.yaml file or directory" )
114+
115+-- inherited from cabal2nix
114116
115117hackageDb :: Parser HackageDb
116118hackageDb = HackageDb <$>
@@ -140,6 +142,8 @@ platform = option (readP Text.parse)
140142 <> value buildPlatform
141143 <> showDefaultWith Text.display )
142144
145+-- utils
146+
143147text :: ReadM Text
144148text = T.pack <$> str
145149