diff --git a/cabal-install/Distribution/Client/CmdRun.hs b/cabal-install/Distribution/Client/CmdRun.hs
index b84cdfc85b40f5b6b3b7ffdf66b9c26adf564839..2859fce84a39eeb5d72eb45b476c9f3c496d9cd2 100644
--- a/cabal-install/Distribution/Client/CmdRun.hs
+++ b/cabal-install/Distribution/Client/CmdRun.hs
@@ -286,7 +286,9 @@ runAction (configFlags, configExFlags, installFlags, haddockFlags)
       emptyProgramInvocation {
         progInvokePath  = exePath,
         progInvokeArgs  = args,
-        progInvokeEnv   = dataDirsEnvironmentForPlan elaboratedPlan
+        progInvokeEnv   = dataDirsEnvironmentForPlan
+                            (distDirLayout baseCtx)
+                            elaboratedPlan
       }
     
     handleDoesNotExist () (removeDirectoryRecursive tempDir)
diff --git a/cabal-install/Distribution/Client/ProjectBuilding.hs b/cabal-install/Distribution/Client/ProjectBuilding.hs
index 0b6b74b03b2dc7d2c1cf193ed5836ded09fa6d12..702b04fb8238330807d4fb4d8044457847deed6f 100644
--- a/cabal-install/Distribution/Client/ProjectBuilding.hs
+++ b/cabal-install/Distribution/Client/ProjectBuilding.hs
@@ -910,7 +910,7 @@ buildAndInstallUnpackedPackage :: Verbosity
                                -> FilePath -> FilePath
                                -> IO BuildResult
 buildAndInstallUnpackedPackage verbosity
-                               DistDirLayout{distTempDirectory}
+                               distDirLayout@DistDirLayout{distTempDirectory}
                                storeDirLayout@StoreDirLayout {
                                  storePackageDBStack
                                }
@@ -1086,7 +1086,8 @@ buildAndInstallUnpackedPackage verbosity
     copyFlags destdir _ = setupHsCopyFlags pkg pkgshared verbosity
                                            builddir destdir
 
-    scriptOptions = setupHsScriptOptions rpkg plan pkgshared srcdir builddir
+    scriptOptions = setupHsScriptOptions rpkg plan pkgshared
+                                         distDirLayout srcdir builddir
                                          isParallelBuild cacheLock
 
     setup :: CommandUI flags -> (Version -> flags) -> IO ()
@@ -1099,7 +1100,7 @@ buildAndInstallUnpackedPackage verbosity
           verbosity
           scriptOptions
             { useLoggingHandle     = mLogFileHandle
-            , useExtraEnvOverrides = dataDirsEnvironmentForPlan plan }
+            , useExtraEnvOverrides = dataDirsEnvironmentForPlan distDirLayout plan }
           (Just (elabPkgDescription pkg))
           cmd flags args
 
@@ -1359,7 +1360,7 @@ buildInplaceUnpackedPackage verbosity
                        setupHsHaddockArgs pkg
 
     scriptOptions    = setupHsScriptOptions rpkg plan pkgshared
-                                            srcdir builddir
+                                            distDirLayout srcdir builddir
                                             isParallelBuild cacheLock
 
     setupInteractive :: CommandUI flags
diff --git a/cabal-install/Distribution/Client/ProjectPlanning.hs b/cabal-install/Distribution/Client/ProjectPlanning.hs
index 3f3a44160e850239d0ae667e24d0b9073b0a8039..2f85dca3cdc21c7a5dc2d1b6d4ad0ba925525ffa 100644
--- a/cabal-install/Distribution/Client/ProjectPlanning.hs
+++ b/cabal-install/Distribution/Client/ProjectPlanning.hs
@@ -3108,6 +3108,7 @@ legacyCustomSetupPkgs compiler (Platform _ os) =
 setupHsScriptOptions :: ElaboratedReadyPackage
                      -> ElaboratedInstallPlan
                      -> ElaboratedSharedConfig
+                     -> DistDirLayout
                      -> FilePath
                      -> FilePath
                      -> Bool
@@ -3116,7 +3117,7 @@ setupHsScriptOptions :: ElaboratedReadyPackage
 -- TODO: Fix this so custom is a separate component.  Custom can ALWAYS
 -- be a separate component!!!
 setupHsScriptOptions (ReadyPackage elab@ElaboratedConfiguredPackage{..})
