git.iko.soy/typeable/stackage2nix
on master help sign in
Get stack.yaml from environment
1822c86Dmitry Bushev
nix-test+4−1
@@ -1,5 +1,7 @@
1#!/usr/bin/env bash
1#!/usr/bin/env bash
2
2
3+set -e
4+
3LTS_HASKELL_DIR=${LTS_HASKELL_DIR:-~/lts-haskell}
5LTS_HASKELL_DIR=${LTS_HASKELL_DIR:-~/lts-haskell}
4ALL_CABAL_HASHES_DIR=${ALL_CABAL_HASHES_DIR:-~/all-cabal-hashes}
6ALL_CABAL_HASHES_DIR=${ALL_CABAL_HASHES_DIR:-~/all-cabal-hashes}
5
7
@@ -14,7 +16,8 @@ fi
14set -x
16set -x
15stack exec -- stack2nix \
17stack exec -- stack2nix \
16 --lts-haskell "$LTS_HASKELL_DIR" \
18 --lts-haskell "$LTS_HASKELL_DIR" \
17− --all-cabal-hashes "$ALL_CABAL_HASHES_DIR"
19+ --all-cabal-hashes "$ALL_CABAL_HASHES_DIR" \
20+ .
18set +x
21set +x
19
22
20nix-build -A stack2nix "$@"
23nix-build -A stack2nix "$@"
src/Runner.hs+15−11
@@ -1,6 +1,7 @@
1module Runner ( run ) where
1module Runner ( run ) where
2
2
3import Control.Lens
3import Control.Lens
4+import Data.Functor
4import Distribution.Nixpkgs.Haskell.FromStack
5import Distribution.Nixpkgs.Haskell.FromStack
5import Distribution.Nixpkgs.Haskell.FromStack.Package
6import Distribution.Nixpkgs.Haskell.FromStack.Package
6import Distribution.Nixpkgs.Haskell.Stack
7import Distribution.Nixpkgs.Haskell.Stack
@@ -21,7 +22,10 @@ import qualified Stackage2nix as S2n
21run :: IO ()
22run :: IO ()
22run = do
23run = do
23 opts <- execParser pinfo
24 opts <- execParser pinfo
25+ stackYaml <- envStackYaml >>= \case
26+ Just p -> putStrLn "Getting project config file from STACK_YAML environment" $> p
27+ Nothing -> pure $ opts ^. optStackYaml
24− stackConf <- either fail pure =<< readStackConfig (opts ^. optStackYaml)
28+ stackConf <- either fail pure =<< readStackConfig stackYaml
25 let
29 let
26 s2nOpts = mkS2nOptions stackConf opts
30 s2nOpts = mkS2nOptions stackConf opts
27 (buildPlan, packageSetConfig) <- S2n.run s2nOpts
31 (buildPlan, packageSetConfig) <- S2n.run s2nOpts
@@ -33,33 +37,33 @@ run = do
33 $ stackConf ^. scPackages
37 $ stackConf ^. scPackages
34 let
38 let
35 out = PP.overrideHaskellPackages overrideConfig packages
39 out = PP.overrideHaskellPackages overrideConfig packages
36− writeFile (opts ^. optOutFile) (show out)
40+ writeFile (opts ^. optOutDerivation) (show out)
37 -- generate stackage packages
41 -- generate stackage packages
38 nodes <- traverse (uncurry $ buildNodeM packageSetConfig)
42 nodes <- traverse (uncurry $ buildNodeM packageSetConfig)
39 $ Map.toList (bpPackages buildPlan)
43 $ Map.toList (bpPackages buildPlan)
40 -- let nodes = reachableDependencies (mkNode <$> packages) allNodes
44 -- let nodes = reachableDependencies (mkNode <$> packages) allNodes
41− withFile (opts ^. optStackageOutPackages) WriteMode $ \h -> do
45+ withFile (opts ^. optOutStackagePackages) WriteMode $ \h -> do
42 hPutStrLn h ("# Generated by stack2nix from " ++ S2n.optBuildPlanFile s2nOpts)
46 hPutStrLn h ("# Generated by stack2nix from " ++ S2n.optBuildPlanFile s2nOpts)
43 hPutStrLn h $ render $ pPrintOutPackages (nodeDerivation <$> nodes)
47 hPutStrLn h $ render $ pPrintOutPackages (nodeDerivation <$> nodes)
44− withFile (opts ^. optStackageOutConfig) WriteMode $ \h -> do
48+ withFile (opts ^. optOutStackageConfig) WriteMode $ \h -> do
45 hPutStrLn h ("# Generated by stack2nix from " ++ S2n.optBuildPlanFile s2nOpts)
49 hPutStrLn h ("# Generated by stack2nix from " ++ S2n.optBuildPlanFile s2nOpts)
46 hPutStrLn h $ render $ pPrintOutConfig (bpSystemInfo buildPlan) nodes
50 hPutStrLn h $ render $ pPrintOutConfig (bpSystemInfo buildPlan) nodes
47− putStrLn $ "Derivation was written to " ++ opts ^. optOutFile
51+ putStrLn $ "Derivation was written to " ++ opts ^. optOutDerivation
48
52
49mkOverrideConfig :: Options -> Version -> OverrideConfig
53mkOverrideConfig :: Options -> Version -> OverrideConfig
50mkOverrideConfig opts ghcVersion = OverrideConfig
54mkOverrideConfig opts ghcVersion = OverrideConfig
51 { _ocGhc = ghcVersion
55 { _ocGhc = ghcVersion
52− , _ocStackagePackages = opts ^. optStackageOutPackages
56+ , _ocStackagePackages = opts ^. optOutStackagePackages
53− , _ocStackageConfig = opts ^. optStackageOutConfig }
57+ , _ocStackageConfig = opts ^. optOutStackageConfig }
54
58
55mkS2nOptions :: StackConfig -> Options -> S2n.Options
59mkS2nOptions :: StackConfig -> Options -> S2n.Options
56mkS2nOptions conf opts = S2n.Options
60mkS2nOptions conf opts = S2n.Options
57 { S2n.optBuildPlanFile =
61 { S2n.optBuildPlanFile =
58− (opts ^. optLtsHaskellPath) Path.</>
62+ (opts ^. optLtsHaskellRepo) Path.</>
59 (conf ^. scResolver . to unStackResolver) Path.<.>
63 (conf ^. scResolver . to unStackResolver) Path.<.>
60 ".yaml"
64 ".yaml"
61− , S2n.optAllCabalHashes = opts ^. optAllCabalHashesPath
65+ , S2n.optAllCabalHashes = opts ^. optAllCabalHashesRepo
62 , S2n.optNixpkgsRepository = opts ^. optNixpkgsRepository
66 , S2n.optNixpkgsRepository = opts ^. optNixpkgsRepository
63 , S2n.optNixpkgsMap = Nothing
67 , S2n.optNixpkgsMap = Nothing
64− , S2n.optOutPackages = opts ^. optStackageOutPackages
68+ , S2n.optOutPackages = opts ^. optOutStackagePackages
65− , S2n.optOutConfig = opts ^. optStackageOutConfig }
69+ , S2n.optOutConfig = opts ^. optOutStackageConfig }
src/Runner/Cli.hs+54−50
@@ -11,34 +11,47 @@ import Distribution.Nixpkgs.Haskell.Stack
11import Distribution.System as System
11import Distribution.System as System
12import qualified Distribution.Text as Text
12import qualified Distribution.Text as Text
13import Options.Applicative as Opts
13import Options.Applicative as Opts
14−import Stack.Config
15import Stack.Types
14import Stack.Types
15+import System.Environment
16+import System.FilePath
16
17
17
18
18data Options = Options
19data Options = Options
19− { _optOutFile :: !FilePath
20+ { _optAllCabalHashesRepo :: !FilePath
21+ , _optLtsHaskellRepo :: !FilePath
22+ , _optOutStackagePackages :: !FilePath
23+ , _optOutStackageConfig :: !FilePath
24+ , _optOutDerivation :: !FilePath
20 , _optHackageDb :: !(Maybe HackageDb)
25 , _optHackageDb :: !(Maybe HackageDb)
21 , _optNixpkgsRepository :: !FilePath
26 , _optNixpkgsRepository :: !FilePath
22− , _optAllCabalHashesPath :: !FilePath
23− , _optLtsHaskellPath :: !FilePath
24− , _optStackageOutPackages :: !FilePath
25− , _optStackageOutConfig :: !FilePath
26 , _optCompilerId :: !CompilerId
27 , _optCompilerId :: !CompilerId
27 , _optPlatform :: !Platform
28 , _optPlatform :: !Platform
28− , _optStackYaml :: !FilePath
29+ , _optStackYamlArg :: !FilePath
29 } deriving (Show)
30 } deriving (Show)
30
31
31makeLenses ''Options
32makeLenses ''Options
32
33
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+
33options :: Parser Options
46options :: Parser Options
34options = Options
47options = Options
35− <$> outFile
48+ <$> allCabalHashesRepo
49+ <*> ltsHaskellRepo
50+ <*> outStackagePackages
51+ <*> outStackageConfig
52+ <*> outDerivation
36 <*> optional hackageDb
53 <*> optional hackageDb
37 <*> nixpkgsRepository
54 <*> nixpkgsRepository
38− <*> allCabalHashesPath
39− <*> ltsHaskellPath
40− <*> stackageOutPackages
41− <*> stackageOutConfig
42 <*> compilerId
55 <*> compilerId
43 <*> platform
56 <*> platform
44 <*> stackYamlArg
57 <*> stackYamlArg
@@ -50,14 +63,6 @@ pinfo = info
50 ( fullDesc
63 ( fullDesc
51 <> header "stack2nix converts Stack files into build instructions for Nix." )
64 <> header "stack2nix converts Stack files into build instructions for Nix." )
52
65
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−
61nixpkgsRepository :: Parser FilePath
66nixpkgsRepository :: Parser FilePath
62nixpkgsRepository = option str
67nixpkgsRepository = option str
63 ( long "nixpkgs"
68 ( long "nixpkgs"
@@ -66,51 +71,48 @@ nixpkgsRepository = option str
66 <> value "<nixpkgs>"
71 <> value "<nixpkgs>"
67 <> showDefaultWith id )
72 <> showDefaultWith id )
68
73
69−stackageBuildPlan :: Parser FilePath
74+allCabalHashesRepo :: Parser FilePath
70−stackageBuildPlan = option str
75+allCabalHashesRepo = 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
77 ( long "all-cabal-hashes"
76 ( long "all-cabal-hashes"
78− <> metavar "ALL_CABAL_HASHES"
77+ <> metavar "REPO_DIR"
79 <> help "path to commercialhaskell/all-cabal-hashes repository" )
78 <> help "path to commercialhaskell/all-cabal-hashes repository" )
80
79
81−ltsHaskellPath :: Parser FilePath
80+ltsHaskellRepo :: Parser FilePath
82−ltsHaskellPath = option str
81+ltsHaskellRepo = option str
83 ( long "lts-haskell"
82 ( long "lts-haskell"
84− <> metavar "LTS_HASKELL_DIR"
83+ <> metavar "REPO_DIR"
85 <> help "path to fpco/lts-haskell repository" )
84 <> help "path to fpco/lts-haskell repository" )
86
85
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
86+outStackagePackages :: Parser FilePath
95−stackageOutPackages = option str
87+outStackagePackages = option str
96− ( long "nix-stackage-packages"
88+ ( long "out-packages"
97− <> metavar "STACKAGE_PACKAGES"
89+ <> metavar "NIX_FILE"
98− <> help "name of the output file for the package set"
90+ <> help "output file of the stackage packages set"
99 <> value "packages.nix"
91 <> value "packages.nix"
100 <> showDefaultWith id)
92 <> showDefaultWith id)
101
93
102−stackageOutConfig :: Parser FilePath
94+outStackageConfig :: Parser FilePath
103−stackageOutConfig = option str
95+outStackageConfig = option str
104− ( long "nix-stackage-config"
96+ ( long "out-config"
105− <> metavar "STACKAGE_CONFIG"
97+ <> metavar "NIX_FILE"
106− <> help "name of the output file for the package set configuration"
98+ <> help "output file of the stackage packages compiler config"
107 <> value "configuration-packages.nix"
99 <> value "configuration-packages.nix"
108 <> showDefaultWith id)
100 <> showDefaultWith id)
109
101
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+
110stackYamlArg :: Parser FilePath
110stackYamlArg :: Parser FilePath
111stackYamlArg = Opts.argument str
111stackYamlArg = Opts.argument str
112 ( metavar "STACK_YAML"
112 ( metavar "STACK_YAML"
113− <> value "stack.yaml" )
113+ <> help "path to stack.yaml file or directory" )
114+
115+-- inherited from cabal2nix
114
116
115hackageDb :: Parser HackageDb
117hackageDb :: Parser HackageDb
116hackageDb = HackageDb <$>
118hackageDb = HackageDb <$>
@@ -140,6 +142,8 @@ platform = option (readP Text.parse)
140 <> value buildPlatform
142 <> value buildPlatform
141 <> showDefaultWith Text.display )
143 <> showDefaultWith Text.display )
142
144
145+-- utils
146+
143text :: ReadM Text
147text :: ReadM Text
144text = T.pack <$> str
148text = T.pack <$> str
145
149