@@ -16,9 +16,11 @@ import Prelude hiding ( readFile )
1616import Control.Arrow ( first , second )
1717import Control.Monad ( (>=>) , forM , when )
1818import Control.Monad.Writer ( join , lift )
19+ import Control.Monad.State ( MonadState , gets , modify )
1920
2021import Data.Char ( isAscii , isAlphaNum )
2122import qualified Data.HashMap.Lazy as M
23+ import qualified Data.HashMap.Strict as MS
2224import qualified Data.HashSet as S
2325import Data.List
2426import qualified Data.Map.Strict as Map
@@ -101,7 +103,7 @@ writeDerivation (drv@Derivation {inputs, name}) = do
101103
102104-- | Traverse the graph of inputDrvs to replace fixed output derivations with their fixed output hash.
103105-- this avoids propagating changes to their .drv when the output hash stays the same.
104- hashDerivationModulo :: (Framed e m , MonadFile m ) => Derivation -> m (Store. Digest 'Store.SHA256 )
106+ hashDerivationModulo :: (MonadNix e t f m , MonadState ( b , MS. HashMap Text Text ) m ) => Derivation -> m (Store. Digest 'Store.SHA256 )
105107hashDerivationModulo (Derivation {
106108 mFixed = Just (Store. SomeDigest (digest :: Store. Digest hashType )),
107109 outputs,
@@ -115,10 +117,14 @@ hashDerivationModulo (Derivation {
115117 <> " :" <> path
116118 outputsList -> throwError $ ErrorCall $ " This is weird. A fixed output drv should only have one output named 'out'. Got " ++ show outputsList
117119hashDerivationModulo drv@ (Derivation {inputs = (inputSrcs, inputDrvs)}) = do
118- inputsModulo <- Map. fromList <$> forM (Map. toList inputDrvs) (\ (path, outs) -> do
119- drv' <- readDerivation $ Text. unpack path
120- hash <- Store. encodeBase16 <$> hashDerivationModulo drv'
121- return (hash, outs)
120+ cache <- gets snd
121+ inputsModulo <- Map. fromList <$> forM (Map. toList inputDrvs) (\ (path, outs) ->
122+ case MS. lookup path cache of
123+ Just hash -> return (hash, outs)
124+ Nothing -> do
125+ drv' <- readDerivation $ Text. unpack path
126+ hash <- Store. encodeBase16 <$> hashDerivationModulo drv'
127+ return (hash, outs)
122128 )
123129 return $ Store. hash @ 'Store.SHA256 $ Text. encodeUtf8 $ unparseDrv (drv {inputs = (inputSrcs, inputsModulo)})
124130
@@ -214,7 +220,7 @@ derivationParser = do
214220 _ -> (Nothing , Flat )
215221
216222
217- defaultDerivationStrict :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
223+ defaultDerivationStrict :: forall e t f m b . ( MonadNix e t f m , MonadState ( b , MS. HashMap Text Text ) m ) => NValue t f m -> m (NValue t f m )
218224defaultDerivationStrict = fromValue @ (AttrSet (NValue t f m )) >=> \ s -> do
219225 (drv, ctx) <- runWithStringContextT' $ buildDerivationWithContext s
220226 drvName <- makeStorePathName $ name drv
@@ -242,13 +248,14 @@ defaultDerivationStrict = fromValue @(AttrSet (NValue t f m)) >=> \s -> do
242248 , env = if useJson drv then env drv else Map. union outputs' (env drv)
243249 }
244250
245- drvPath <- writeDerivation drv'
251+ drvPath <- pathToText <$> writeDerivation drv'
246252
247- -- TODO: memoize this result here.
248- -- _ <- hashDerivationModulo drv'
253+ -- Memoize here, as it may be our last chance in case of readonly stores.
254+ drvHash <- Store. encodeBase16 <$> hashDerivationModulo drv'
255+ modify (\ (a, b) -> (a, MS. insert drvPath drvHash b))
249256
250- let outputsWithContext = Map. mapWithKey (\ out path -> principledMakeNixStringWithSingletonContext path (StringContext (pathToText drvPath) (DerivationOutput out))) (outputs drv')
251- drvPathWithContext = principledMakeNixStringWithSingletonContext (pathToText drvPath) (StringContext (pathToText drvPath) AllOutputs )
257+ let outputsWithContext = Map. mapWithKey (\ out path -> principledMakeNixStringWithSingletonContext path (StringContext drvPath (DerivationOutput out))) (outputs drv')
258+ drvPathWithContext = principledMakeNixStringWithSingletonContext drvPath (StringContext drvPath AllOutputs )
252259 attrSet = M. map nvStr $ M. fromList $ (" drvPath" , drvPathWithContext): Map. toList outputsWithContext
253260 -- TODO: Add location information for all the entries.
254261 -- here --v
0 commit comments