@@ -16,9 +16,11 @@ import Prelude hiding ( readFile )
16
16
import Control.Arrow ( first , second )
17
17
import Control.Monad ( (>=>) , forM , when )
18
18
import Control.Monad.Writer ( join , lift )
19
+ import Control.Monad.State ( MonadState , gets , modify )
19
20
20
21
import Data.Char ( isAscii , isAlphaNum )
21
22
import qualified Data.HashMap.Lazy as M
23
+ import qualified Data.HashMap.Strict as MS
22
24
import qualified Data.HashSet as S
23
25
import Data.List
24
26
import qualified Data.Map.Strict as Map
@@ -101,7 +103,7 @@ writeDerivation (drv@Derivation {inputs, name}) = do
101
103
102
104
-- | Traverse the graph of inputDrvs to replace fixed output derivations with their fixed output hash.
103
105
-- 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 )
105
107
hashDerivationModulo (Derivation {
106
108
mFixed = Just (Store. SomeDigest (digest :: Store. Digest hashType )),
107
109
outputs,
@@ -115,10 +117,14 @@ hashDerivationModulo (Derivation {
115
117
<> " :" <> path
116
118
outputsList -> throwError $ ErrorCall $ " This is weird. A fixed output drv should only have one output named 'out'. Got " ++ show outputsList
117
119
hashDerivationModulo 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)
122
128
)
123
129
return $ Store. hash @ 'Store.SHA256 $ Text. encodeUtf8 $ unparseDrv (drv {inputs = (inputSrcs, inputsModulo)})
124
130
@@ -214,7 +220,7 @@ derivationParser = do
214
220
_ -> (Nothing , Flat )
215
221
216
222
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 )
218
224
defaultDerivationStrict = fromValue @ (AttrSet (NValue t f m )) >=> \ s -> do
219
225
(drv, ctx) <- runWithStringContextT' $ buildDerivationWithContext s
220
226
drvName <- makeStorePathName $ name drv
@@ -242,13 +248,14 @@ defaultDerivationStrict = fromValue @(AttrSet (NValue t f m)) >=> \s -> do
242
248
, env = if useJson drv then env drv else Map. union outputs' (env drv)
243
249
}
244
250
245
- drvPath <- writeDerivation drv'
251
+ drvPath <- pathToText <$> writeDerivation drv'
246
252
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))
249
256
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 )
252
259
attrSet = M. map nvStr $ M. fromList $ (" drvPath" , drvPathWithContext): Map. toList outputsWithContext
253
260
-- TODO: Add location information for all the entries.
254
261
-- here --v
0 commit comments