@@ -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]