git.iko.soy/typeable/cabal2nix
on master help sign in
Merge pull request #51 from danbst/master

cabal2nix: added info warning for situations when SHA hash cannot be autocomputed

commitdd44df81f05f08479d5206ccc918c4a45a7e7f20 authorPeter Simons <simons@cryp.to> dateFri 30 Aug 2013, 02:00 (13 years ago) parent479384d6c56f98
2 files changed+15−5
src/Cabal2Nix/Hackage.hs+9−1 src/cabal2nix.hs+6−4
src/Cabal2Nix/Hackage.hs+9−1
@@ -11,6 +11,7 @@ import Network.Browser ( browse, request, setCheckForProxy, setDebugLog, setOutH
1111import System.Directory ( doesFileExist, getHomeDirectory, createDirectoryIfMissing )
1212import System.FilePath ( dropFileName, (</>), (<.>) )
1313import System.Process ( readProcess )
14+import Control.Exception (handle, SomeException(..))
1415
1516import qualified Distribution.Hackage.DB as DB
1617
@@ -35,12 +36,19 @@ hashPackage pkg = do
3536 exists <- doesFileExist cachePath
3637 hash' <- if exists
3738 then readFile cachePath
38− else readProcess "bash" ["-c", "exec nix-prefetch-url 2>/dev/tty " ++ hackagePath pkg TarGz] ""
39+ else getHackageHash
3940 let hash = reverse (dropWhile (=='\n') (reverse hash'))
4041 unless exists $ do
4142 createDirectoryIfMissing True (dropFileName cachePath)
4243 writeFile cachePath hash
4344 return hash
45+ where getHackageHash = do
46+ let command = "exec nix-prefetch-url 2>/dev/tty " ++ hackagePath pkg TarGz
47+ handle handlePrefetchError (readProcess "bash" ["-c", command] "")
48+ handlePrefetchError (SomeException _) = do
49+ error $ "\nError: Cannot compute hash. (Not a hackage project?)\n" ++
50+ "Specify hash explicitly via --sha256 and add appropriate \"src\" attribute " ++
51+ "to resulting nix expression."
4452
4553hashCachePath :: PackageIdentifier -> IO FilePath
4654hashCachePath (PackageIdentifier (PackageName name) version') = do
src/cabal2nix.hs+6−4
@@ -18,7 +18,7 @@ import System.IO ( hPutStrLn, hFlush, stdout, stderr )
1818data Configuration = Configuration
1919 { optPrintHelp :: Bool
2020 , optPrintVersion :: Bool
21− , optSha256 :: String
21+ , optSha256 :: Maybe String
2222 , optMaintainer :: [String]
2323 , optPlatform :: [String]
2424 , optHaddock :: Bool
@@ -31,7 +31,7 @@ defaultConfiguration :: Configuration
3131defaultConfiguration = Configuration
3232 { optPrintHelp = False
3333 , optPrintVersion = False
34− , optSha256 = ""
34+ , optSha256 = Nothing
3535 , optMaintainer = []
3636 , optPlatform = []
3737 , optHaddock = True
@@ -42,7 +42,7 @@ defaultConfiguration = Configuration
4242options :: [OptDescr (Configuration -> Configuration)]
4343options =
4444 [ Option "h" ["help"] (NoArg (\o -> o { optPrintHelp = True })) "show this help text"
45− , Option "" ["sha256"] (ReqArg (\x o -> o { optSha256 = x }) "HASH") "sha256 hash of source tarball"
45+ , Option "" ["sha256"] (ReqArg (\x o -> o { optSha256 = Just x }) "HASH") "sha256 hash of source tarball"
4646 , Option "m" ["maintainer"] (ReqArg (\x o -> o { optMaintainer = x : optMaintainer o }) "MAINTAINER") "maintainer of this package (may be specified multiple times)"
4747 , Option "p" ["platform"] (ReqArg (\x o -> o { optPlatform = x : optPlatform o }) "PLATFORM") "supported build platforms (may be specified multiple times)"
4848 , Option "" ["no-haddock"] (NoArg (\o -> o { optHaddock = False })) "don't run Haddock when building this package"
@@ -84,7 +84,9 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
8484 exitFailure
8585
8686 let packageId = package (packageDescription cabal)
87− sha <- if null (optSha256 cfg) then hashPackage packageId else return (optSha256 cfg)
87+ sha <- case optSha256 cfg of
88+ Just hash -> return hash
89+ Nothing -> hashPackage packageId
8890
8991 let deriv = (cabal2nix cabal) { sha256 = sha, runHaddock = optHaddock cfg, jailbreak = optJailbreak cfg }
9092 deriv' = deriv { metaSection = (metaSection deriv)