Skip to content

Commit

Permalink
Fetch .tar.gz using nix-prefetch-url --unpack
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pwm committed Nov 9, 2022
1 parent 8e97f51 commit 34fbe05
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cabal2nix/src/Distribution/Nixpkgs/Fetch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Distribution.Nixpkgs.Fetch
, derivKindFunction
, FetchSubmodules(..)
, UnpackArchive(..)
, fetchPackedUrl
, fetch
, fetchWith
) where
Expand Down Expand Up @@ -115,6 +116,9 @@ fromDerivationSource DerivationSource{..} =
sourceCabalDir = "."
}

fetchPackedUrl :: (String -> MaybeT IO a) -> Source -> IO (Maybe (DerivationSource, a))
fetchPackedUrl f = runMaybeT . (fetchWith (False, DerivKindUrl UnpackArchive) >=> \(derivSource, file) -> (,) derivSource <$> f file)

-- | Fetch a source, trying any of the various nix-prefetch-* scripts.
fetch :: forall a.
FetchSubmodules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,18 @@ fetchOrFromDB optHpack optSubmodules hackageDB src
(msrc, pkgDesc) <- fromDB hackageDB . drop (length "cabal://") $ sourceUrl src
return (msrc, False, pkgDesc)
| otherwise = do
r <- fetch optSubmodules (\dir -> cabalFromPath optHpack (dir </> sourceCabalDir src)) src
let fetcher =
if checkAny isPrefixOf ["http://", "https://"] (sourceUrl src) &&
checkAny isSuffixOf ["tar.gz", ".tgz", ".zip"] (sourceUrl src)
then fetchPackedUrl
else fetch optSubmodules
r <- fetcher (\dir -> cabalFromPath optHpack (dir </> sourceCabalDir src)) src
case r of
Nothing -> fail $ "Failed to fetch source. Does this source exist? " ++ show src
Just (derivSource, (externalSource, ranHpack, pkgDesc)) ->
return (derivSource <$ guard externalSource, ranHpack, pkgDesc)
where
checkAny f xs s = or $ (`f` s) <$> xs

loadHackageDB :: Maybe FilePath
-- ^ The path to the Hackage database.
Expand Down

0 comments on commit 34fbe05

Please sign in to comment.