Skip to content

Commit b2ddb08

Browse files
committed
Fix subdir existence checks and add comments
1 parent 57e07c4 commit b2ddb08

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

lib/Distribution/Nixpkgs/Fetch.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ fetch :: forall a. (String -> MaybeT IO a) -- ^ This function is passed the
8181
-> Source -- ^ The source to fetch from.
8282
-> IO (Maybe (DerivationSource, a)) -- ^ The derivation source and the result of the processing function. Returns Nothing if the download failed.
8383
fetch f source = firstJust . map runMaybeT . (fetchLocal source :) $ map (\fetcher -> fetchWith fetcher source >>= process)
84-
[ (True, "git", ["--fetch-submodules"])
84+
[ (False, "url", [])
85+
, (True, "git", ["--fetch-submodules"])
8586
, (True, "hg", [])
8687
, (True, "svn", [])
8788
, (True, "bzr", [])
88-
, (False, "url", [])
8989
]
9090
where
9191
-- | Remove '/' from the end of the path. Nix doesn't accept paths that

lib/Stack2nix.hs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ module Stack2nix
88

99
import qualified Data.Text as T
1010
import Data.String (fromString)
11-
import Data.Maybe (fromMaybe)
1211

1312
import Control.Monad.Trans.Maybe
1413
import Control.Monad.IO.Class (liftIO)
15-
import Control.Monad (unless, forM)
14+
import Control.Monad (unless, forM, forM_)
1615
import Extra (unlessM)
1716

1817
import qualified Data.Map as M (fromListWith, toList)
1918
import System.FilePath ((<.>), (</>), takeDirectory, dropFileName)
20-
import System.Directory (createDirectoryIfMissing, doesDirectoryExist, doesFileExist, getCurrentDirectory)
19+
import System.Directory (createDirectoryIfMissing, doesDirectoryExist, doesFileExist)
2120
import System.IO (IOMode(..), openFile, hClose)
2221
import Data.Yaml (decodeFileEither)
2322

@@ -63,7 +62,7 @@ stackexpr args =
6362
=<< resolveSnapshot (argStackYaml args) value
6463

6564
stack2nix :: Args -> Stack -> IO NExpr
66-
stack2nix args stack@(Stack resolver compiler pkgs pkgFlags ghcOpts) =
65+
stack2nix args (Stack resolver compiler pkgs pkgFlags ghcOpts) =
6766
do let extraDeps = extraDeps2nix pkgs
6867
flags = flags2nix pkgFlags
6968
ghcOptions = ghcOptions2nix ghcOpts
@@ -148,7 +147,6 @@ writeDoc file doc =
148147
-- makeRelativeToCurrentDirectory
149148
packages2nix :: Args -> [Dependency] -> IO [(T.Text, Binding NExpr)]
150149
packages2nix args pkgs =
151-
do cwd <- getCurrentDirectory
152150
fmap concat . forM pkgs $ \case
153151
(LocalPath folder) ->
154152
do cabalFiles <- findCabalFiles (argHpackUse args) (dropFileName (argStackYaml args) </> folder)
@@ -165,11 +163,15 @@ packages2nix args pkgs =
165163
do hits <- forM subdirs $ \subdir -> liftIO $ cacheHits (argCacheFile args) url rev subdir
166164
if any null hits
167165
then
168-
fetch (return . cabalFromPath url rev subdirs)
166+
-- If any of the subdirs were missing we need to fetch the files and
167+
-- generate the bindings.
168+
fetch (cabalFromPath url rev subdirs)
169169
(Source url rev UnknownHash) >>= \case
170-
(Just (DerivationSource{..}, genBindings)) -> fromMaybe [] <$> runMaybeT (genBindings derivHash)
170+
(Just (DerivationSource{..}, genBindings)) -> genBindings derivHash
171171
_ -> return []
172172
else
173+
-- If the subdirs are all in the cache then the bindings should already be
174+
-- generated too.
173175
forM (concat hits) $ \( pkg, nix ) ->
174176
return (fromString pkg, fromString pkg $= mkPath False nix)
175177
_ -> return []
@@ -179,13 +181,19 @@ packages2nix args pkgs =
179181
-> String -- Revision
180182
-> [FilePath] -- Subdirs
181183
-> FilePath -- Local Directory
182-
-> String -- Sha256
183-
-> MaybeT IO [(T.Text, Binding NExpr)]
184-
cabalFromPath url rev subdirs dir sha256 =
185-
fmap concat . forM subdirs $ \subdir -> do
184+
-> MaybeT IO (String -> IO [(T.Text, Binding NExpr)])
185+
cabalFromPath url rev subdirs dir = do
186+
-- Check that all the subdirs exist if not this
187+
-- fail the MaybeT so that the next fetcher will be tried
188+
forM_ subdirs $ \subdir -> do
186189
let path = dir </> subdir
187190
d <- liftIO $ doesDirectoryExist path
188191
unless d $ fail ("not a directory: " ++ path)
192+
-- If we got this far we are confident we have downloaded
193+
-- with the right fetcher. Return an action that will
194+
-- be used to generate the bindings.
195+
return $ \sha256 -> fmap concat . forM subdirs $ \subdir -> do
196+
let path = dir </> subdir
189197
cabalFiles <- liftIO $ findCabalFiles (argHpackUse args) path
190198
forM cabalFiles $ \cabalFile -> do
191199
let pkg = cabalFilePkgName cabalFile

0 commit comments

Comments
 (0)