git.iko.soy/typeable/cabal2nix
on master help sign in
Fix sha256 option and improve error messages
commit66dd4c64a79f801866e1b72e1d69e2e9210e3cb1 authorBenno Fünfstück <benno.fuenfstueck@gmail.com> dateSun 17 Aug 2014, 19:05 (12 years ago) parent813d429
1 file changed+20−7
src/Cabal2Nix/Package.hs+20−7
src/Cabal2Nix/Package.hs+20−7
@@ -1,9 +1,9 @@
11module Cabal2Nix.Package where
22
3+import Control.Applicative
34import Control.Monad
45import Control.Monad.IO.Class
56import Control.Monad.Trans.Maybe
6−import Data.Functor ( (<$>), (<$) )
77import Data.List ( isSuffixOf, isPrefixOf )
88import Data.Maybe ( listToMaybe )
99import Distribution.NixOS.Derivation.Cabal
@@ -74,15 +74,28 @@ hashCachePath pid = do
7474sourceFromHackage :: Maybe String -> String -> IO DerivationSource
7575sourceFromHackage optHash pkgId = do
7676 cacheFile <- hashCachePath pkgId
77− hash <- maybe (readFileMay cacheFile) (return . Just) optHash
78− res <- runMaybeT $ fst <$> fetchWith (False, "url") (Source ("mirror://hackage/" ++ pkgId ++ ".tar.gz") "" hash)
79− case res of
80− Just r -> writeFile cacheFile (derivHash r) >> return r
77+ let cachedHash = MaybeT $ maybe (readFileMay cacheFile) (return . Just) optHash
78+ let url = "mirror://hackage/" ++ pkgId ++ ".tar.gz"
79+
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.
82+ maybeHash <- runMaybeT $ cachedHash <|> derivHash . fst <$> fetchWith (False, "url") (Source url "" Nothing)
83+ case maybeHash of
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.
89+ seq (length hash) $
90+ DerivationSource "url" url "" hash <$ writeFile cacheFile hash
8191 Nothing -> do
8292 hPutStr stderr $ unlines
8393 [ "*** cannot compute hash. (Not a hackage project?)"
84− , " Specify hash explicitly via --sha256 and add appropriate \"src\" attribute"
85− , " to resulting nix expression."
94+ , " If your project is not on hackage, please supply the path to the root directory of"
95+ , " the project, not to the cabal file."
96+ , ""
97+ , " If your project is on hackage but you still want to specify the hash manually, you"
98+ , " can use the --sha256 option."
8699 ]
87100 exitFailure
88101