git.iko.soy/typeable/cabal2nix
on master help sign in
Add option to run hpack in preConfigure
commit38b6ad822ae38cbc5c6acb677b8b339e4faf2696 authorTom Smalley <tfsmalley@gmail.com> dateWed 13 Sep 2017, 18:01 (9 years ago) parentbc72d95
4 files changed+23−14
src/Cabal2nix.hs+3 src/Distribution/Nixpkgs/Haskell/Derivation.hs+5−1 src/Distribution/Nixpkgs/Haskell/FromCabal.hs+1 src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs+14−13
src/Cabal2nix.hs+3
@@ -36,6 +36,7 @@ data Options = Options
3636 , optMaintainer :: [String]
3737--, optPlatform :: [String] -- TODO: fix command line handling of platforms
3838 , optHaddock :: Bool
39+ , optHpack :: Bool
3940 , optDoCheck :: Bool
4041 , optJailbreak :: Bool
4142 , optRevision :: Maybe String
@@ -60,6 +61,7 @@ options = Options
6061 <*> many (strOption $ long "maintainer" <> metavar "MAINTAINER" <> help "maintainer of this package (may be specified multiple times)")
6162-- <*> many (strOption $ long "platform" <> metavar "PLATFORM" <> help "supported build platforms (may be specified multiple times)")
6263 <*> flag True False (long "no-haddock" <> help "don't run Haddock when building this package")
64+ <*> switch (long "hpack" <> help "run hpack before configuring this package")
6365 <*> flag True False (long "no-check" <> help "don't run regression test suites of this package")
6466 <*> switch (long "jailbreak" <> help "disregard version restrictions on build inputs")
6567 <*> optional (strOption $ long "revision" <> help "revision to use when fetching from VCS")
@@ -136,6 +138,7 @@ cabal2nix' args = do
136138 & src .~ pkgSource pkg
137139 & subpath .~ (fromMaybe "." optSubpath)
138140 & runHaddock .~ optHaddock
141+ & runHpack .~ (optHpack || pkgRunHpack pkg)
139142 & jailbreak .~ optJailbreak
140143 & hyperlinkSource .~ optHyperlinkSource
141144 & enableLibraryProfiling .~ (fromMaybe False optEnableProfiling || optEnableLibraryProfiling)
src/Distribution/Nixpkgs/Haskell/Derivation.hs+5−1
@@ -6,7 +6,7 @@
66module Distribution.Nixpkgs.Haskell.Derivation
77 ( Derivation, nullDerivation, pkgid, revision, src, subpath, isLibrary, isExecutable
88 , extraFunctionArgs, libraryDepends, executableDepends, testDepends, configureFlags
9− , cabalFlags, runHaddock, jailbreak, doCheck, testTarget, hyperlinkSource, enableSplitObjs
9+ , cabalFlags, runHaddock, runHpack, jailbreak, doCheck, testTarget, hyperlinkSource, enableSplitObjs
1010 , enableLibraryProfiling, enableExecutableProfiling, phaseOverrides, editedCabalFile, metaSection
1111 , dependencies, setupDepends, benchmarkDepends, enableSeparateDataOutput
1212 )
@@ -59,6 +59,7 @@ data Derivation = MkDerivation
5959 , _editedCabalFile :: String
6060 , _enableSeparateDataOutput :: Bool
6161 , _metaSection :: Meta
62+ , _runHpack :: Bool
6263 }
6364 deriving (Show, Eq, Generic)
6465
@@ -90,6 +91,7 @@ nullDerivation = MkDerivation
9091 , _editedCabalFile = error "undefined Derivation.editedCabalFile"
9192 , _enableSeparateDataOutput = error "undefined Derivation.enableSeparateDataOutput"
9293 , _metaSection = error "undefined Derivation.metaSection"
94+ , _runHpack = error "undefined Derivation.runHpack"
9395 }
9496
9597makeLenses ''Derivation
@@ -130,6 +132,7 @@ instance Pretty Derivation where
130132 , boolattr "hyperlinkSource" (not _hyperlinkSource) _hyperlinkSource
131133 , onlyIf (not (null _phaseOverrides)) $ vcat ((map text . lines) _phaseOverrides)
132134 , pPrint _metaSection
135+ , onlyIf _runHpack $ attr "preConfigure" $ string "${hpack}/bin/hpack"
133136 ]
134137 , rbrace
135138 ]
@@ -138,6 +141,7 @@ instance Pretty Derivation where
138141 inputs = Set.unions [ Set.map (view (localName . ident)) _extraFunctionArgs
139142 , setOf (dependencies . each . folded . localName . ident) drv
140143 , Set.fromList ["fetch" ++ derivKind _src | derivKind _src /= "" && not isHackagePackage]
144+ , if _runHpack then Set.singleton "hpack" else Set.empty
141145 ]
142146
143147 renderedFlags = [ text "-f" <> (if enable then empty else char '-') <> text (unFlagName f) | (f, enable) <- _cabalFlags ]
src/Distribution/Nixpkgs/Haskell/FromCabal.hs+1
@@ -79,6 +79,7 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags (Packag
7979 & configureFlags .~ mempty
8080 & cabalFlags .~ flags
8181 & runHaddock .~ maybe True (not . null . exposedModules) library
82+ & runHpack .~ False
8283 & jailbreak .~ False
8384 & doCheck .~ True
8485 & testTarget .~ mempty
src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs+14−13
@@ -28,25 +28,26 @@ import System.IO ( hPutStrLn, stderr, hPutStr )
2828
2929data Package = Package
3030 { pkgSource :: DerivationSource
31+ , pkgRunHpack :: Bool
3132 , pkgCabal :: Cabal.GenericPackageDescription
3233 }
3334 deriving (Show)
3435
3536getPackage :: Maybe String -> Source -> IO Package
3637getPackage optHackageDB source = do
37− (derivSource, pkgDesc) <- fetchOrFromDB optHackageDB source
38− flip Package pkgDesc <$> maybe (sourceFromHackage (sourceHash source) (showPackageIdentifier pkgDesc) $ sourceCabalDir source) return derivSource
38+ (derivSource, runHpack, pkgDesc) <- fetchOrFromDB optHackageDB source
39+ (\s -> Package s runHpack pkgDesc) <$> maybe (sourceFromHackage (sourceHash source) (showPackageIdentifier pkgDesc) $ sourceCabalDir source) return derivSource
3940
40−fetchOrFromDB :: Maybe String -> Source -> IO (Maybe DerivationSource, Cabal.GenericPackageDescription)
41+fetchOrFromDB :: Maybe String -> Source -> IO (Maybe DerivationSource, Bool, Cabal.GenericPackageDescription)
4142fetchOrFromDB optHackageDB src
42− | "cabal://" `isPrefixOf` sourceUrl src = fmap ((,) Nothing) . fromDB optHackageDB . drop (length "cabal://") $ sourceUrl src
43+ | "cabal://" `isPrefixOf` sourceUrl src = fmap ((,,) Nothing False) . fromDB optHackageDB . drop (length "cabal://") $ sourceUrl src
4344 | otherwise = do
4445 r <- fetch (\dir -> cabalFromPath (dir </> sourceCabalDir src)) src
4546 case r of
4647 Nothing ->
4748 hPutStrLn stderr "*** failed to fetch source. Does the URL exist?" >> exitFailure
48− Just (derivSource, (externalSource, pkgDesc)) -> do
49− return (derivSource <$ guard externalSource, pkgDesc)
49+ Just (derivSource, (externalSource, (runHpack, pkgDesc))) -> do
50+ return (derivSource <$ guard externalSource, runHpack, pkgDesc)
5051
5152fromDB :: Maybe String -> String -> IO Cabal.GenericPackageDescription
5253fromDB optHackageDB pkg = do
@@ -125,21 +126,21 @@ showPackageIdentifier pkgDesc = name ++ "-" ++ display version where
125126 name = Cabal.unPackageName (Cabal.packageName pkgId)
126127 version = Cabal.packageVersion pkgId
127128
128−cabalFromPath :: FilePath -> MaybeT IO (Bool, Cabal.GenericPackageDescription)
129+cabalFromPath :: FilePath -> MaybeT IO (Bool, (Bool, Cabal.GenericPackageDescription))
129130cabalFromPath path = do
130131 d <- liftIO $ doesDirectoryExist path
131132 (,) d <$> if d
132133 then cabalFromDirectory path
133− else cabalFromFile False path
134+ else (,) False <$> cabalFromFile False path
134135
135−cabalFromDirectory :: FilePath -> MaybeT IO Cabal.GenericPackageDescription
136+cabalFromDirectory :: FilePath -> MaybeT IO (Bool, Cabal.GenericPackageDescription)
136137cabalFromDirectory dir = do
137138 cabals <- liftIO $ getDirectoryContents dir >>= filterM doesFileExist . map (dir </>) . filter (".cabal" `isSuffixOf`)
138139 case cabals of
139140 [] -> do
140141 liftIO $ hPutStrLn stderr "*** found zero cabal files. Trying hpack..."
141− hpackFromDirectory dir
142− [cabalFile] -> cabalFromFile True cabalFile
142+ (,) True <$> hpackFromDirectory dir
143+ [cabalFile] -> (,) False <$> cabalFromFile True cabalFile
143144 _ -> liftIO $ hPutStrLn stderr ("*** found more than one cabal file (" ++ show cabals ++ "). Exiting.") >> exitFailure
144145
145146handleIO :: (Exception.IOException -> IO a) -> IO a -> IO a
@@ -150,8 +151,8 @@ hpackFromDirectory dir = do
150151 mPackage <- liftIO $ Hpack.readPackageConfig $ dir </> "package.yaml"
151152 case mPackage of
152153 Left err -> liftIO $ hPutStrLn stderr ("*** hpack error: " ++ show err ++ ". Exiting.") >> exitFailure
153− Right (_, pkg) -> do
154− let buf = LBS8.pack $ Hpack.renderPackage Hpack.defaultRenderSettings 0 [] [] pkg
154+ Right (_, pkg') -> do
155+ let buf = LBS8.pack $ Hpack.renderPackage Hpack.defaultRenderSettings 0 [] [] pkg'
155156 hash = printSHA256 (digest (digestByName "sha256") buf)
156157 case parsePackage' buf of
157158 Left msg -> liftIO $ hPutStrLn stderr ("*** cannot parse hpack output: " ++ msg) >> exitFailure