which 0.1.0.1 → 0.2
raw patch · 3 files changed
+30/−13 lines, 3 filesdep ~basedep ~template-haskell
Dependency ranges changed: base, template-haskell
Files
- ChangeLog.md +4/−0
- src/System/Which.hs +25/−12
- which.cabal +1/−1
ChangeLog.md view
@@ -1,5 +1,9 @@ # Revision history for which +## 0.2++* Allow results outside of the nix store (use `staticWhichNix` for the old behavior, which requires results to be in `/nix/store`)+ ## 0.1.0.1 * Loosen version bounds. Support GHC 8.8 and 8.10
src/System/Which.hs view
@@ -1,5 +1,6 @@-{-# LANGUAGE OverloadedStrings, TemplateHaskell #-}-module System.Which where+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TemplateHaskell #-}+module System.Which (which, staticWhich, staticWhichNix) where import qualified Shelly as Sh import qualified Data.Text as T@@ -7,24 +8,36 @@ import Data.Monoid ((<>)) import Data.List (isPrefixOf) --- | Determine which executable would run if the given path were executed, or return Nothing if a suitable executable cannot be found+-- | Determine which executable would run if the given path were+-- executed, or return Nothing if a suitable executable cannot be+-- found which :: FilePath -> IO (Maybe FilePath) which f = fmap (fmap (T.unpack . Sh.toTextIgnore)) $ Sh.shelly $ Sh.which $ Sh.fromText $ T.pack f --- | Run `which` at compile time, and substitute the full path to the executable.------ This is useful in NixOS to ensure that the resulting executable contains the dependency in its closure and that it refers to the same version at run time as at compile time-staticWhich :: FilePath -> Q Exp-staticWhich f = do+staticWhichImpl :: (FilePath -> Maybe String) -> FilePath -> Q Exp+staticWhichImpl test f = do mf' <- runIO $ which f case mf' of Nothing -> compileError $ "Could not find executable for " <> show f- Just f'- | "/nix/store/" `isPrefixOf` f' -> [| f' |]- | otherwise -> compileError $ "Path to executable " <> show f <> " was found in " <> show f' <> " which is not in /nix/store. Be sure to add the relevant package to 'backendTools' in default.nix."-+ Just f' -> case test f' of+ Just err -> compileError err+ Nothing -> [| f' |] where compileError msg' = do let msg = "staticWhich: " <> msg' reportError msg [| error msg |]++-- | Run `which` at compile time, and substitute the full path to the executable.+staticWhich :: FilePath -> Q Exp+staticWhich = staticWhichImpl (const Nothing)++-- A variant of 'staticWhich' that ensures the executable is in the nix store.+-- This is useful in NixOS to ensure that the resulting executable+-- contains the dependency in its closure and that it refers to the+-- same version at run time as at compile time+staticWhichNix :: FilePath -> Q Exp+staticWhichNix = staticWhichImpl $ \x ->+ if "/nix/store/" `isPrefixOf` x+ then Nothing+ else Just $ "Executable was found in " <> x <> " which is not in /nix/store."
which.cabal view
@@ -1,5 +1,5 @@ name: which-version: 0.1.0.1+version: 0.2 license: BSD3 license-file: LICENSE author: Obsidian Systems LLC