on master help sign in
command: Mark extra shell features as unstable, use shellPackages
commit323e52ea3f2fa4c2fff6d83229417cc8ab583c11 authorElliot Cameron <eacameron@gmail.com> dateFri 24 Jan 2020, 00:35 (7 years ago) parent12ce323
3 files changed+27−13
default.nix+1−1 lib/command/src/Obelisk/Command/Project.hs+17−7 lib/command/src/Obelisk/Command/Run.hs+9−5
default.nix+1−1
@@ -351,7 +351,7 @@ in rec {
351351 ios = self.__iosWithConfig (self.base + "/config");
352352
353353 passthru = {
354− self = allConfig;
354+ __unstable__.self = allConfig;
355355 inherit (self)
356356 staticFilesImpure processedStatic
357357 __iosWithConfig __androidWithConfig
lib/command/src/Obelisk/Command/Project.hs+17−7
@@ -20,11 +20,17 @@ import Control.Lens ((.~), (?~), (<&>))
2020import Control.Monad
2121import Control.Monad.IO.Class (liftIO)
2222import Control.Monad.State
23+import qualified Data.Aeson as Json
2324import Data.Bits
25+import qualified Data.ByteString.Lazy as BSL
2426import Data.Default (def)
2527import Data.Function ((&), on)
2628import Data.List (isInfixOf)
29+import Data.Map (Map)
30+import Data.Text (Text)
2731import qualified Data.Text as T
32+import Data.Text.Encoding (decodeUtf8)
33+import Data.Traversable (for)
2834import System.Directory
2935import System.Environment (lookupEnv)
3036import System.FilePath
@@ -240,8 +246,6 @@ filePermissionIsSafe s umask = not fileWorldWritable && fileGroupWritable <= uma
240246 fileGroupWritable = fileMode s .&. 0o020 == 0o020
241247 umaskGroupWritable = umask .&. 0o020 == 0
242248
243−-- nix-shell -E '{ root, shell-pkgs }: ((import root {}).passthru.self.extend (self: super: { shells-ghc = shell-pkgs; })).project.shells.ghc' --arg root ./. --arg shell-pkgs '["backend"]'
244−
245249-- | Nix syntax requires relative paths to be prefixed by @./@ or
246250-- @../@. This will make a 'FilePath' that can be embedded in a Nix
247251-- expression.
@@ -249,9 +253,9 @@ toNixPath :: FilePath -> FilePath
249253toNixPath root | "/" `isInfixOf` root = root
250254 | otherwise = "./" <> root
251255
252−
253−nixShellWithPkgs :: MonadObelisk m => FilePath -> Bool -> [String] -> Maybe String -> m ()
254−nixShellWithPkgs root isPure packageNames command = do
256+nixShellWithPkgs :: MonadObelisk m => FilePath -> Bool -> Map Text FilePath -> Maybe String -> m ()
257+nixShellWithPkgs root isPure packageNamesAndPaths command = do
258+ packageNamesAndAbsPaths <- liftIO $ for packageNamesAndPaths makeAbsolute
255259 nixpkgsPath <- fmap T.strip $ readProcessAndLogStderr Debug $ setCwd (Just root) $
256260 proc nixExePath ["eval", "(import .obelisk/impl {}).nixpkgs.path"]
257261 nixRemote <- liftIO $ lookupEnv "NIX_REMOTE"
@@ -260,9 +264,15 @@ nixShellWithPkgs root isPure packageNames command = do
260264 & nixShellConfig_pure .~ isPure
261265 & nixShellConfig_common . nixCmdConfig_target .~ (def
262266 & target_path .~ Nothing
263− & target_expr ?~ "{ root, shell-pkgs }: ((import root {}).passthru.self.extend (self: super: { shells-ghc = shell-pkgs; })).project.shells.ghc"
267+ & target_expr ?~
268+ "{root, pkgs}: ((import root {}).passthru.__unstable__.self.extend (self: super: {\
269+ \shellPackages = builtins.fromJSON pkgs; })\
270+ \).project.shells.ghc"
264271 )
265− & nixShellConfig_common . nixCmdConfig_args .~ [rawArg "root" $ toNixPath root, rawArg "shell-pkgs" ("[" <> unwords (map show packageNames) <> "]")]
272+ & nixShellConfig_common . nixCmdConfig_args .~
273+ [ rawArg "root" $ toNixPath root
274+ , strArg "pkgs" (T.unpack $ decodeUtf8 $ BSL.toStrict $ Json.encode packageNamesAndAbsPaths)
275+ ]
266276 & nixShellConfig_run .~ (command <&> \c -> mconcat
267277 [ "export NIX_PATH=nixpkgs=", T.unpack nixpkgsPath, "; "
268278 , maybe "" (\v -> "export NIX_REMOTE=" <> v <> "; ") nixRemote
lib/command/src/Obelisk/Command/Run.hs+9−5
@@ -6,6 +6,7 @@
66{-# LANGUAGE TupleSections #-}
77module Obelisk.Command.Run where
88
9+import Control.Arrow ((&&&))
910import Control.Exception (Exception, bracket)
1011import Control.Monad (filterM, unless)
1112import Control.Monad.Except (runExceptT, throwError)
@@ -16,8 +17,11 @@ import Data.Either
1617import Data.Foldable (for_, toList)
1718import Data.List.Extra (dropPrefix)
1819import qualified Data.List.NonEmpty as NE
20+import Data.Map (Map)
21+import qualified Data.Map as Map
1922import Data.Maybe
2023import qualified Data.Set as Set
24+import Data.Text (Text)
2125import qualified Data.Text as T
2226import Data.Traversable (for)
2327import Debug.Trace (trace)
@@ -243,6 +247,9 @@ parsePackagesOrFail dirs = do
243247
244248 pure packageInfos
245249
250+packageInfoToNamePathMap :: [CabalPackageInfo] -> Map Text FilePath
251+packageInfoToNamePathMap = Map.fromList . map (_cabalPackageInfo_packageName &&& _cabalPackageInfo_packageRoot)
252+
246253-- | Create ghci configuration to load the given packages
247254withGhciScript
248255 :: MonadObelisk m
@@ -285,9 +292,7 @@ runGhciRepl
285292runGhciRepl packages dotGhci = withProjectRoot "." $ \root ->
286293 -- NOTE: We do *not* want to use $(staticWhich "ghci") here because we need the
287294 -- ghc that is provided by the shell in the user's project.
288− nixShellWithPkgs root True packageNames $ Just $ "ghci " <> makeBaseGhciOptions dotGhci -- TODO: Shell escape
289− where
290− packageNames = map (T.unpack . _cabalPackageInfo_packageName) packages
295+ nixShellWithPkgs root True (packageInfoToNamePathMap packages) $ Just $ "ghci " <> makeBaseGhciOptions dotGhci -- TODO: Shell escape
291296
292297-- | Run ghcid
293298runGhcid
@@ -297,9 +302,8 @@ runGhcid
297302 -> Maybe String -- ^ Optional command to run at every reload
298303 -> m ()
299304runGhcid dotGhci packages mcmd = withProjectRoot "." $ \root ->
300− nixShellWithPkgs root True packageNames (Just $ unwords $ ghcidExePath : opts) -- TODO: Shell escape
305+ nixShellWithPkgs root True (packageInfoToNamePathMap packages) (Just $ unwords $ ghcidExePath : opts) -- TODO: Shell escape
301306 where
302− packageNames = map (T.unpack . _cabalPackageInfo_packageName) packages
303307 opts =
304308 [ "-W"
305309 --TODO: The decision of whether to use -fwarn-redundant-constraints should probably be made by the user