Skip to content

Commit 34fbe05

Browse files
committed
Fetch .tar.gz using nix-prefetch-url --unpack
Fetching `tar.gz` files is common enough of a use-case that warrants treating them specially by bypassing other fetching options and using `nix-prefetch-url --unpack` directly. This speeds up large tarballs as `cabal2nix` currently downloads them twice as the first fetch assumes an unpacked url and thus will fail.
1 parent 8e97f51 commit 34fbe05

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

cabal2nix/src/Distribution/Nixpkgs/Fetch.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Distribution.Nixpkgs.Fetch
1212
, derivKindFunction
1313
, FetchSubmodules(..)
1414
, UnpackArchive(..)
15+
, fetchPackedUrl
1516
, fetch
1617
, fetchWith
1718
) where
@@ -115,6 +116,9 @@ fromDerivationSource DerivationSource{..} =
115116
sourceCabalDir = "."
116117
}
117118

119+
fetchPackedUrl :: (String -> MaybeT IO a) -> Source -> IO (Maybe (DerivationSource, a))
120+
fetchPackedUrl f = runMaybeT . (fetchWith (False, DerivKindUrl UnpackArchive) >=> \(derivSource, file) -> (,) derivSource <$> f file)
121+
118122
-- | Fetch a source, trying any of the various nix-prefetch-* scripts.
119123
fetch :: forall a.
120124
FetchSubmodules

cabal2nix/src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,18 @@ fetchOrFromDB optHpack optSubmodules hackageDB src
9595
(msrc, pkgDesc) <- fromDB hackageDB . drop (length "cabal://") $ sourceUrl src
9696
return (msrc, False, pkgDesc)
9797
| otherwise = do
98-
r <- fetch optSubmodules (\dir -> cabalFromPath optHpack (dir </> sourceCabalDir src)) src
98+
let fetcher =
99+
if checkAny isPrefixOf ["http://", "https://"] (sourceUrl src) &&
100+
checkAny isSuffixOf ["tar.gz", ".tgz", ".zip"] (sourceUrl src)
101+
then fetchPackedUrl
102+
else fetch optSubmodules
103+
r <- fetcher (\dir -> cabalFromPath optHpack (dir </> sourceCabalDir src)) src
99104
case r of
100105
Nothing -> fail $ "Failed to fetch source. Does this source exist? " ++ show src
101106
Just (derivSource, (externalSource, ranHpack, pkgDesc)) ->
102107
return (derivSource <$ guard externalSource, ranHpack, pkgDesc)
108+
where
109+
checkAny f xs s = or $ (`f` s) <$> xs
103110

104111
loadHackageDB :: Maybe FilePath
105112
-- ^ The path to the Hackage database.

0 commit comments

Comments
 (0)