git.iko.soy/typeable/cabal2nix
on master help sign in
Merge pull request #183 from cheecheeo/140_uncache_unknown_hashes

Don't store hashes taken from the command line.

commite5e4464893b2b964424077e4fe7ef1a76e6d1173 authorPeter Simons <simons@cryp.to> dateWed 22 Jul 2015, 22:01 (11 years ago) parent3606ad42d6ecbe
4 files changed+49−24
src/Cabal2Nix/Package.hs+26−16 src/Distribution/Nixpkgs/Fetch.hs+20−6 src/cabal2nix.hs+1−1 src/hackage2nix.hs+2−1
src/Cabal2Nix/Package.hs+26−16
@@ -74,33 +74,43 @@ hashCachePath pid = do
7474 createDirectoryIfMissing True cacheDir
7575 return $ cacheDir </> pid <.> "sha256"
7676
77−sourceFromHackage :: Maybe String -> String -> IO DerivationSource
77+sourceFromHackage :: Hash -> String -> IO DerivationSource
7878sourceFromHackage optHash pkgId = do
7979 cacheFile <- hashCachePath pkgId
80− let cachedHash = MaybeT $ maybe (readFileMay cacheFile) (return . Just) optHash
81− url = "mirror://hackage/" ++ pkgId ++ ".tar.gz"
80+ cachedHash <-
81+ case optHash of
82+ Certain h -> return . Certain $ h
83+ Guess h -> return . Guess $ h
84+ _ -> fmap (maybe UnknownHash Certain) . readFileMay $ cacheFile
85+ let url = "mirror://hackage/" ++ pkgId ++ ".tar.gz"
8286
8387 -- Use the cached hash (either from cache file or given on cmdline via sha256 opt)
8488 -- if available, otherwise download from hackage to compute hash.
85− maybeHash <- runMaybeT $ cachedHash <|> derivHash . fst <$> fetchWith (False, "url", []) (Source url "" Nothing)
86− case maybeHash of
87− Just hash ->
89+ case cachedHash of
90+ Guess hash -> return $ DerivationSource "url" url "" hash
91+ Certain hash ->
8892 -- We need to force the hash here. If we didn't do this, then when reading the
8993 -- hash from the cache file, the cache file will still be open for reading
9094 -- (because lazy io) when writeFile opens the file again for writing. By forcing
9195 -- the hash here, we ensure that the file is closed before opening it again.
9296 seq (length hash) $
9397 DerivationSource "url" url "" hash <$ writeFile cacheFile hash
94− Nothing -> do
95− hPutStr stderr $ unlines
96− [ "*** cannot compute hash. (Not a hackage project?)"
97− , " If your project is not on hackage, please supply the path to the root directory of"
98− , " the project, not to the cabal file."
99− , ""
100− , " If your project is on hackage but you still want to specify the hash manually, you"
101− , " can use the --sha256 option."
102− ]
103− exitFailure
98+ UnknownHash -> do
99+ maybeHash <- runMaybeT (derivHash . fst <$> (fetchWith (False, "url", []) (Source url "" UnknownHash)))
100+ case maybeHash of
101+ Just hash ->
102+ seq (length hash) $
103+ DerivationSource "url" url "" hash <$ writeFile cacheFile hash
104+ Nothing -> do
105+ hPutStr stderr $ unlines
106+ [ "*** cannot compute hash. (Not a hackage project?)"
107+ , " If your project is not on hackage, please supply the path to the root directory of"
108+ , " the project, not to the cabal file."
109+ , ""
110+ , " If your project is on hackage but you still want to specify the hash manually, you"
111+ , " can use the --sha256 option."
112+ ]
113+ exitFailure
104114
105115showPackageIdentifier :: Cabal.GenericPackageDescription -> String
106116showPackageIdentifier pkgDesc = name ++ "-" ++ showVersion version where
src/Distribution/Nixpkgs/Fetch.hs+20−6
@@ -4,6 +4,7 @@
44{-# LANGUAGE DeriveGeneric #-}
55module Distribution.Nixpkgs.Fetch
66 ( Source(..)
7+ , Hash(..)
78 , DerivationSource(..), fromDerivationSource
89 , fetch
910 , fetchWith
@@ -27,11 +28,24 @@ data Source = Source
2728 { sourceUrl :: String -- ^ URL to fetch from.
2829 , sourceRevision :: String -- ^ Revision to use. For protocols where this doesn't make sense (such as HTTP), this
2930 -- should be the empty string.
30− , sourceHash :: Maybe String -- ^ The expected hash of the source, if available.
31+ , sourceHash :: Hash -- ^ The expected hash of the source, if available.
3132 } deriving (Show, Eq, Ord, Generic)
3233
3334instance NFData Source where rnf = genericRnf
3435
36+data Hash = Certain String | Guess String | UnknownHash
37+ deriving (Show, Eq, Ord, Generic)
38+
39+instance NFData Hash where rnf = genericRnf
40+
41+isUnknown :: Hash -> Bool
42+isUnknown UnknownHash = True
43+isUnknown _ = False
44+
45+hashToList :: Hash -> [String]
46+hashToList (Certain s) = [s]
47+hashToList _ = []
48+
3549-- | A source for a derivation. It always needs a hash and also has a protocol attached to it (url, git, svn, ...).
3650-- A @DerivationSource@ also always has it's revision fully resolved (not relative revisions like @master@, @HEAD@, etc).
3751data DerivationSource = DerivationSource
@@ -45,7 +59,7 @@ data DerivationSource = DerivationSource
4559instance NFData DerivationSource where rnf = genericRnf
4660
4761fromDerivationSource :: DerivationSource -> Source
48−fromDerivationSource DerivationSource{..} = Source derivUrl derivRevision $ Just derivHash
62+fromDerivationSource DerivationSource{..} = Source derivUrl derivRevision $ Certain derivHash
4963
5064-- | Fetch a source, trying any of the various nix-prefetch-* scripts.
5165fetch :: forall a. (String -> MaybeT IO a) -- ^ This function is passed the output path name as an argument.
@@ -82,7 +96,7 @@ fetch f = runMaybeT . fetchers where
8296 localArchive :: FilePath -> MaybeT IO (DerivationSource, a)
8397 localArchive path = do
8498 absolutePath <- liftIO $ canonicalizePath path
85− unpacked <- snd <$> fetchWith (False, "zip", []) (Source ("file://" ++ absolutePath) "" Nothing)
99+ unpacked <- snd <$> fetchWith (False, "zip", []) (Source ("file://" ++ absolutePath) "" UnknownHash)
86100 process (DerivationSource "" absolutePath "" "", unpacked)
87101
88102 process :: (DerivationSource, FilePath) -> MaybeT IO (DerivationSource, a)
@@ -91,7 +105,7 @@ fetch f = runMaybeT . fetchers where
91105-- | Like 'fetch', but allows to specify which script to use.
92106fetchWith :: (Bool, String, [String]) -> Source -> MaybeT IO (DerivationSource, FilePath)
93107fetchWith (supportsRev, kind, addArgs) source = do
94− unless ((sourceRevision source /= "") || isNothing (sourceHash source) || not supportsRev) $
108+ unless ((sourceRevision source /= "") || isUnknown (sourceHash source) || not supportsRev) $
95109 liftIO (hPutStrLn stderr "** need a revision for VCS when the hash is given. skipping.") >> mzero
96110
97111 MaybeT $ liftIO $ do
@@ -114,7 +128,7 @@ fetchWith (supportsRev, kind, addArgs) source = do
114128 script = "nix-prefetch-" ++ kind
115129
116130 args :: [String]
117− args = addArgs ++ sourceUrl source : [ sourceRevision source | supportsRev ] ++ maybeToList (sourceHash source)
131+ args = addArgs ++ sourceUrl source : [ sourceRevision source | supportsRev ] ++ hashToList (sourceHash source)
118132
119133 processOutputWithRev :: [String] -> IO (DerivationSource, FilePath)
120134 processOutputWithRev [rev,hash,path] = return (DerivationSource kind (sourceUrl source) (extractRevision rev) hash, path)
@@ -136,7 +150,7 @@ fetchWith (supportsRev, kind, addArgs) source = do
136150
137151 processOutputSwitch :: [String] -> IO (DerivationSource, FilePath)
138152 processOutputSwitch
139− | supportsRev && isNothing (sourceHash source) = processOutputWithRev
153+ | supportsRev && isUnknown (sourceHash source) = processOutputWithRev
140154 | otherwise = processOutput
141155
142156stripPrefix :: Eq a => [a] -> [a] -> [a]
src/cabal2nix.hs+1−1
@@ -78,7 +78,7 @@ main :: IO ()
7878main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
7979 cfg <- execParser pinfo
8080
81− pkg <- getPackage (optHackageDb cfg) $ Source (optUrl cfg) (fromMaybe "" (optRevision cfg)) (optSha256 cfg)
81+ pkg <- getPackage (optHackageDb cfg) $ Source (optUrl cfg) (fromMaybe "" (optRevision cfg)) (maybe UnknownHash Guess (optSha256 cfg))
8282
8383 let flags = readFlagList (optFlags cfg)
8484
src/hackage2nix.hs+2−1
@@ -41,6 +41,7 @@ import Data.Monoid
4141import Data.Set ( Set )
4242import qualified Data.Set as Set
4343import Distribution.Compiler
44+import Distribution.Nixpkgs.Fetch
4445import Distribution.Nixpkgs.Haskell
4546import Distribution.Nixpkgs.PackageMap
4647import Distribution.Nixpkgs.Util.PrettyPrinting hiding ( attr, (<>) )
@@ -156,7 +157,7 @@ generatePackageSet config hackage nixpkgs = do
156157 putStrLn ""
157158 pkgs <- flip parMapM (Map.toAscList db) $ \(name, vs) -> do
158159 defs <- forM (Set.toAscList vs) $ \pkgversion -> do
159− srcSpec <- liftIO $ sourceFromHackage Nothing (name ++ "-" ++ display pkgversion)
160+ srcSpec <- liftIO $ sourceFromHackage UnknownHash (name ++ "-" ++ display pkgversion)
160161 let Just cabalFileHash = lookup "x-cabal-file-hash" (customFieldsPD (packageDescription descr))
161162
162163 -- TODO: Include list of broken dependencies in the generated output.