@@ -1,9 +1,9 @@
1module Cabal2Nix.Package where
1module Cabal2Nix.Package where
3+import Control.Applicative
3import Control.Monad
4import Control.Monad
4import Control.Monad.IO.Class
5import Control.Monad.IO.Class
5import Control.Monad.Trans.Maybe
6import Control.Monad.Trans.Maybe
6−import Data.Functor ( (<$>), (<$) )
7import Data.List ( isSuffixOf, isPrefixOf )
7import Data.List ( isSuffixOf, isPrefixOf )
8import Data.Maybe ( listToMaybe )
8import Data.Maybe ( listToMaybe )
9import Distribution.NixOS.Derivation.Cabal
9import Distribution.NixOS.Derivation.Cabal
@@ -74,15 +74,28 @@ hashCachePath pid = do
74sourceFromHackage :: Maybe String -> String -> IO DerivationSource
74sourceFromHackage :: Maybe String -> String -> IO DerivationSource
75sourceFromHackage optHash pkgId = do
75sourceFromHackage optHash pkgId = do
76 cacheFile <- hashCachePath pkgId
76 cacheFile <- hashCachePath pkgId
77− hash <- maybe (readFileMay cacheFile) (return . Just) optHash
77+ let cachedHash = MaybeT $ maybe (readFileMay cacheFile) (return . Just) optHash
78+ let url = "mirror://hackage/" ++ pkgId ++ ".tar.gz"
80+ -- Use the cached hash (either from cache file or given on cmdline via sha256 opt)
81+ -- if available, otherwise download from hackage to compute hash.
78− res <- runMaybeT $ fst <$> fetchWith (False, "url") (Source ("mirror://hackage/" ++ pkgId ++ ".tar.gz") "" hash)
82+ maybeHash <- runMaybeT $ cachedHash <|> derivHash . fst <$> fetchWith (False, "url") (Source url "" Nothing)
79− case res of
83+ case maybeHash of
80− Just r -> writeFile cacheFile (derivHash r) >> return r
84+ Just hash ->
85+ -- We need to force the hash here. If we didn't do this, then when reading the
86+ -- hash from the cache file, the cache file will still be open for reading
87+ -- (because lazy io) when writeFile opens the file again for writing. By forcing
88+ -- the hash here, we ensure that the file is closed before opening it again.
90+ DerivationSource "url" url "" hash <$ writeFile cacheFile hash
81 Nothing -> do
91 Nothing -> do
82 hPutStr stderr $ unlines
92 hPutStr stderr $ unlines
83 [ "*** cannot compute hash. (Not a hackage project?)"
93 [ "*** cannot compute hash. (Not a hackage project?)"
84− , " Specify hash explicitly via --sha256 and add appropriate \"src\" attribute"
94+ , " If your project is not on hackage, please supply the path to the root directory of"
85− , " to resulting nix expression."
95+ , " the project, not to the cabal file."
97+ , " If your project is on hackage but you still want to specify the hash manually, you"
98+ , " can use the --sha256 option."
87 exitFailure
100 exitFailure