git.iko.soy/typeable/cabal2nix
on master help sign in
Merge pull request #357 from NixOS/refactor-cabal2nix

Refactor cabal2nix lib api

commitae983e330edf22a7bb825be1c537013278b2257c authorPeter Simons <simons@cryp.to> dateTue 10 Jul 2018, 20:23 (8 years ago) parente82312f890c282
2 files changed+16−13
src/Cabal2nix.hs+15−12 src/Distribution/Nixpkgs/Haskell/Hackage.hs+1−1
src/Cabal2nix.hs+15−12
@@ -3,7 +3,8 @@
33{-# LANGUAGE RecordWildCards #-}
44
55module Cabal2nix
6− ( main, cabal2nix, cabal2nix', cabal2nixWithDB
6+ ( main, cabal2nix, cabal2nix', cabal2nixWithDB, parseArgs
7+ , Options(..)
78 )
89 where
910
@@ -61,9 +62,9 @@ data Options = Options
6162 , optSystem :: Platform
6263 , optSubpath :: Maybe FilePath
6364 , optHackageSnapshot :: Maybe UTCTime
65+ , optNixpkgsIdentifier :: NixpkgsResolver
6466 , optUrl :: String
6567 }
66− deriving (Show)
6768
6869options :: Parser Options
6970options = Options
@@ -87,6 +88,7 @@ options = Options
8788 <*> option (maybeReader parsePlatform) (long "system" <> help "host system (in either short Nix format or full LLVM style) to use when evaluating the Cabal file" <> value buildPlatform <> showDefaultWith display)
8889 <*> optional (strOption $ long "subpath" <> metavar "PATH" <> help "Path to Cabal file's directory relative to the URI (default is root directory)")
8990 <*> optional (option utcTimeReader (long "hackage-snapshot" <> help "hackage snapshot time, ISO format"))
91+ <*> pure (\i -> Just (binding # (i, path # [i])))
9092 <*> strArgument (metavar "URI")
9193
9294-- | A parser for the date. Hackage updates happen maybe once or twice a month.
@@ -183,19 +185,15 @@ hpackOverrides :: Derivation -> Derivation
183185hpackOverrides = over phaseOverrides (++ "preConfigure = \"hpack\";")
184186 . set (libraryDepends . tool . contains (PP.pkg "hpack")) True
185187
186−cabal2nix' :: [String] -> IO (Either Doc Derivation)
187−cabal2nix' args = do
188− opts@Options {..} <- handleParseResult $ execParserPure defaultPrefs pinfo args
189−
188+cabal2nix' :: Options -> IO (Either Doc Derivation)
189+cabal2nix' opts@Options{..} = do
190190 pkg <- getPackage optHpack optHackageDb optHackageSnapshot $ Source optUrl (fromMaybe "" optRevision) (maybe UnknownHash Guess optSha256) (fromMaybe "" optSubpath)
191191 processPackage opts pkg
192192
193−cabal2nixWithDB :: DB.HackageDB -> [String] -> IO (Either Doc Derivation)
194−cabal2nixWithDB db args = do
195− opts@Options {..} <- handleParseResult $ execParserPure defaultPrefs pinfo args
193+cabal2nixWithDB :: DB.HackageDB -> Options -> IO (Either Doc Derivation)
194+cabal2nixWithDB db opts@Options{..} = do
196195 when (isJust optHackageDb) $ hPutStrLn stderr "WARN: HackageDB provided directly; ignoring --hackage-db"
197196 when (isJust optHackageSnapshot) $ hPutStrLn stderr "WARN: HackageDB provided directly; ignoring --hackage-snapshot"
198−
199197 pkg <- getPackage' optHpack (return db) $ Source optUrl (fromMaybe "" optRevision) (maybe UnknownHash Guess optSha256) (fromMaybe "" optSubpath)
200198 processPackage opts pkg
201199
@@ -210,7 +208,7 @@ processPackage Options{..} pkg = do
210208
211209 deriv :: Derivation
212210 deriv = withHpackOverrides $ fromGenericPackageDescription (const True)
213− (\i -> Just (binding # (i, path # [i])))
211+ optNixpkgsIdentifier
214212 optSystem
215213 (unknownCompilerInfo optCompiler NoAbiTag)
216214 flags
@@ -253,7 +251,12 @@ processPackage Options{..} pkg = do
253251 pure $ if optNixShellOutput then Left shell else Right deriv
254252
255253cabal2nix :: [String] -> IO ()
256−cabal2nix = cabal2nix' >=> putStrLn . either render prettyShow
254+cabal2nix = parseArgs >=> cabal2nix' >=> putStrLn . either render prettyShow
255+
256+parseArgs :: [String] -> IO Options
257+parseArgs = handleParseResult . execParserPure defaultPrefs pinfo
258+
259+-- Utils
257260
258261readFlagList :: [String] -> FlagAssignment
259262readFlagList = mkFlagAssignment . map tagWithValue
src/Distribution/Nixpkgs/Haskell/Hackage.hs+1−1
@@ -1,6 +1,6 @@
11module Distribution.Nixpkgs.Haskell.Hackage
22 ( HackageDB, PackageData, VersionData(..)
3− , hackageTarball, readTarball
3+ , hackageTarball, readTarball, parsePackageData
44 )
55 where
66