From 34fbe05891588eaa5ba747c7f459b95c21efb492 Mon Sep 17 00:00:00 2001 From: Zsolt Szende Date: Tue, 8 Nov 2022 21:48:49 +0000 Subject: [PATCH] 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. --- cabal2nix/src/Distribution/Nixpkgs/Fetch.hs | 4 ++++ .../Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cabal2nix/src/Distribution/Nixpkgs/Fetch.hs b/cabal2nix/src/Distribution/Nixpkgs/Fetch.hs index 1f2891e0c..e23cf2206 100644 --- a/cabal2nix/src/Distribution/Nixpkgs/Fetch.hs +++ b/cabal2nix/src/Distribution/Nixpkgs/Fetch.hs @@ -12,6 +12,7 @@ module Distribution.Nixpkgs.Fetch , derivKindFunction , FetchSubmodules(..) , UnpackArchive(..) + , fetchPackedUrl , fetch , fetchWith ) where @@ -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 diff --git a/cabal2nix/src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs b/cabal2nix/src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs index 4797bef20..63e5b6157 100644 --- a/cabal2nix/src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs +++ b/cabal2nix/src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs @@ -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.