@@ -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
@@ -100,7 +102,7 @@ writeDerivation (drv@Derivation {inputs, name}) = do
100102
101103-- | Traverse the graph of inputDrvs to replace fixed output derivations with their fixed output hash.
102104-- this avoids propagating changes to their .drv when the output hash stays the same.
103- hashDerivationModulo :: (Framed e m , MonadFile m ) => Derivation -> m (Store. Digest 'Store.SHA256 )
105+ hashDerivationModulo :: (MonadNix e t f m , MonadState ( b , MS. HashMap Text Text ) m ) => Derivation -> m (Store. Digest 'Store.SHA256 )
104106hashDerivationModulo (Derivation {
105107 mFixed = Just (Store. SomeDigest (digest :: Store. Digest hashType )),
106108 outputs,
@@ -114,10 +116,14 @@ hashDerivationModulo (Derivation {
114116 <> " :" <> path
115117 outputsList -> throwError $ ErrorCall $ " This is weird. A fixed output drv should only have one output named 'out'. Got " ++ show outputsList
116118hashDerivationModulo drv@ (Derivation {inputs = (inputSrcs, inputDrvs)}) = do
117- inputsModulo <- Map. fromList <$> forM (Map. toList inputDrvs) (\ (path, outs) -> do
118- drv' <- readDerivation $ Text. unpack path
119- hash <- Store. encodeBase16 <$> hashDerivationModulo drv'
120- return (hash, outs)
119+ cache <- gets snd
120+ inputsModulo <- Map. fromList <$> forM (Map. toList inputDrvs) (\ (path, outs) ->
121+ case MS. lookup path cache of
122+ Just hash -> return (hash, outs)
123+ Nothing -> do
124+ drv' <- readDerivation $ Text. unpack path
125+ hash <- Store. encodeBase16 <$> hashDerivationModulo drv'
126+ return (hash, outs)
121127 )
122128 return $ Store. hash @ 'Store.SHA256 $ Text. encodeUtf8 $ unparseDrv (drv {inputs = (inputSrcs, inputsModulo)})
123129
@@ -220,7 +226,7 @@ derivationParser = do
220226 _ -> (Nothing , Flat )
221227
222228
223- defaultDerivationStrict :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
229+ 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 )
224230defaultDerivationStrict = fromValue @ (AttrSet (NValue t f m )) >=> \ s -> do
225231 (drv, ctx) <- runWithStringContextT' $ buildDerivationWithContext s
226232 drvName <- makeStorePathName $ name drv
@@ -248,13 +254,14 @@ defaultDerivationStrict = fromValue @(AttrSet (NValue t f m)) >=> \s -> do
248254 , env = if useJson drv then env drv else Map. union outputs' (env drv)
249255 }
250256
251- drvPath <- writeDerivation drv'
257+ drvPath <- pathToText <$> writeDerivation drv'
252258
253- -- TODO: memoize this result here.
254- -- _ <- hashDerivationModulo drv'
259+ -- Memoize here, as it may be our last chance in case of readonly stores.
260+ drvHash <- Store. encodeBase16 <$> hashDerivationModulo drv'
261+ modify (\ (a, b) -> (a, MS. insert drvPath drvHash b))
255262
256- let outputsWithContext = Map. mapWithKey (\ out path -> principledMakeNixStringWithSingletonContext path (StringContext (pathToText drvPath) (DerivationOutput out))) (outputs drv')
257- drvPathWithContext = principledMakeNixStringWithSingletonContext (pathToText drvPath) (StringContext (pathToText drvPath) AllOutputs )
263+ let outputsWithContext = Map. mapWithKey (\ out path -> principledMakeNixStringWithSingletonContext path (StringContext drvPath (DerivationOutput out))) (outputs drv')
264+ drvPathWithContext = principledMakeNixStringWithSingletonContext drvPath (StringContext drvPath AllOutputs )
258265 attrSet = M. map nvStr $ M. fromList $ (" drvPath" , drvPathWithContext): Map. toList outputsWithContext
259266 -- TODO: Add location information for all the entries.
260267 -- here --v
0 commit comments