-                     plan ElaboratedSharedConfig{..} srcdir builddir
+                     plan ElaboratedSharedConfig{..} distdir srcdir builddir
                      isParallelBuild cacheLock =
     SetupScriptOptions {
       useCabalVersion          = thisVersion elabSetupScriptCliVersion,
@@ -3135,7 +3136,7 @@ setupHsScriptOptions (ReadyPackage elab@ElaboratedConfiguredPackage{..})
       useLoggingHandle         = Nothing, -- this gets set later
       useWorkingDir            = Just srcdir,
       useExtraPathEnv          = elabExeDependencyPaths elab,
-      useExtraEnvOverrides     = dataDirsEnvironmentForPlan plan,
+      useExtraEnvOverrides     = dataDirsEnvironmentForPlan distdir plan,
       useWin32CleanHack        = False,   --TODO: [required eventually]
       forceExternalSetupMethod = isParallelBuild,
       setupCacheLock           = Just cacheLock,
diff --git a/cabal-install/Distribution/Client/ProjectPlanning/Types.hs b/cabal-install/Distribution/Client/ProjectPlanning/Types.hs
index 503bf430b73fbb37a877612fe971dade699644d2..4583f2ae2fc891aece635cdadcdaad8ff685a36f 100644
--- a/cabal-install/Distribution/Client/ProjectPlanning/Types.hs
+++ b/cabal-install/Distribution/Client/ProjectPlanning/Types.hs
@@ -378,12 +378,13 @@ elabRequiresRegistration elab =
 -- | Construct the environment needed for the data files to work.
 -- This consists of a separate @*_datadir@ variable for each
 -- inplace package in the plan.
-dataDirsEnvironmentForPlan :: ElaboratedInstallPlan
+dataDirsEnvironmentForPlan :: DistDirLayout
+                           -> ElaboratedInstallPlan
                            -> [(String, Maybe FilePath)]
-dataDirsEnvironmentForPlan = catMaybes
+dataDirsEnvironmentForPlan distDirLayout = catMaybes
                            . fmap (InstallPlan.foldPlanPackage
                                (const Nothing)
-                               dataDirEnvVarForPackage)
+                               (dataDirEnvVarForPackage distDirLayout))
                            . InstallPlan.toList
 
 -- | Construct an environment variable that points
@@ -393,19 +394,27 @@ dataDirsEnvironmentForPlan = catMaybes
 --   for inplace packages.
 -- * 'Nothing' for packages installed in the store (the path was
 --   already included in the package at install/build time).
--- * The other cases are not handled yet. See below.
-dataDirEnvVarForPackage :: ElaboratedConfiguredPackage
+dataDirEnvVarForPackage :: DistDirLayout
+                        -> ElaboratedConfiguredPackage
                         -> Maybe (String, Maybe FilePath)
-dataDirEnvVarForPackage pkg =
-  case (elabBuildStyle pkg, elabPkgSourceLocation pkg)
-  of (BuildAndInstall, _) -> Nothing
-     (BuildInplaceOnly, LocalUnpackedPackage path) -> Just
-       (pkgPathEnvVar (elabPkgDescription pkg) "datadir",
-        Just $ path </> dataDir (elabPkgDescription pkg))
-     -- TODO: handle the other cases for PackageLocation.
-     -- We will only need this when we add support for
-     -- remote/local tarballs.
-     (BuildInplaceOnly, _) -> Nothing
+dataDirEnvVarForPackage distDirLayout pkg =
+  case elabBuildStyle pkg
+  of BuildAndInstall -> Nothing
+     BuildInplaceOnly -> Just
+       ( pkgPathEnvVar (elabPkgDescription pkg) "datadir"
+       , Just $ srcPath (elabPkgSourceLocation pkg)
+            </> dataDir (elabPkgDescription pkg))
+  where
+    srcPath (LocalUnpackedPackage path) = path
+    srcPath (LocalTarballPackage _path) = unpackedPath
+    srcPath (RemoteTarballPackage _uri _localTar) = unpackedPath
+    srcPath (RepoTarballPackage _repo _packageId _localTar) = unpackedPath
+    srcPath (RemoteSourceRepoPackage _sourceRepo (Just localCheckout)) = localCheckout
+    -- TODO: see https://github.com/haskell/cabal/wiki/Potential-Refactors#unresolvedpkgloc
+    srcPath (RemoteSourceRepoPackage _sourceRepo Nothing) = error
+      "calling dataDirEnvVarForPackage on a not-downloaded repo is an error"
+    unpackedPath =
+      distUnpackedSrcDirectory distDirLayout $ elabPkgSourceId pkg
 
 instance Package ElaboratedConfiguredPackage where
   packageId = elabPkgSourceId