Skip to content

Commit d1e35a3

Browse files
committed
Effects.Derivation: derivationParser: refactor
1 parent 644f43c commit d1e35a3

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/Nix/Effects/Derivation.hs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ parsePath p = case Store.parsePath "/nix/store" (encodeUtf8 p) of
7171
writeDerivation :: (Framed e m, MonadStore m) => Derivation -> m Store.StorePath
7272
writeDerivation drv@Derivation{inputs, name} = do
7373
let (inputSrcs, inputDrvs) = inputs
74-
references <- fmap Set.fromList $ traverse parsePath $ Set.toList $ Set.union inputSrcs $ Set.fromList $ Map.keys inputDrvs
74+
references <- Set.fromList <$> traverse parsePath (Set.toList $ Set.union inputSrcs $ Set.fromList $ Map.keys inputDrvs)
7575
path <- addTextToStore (Text.append name ".drv") (unparseDrv drv) (S.fromList $ Set.toList references) False
7676
parsePath $ toText $ unStorePath path
7777

7878
-- | Traverse the graph of inputDrvs to replace fixed output derivations with their fixed output hash.
7979
-- this avoids propagating changes to their .drv when the output hash stays the same.
80-
hashDerivationModulo :: (MonadNix e t f m, MonadState (b, HashMap Text Text) m) => Derivation -> m (Store.Digest 'Store.SHA256)
80+
hashDerivationModulo :: (MonadNix e t f m, MonadState (b, AttrSet Text) m) => Derivation -> m (Store.Digest 'Store.SHA256)
8181
hashDerivationModulo
8282
Derivation
8383
{ mFixed = Just (Store.SomeDigest (digest :: Store.Digest hashType))
@@ -212,10 +212,12 @@ derivationParser = do
212212
)
213213
regular = noneOf ['\\', '"']
214214

215+
wrap o c p =
216+
string o *> sepBy p (string ",") <* string c
217+
215218
parens :: Parsec () Text a -> Parsec () Text [a]
216-
parens p =
217-
(string "(") *> sepBy p (string ",") <* (string ")")
218-
serializeList p = (string "[") *> sepBy p (string ",") <* (string "]")
219+
parens p = wrap "(" ")" p
220+
serializeList p = wrap "[" "]" p
219221

220222
parseFixed :: [(Text, Text, Text, Text)] -> (Maybe Store.SomeNamedDigest, HashMode)
221223
parseFixed fullOutputs = case fullOutputs of
@@ -234,33 +236,44 @@ derivationParser = do
234236
_ -> (Nothing, Flat)
235237

236238

237-
defaultDerivationStrict :: forall e t f m b. (MonadNix e t f m, MonadState (b, HashMap Text Text) m) => NValue t f m -> m (NValue t f m)
239+
defaultDerivationStrict :: forall e t f m b. (MonadNix e t f m, MonadState (b, AttrSet Text) m) => NValue t f m -> m (NValue t f m)
238240
defaultDerivationStrict val = do
239241
s <- fromValue @(AttrSet (NValue t f m)) val
240242
(drv, ctx) <- runWithStringContextT' $ buildDerivationWithContext s
241243
drvName <- makeStorePathName $ name drv
242-
let inputs = toStorePaths ctx
244+
let
245+
inputs = toStorePaths ctx
246+
ifNotJsonModEnv f =
247+
bool f id (useJson drv)
248+
(env drv)
243249

244250
-- Compute the output paths, and add them to the environment if needed.
245251
-- Also add the inputs, just computed from the strings contexts.
246252
drv' <- case mFixed drv of
247253
Just (Store.SomeDigest digest) -> do
248-
let out = pathToText $ Store.makeFixedOutputPath "/nix/store" (hashMode drv == Recursive) digest drvName
249-
let env' = if useJson drv then env drv else Map.insert "out" out (env drv)
254+
let
255+
out = pathToText $ Store.makeFixedOutputPath "/nix/store" (hashMode drv == Recursive) digest drvName
256+
env' = ifNotJsonModEnv $ Map.insert "out" out
250257
pure $ drv { inputs, env = env', outputs = one ("out", out) }
251258

252259
Nothing -> do
253260
hash <- hashDerivationModulo $ drv
254261
{ inputs
255262
--, outputs = Map.map (const "") (outputs drv) -- not needed, this is already the case
256-
, env = if useJson drv then env drv
257-
else foldl' (\m k -> Map.insert k "" m) (env drv) (Map.keys $ outputs drv)
263+
, env =
264+
ifNotJsonModEnv
265+
(\ baseEnv ->
266+
foldl'
267+
(\m k -> Map.insert k "" m)
268+
baseEnv
269+
(Map.keys $ outputs drv)
270+
)
258271
}
259-
outputs' <- sequence $ Map.mapWithKey (\o _ -> makeOutputPath o hash drvName) (outputs drv)
272+
outputs' <- sequence $ Map.mapWithKey (\o _ -> makeOutputPath o hash drvName) $ outputs drv
260273
pure $ drv
261274
{ inputs
262275
, outputs = outputs'
263-
, env = if useJson drv then env drv else Map.union outputs' (env drv)
276+
, env = ifNotJsonModEnv $ Map.union outputs'
264277
}
265278

266279
drvPath <- pathToText <$> writeDerivation drv'
@@ -269,9 +282,13 @@ defaultDerivationStrict val = do
269282
drvHash <- Store.encodeInBase Store.Base16 <$> hashDerivationModulo drv'
270283
modify $ second $ MS.insert drvPath drvHash
271284

272-
let outputsWithContext = Map.mapWithKey (\out path -> makeNixStringWithSingletonContext path (StringContext drvPath $ DerivationOutput out)) (outputs drv')
273-
drvPathWithContext = makeNixStringWithSingletonContext drvPath (StringContext drvPath AllOutputs)
274-
attrSet = nvStr <$> M.fromList (("drvPath", drvPathWithContext) : Map.toList outputsWithContext)
285+
let
286+
outputsWithContext =
287+
Map.mapWithKey
288+
(\out path -> makeNixStringWithSingletonContext path $ StringContext drvPath $ DerivationOutput out)
289+
(outputs drv')
290+
drvPathWithContext = makeNixStringWithSingletonContext drvPath $ StringContext drvPath AllOutputs
291+
attrSet = nvStr <$> M.fromList (("drvPath", drvPathWithContext) : Map.toList outputsWithContext)
275292
-- TODO: Add location information for all the entries.
276293
-- here --v
277294
pure $ nvSet mempty attrSet
@@ -281,7 +298,7 @@ defaultDerivationStrict val = do
281298
pathToText = decodeUtf8 . Store.storePathToRawFilePath
282299

283300
makeOutputPath o h n = do
284-
name <- makeStorePathName (Store.unStorePathName n <> if o == "out" then "" else "-" <> o)
301+
name <- makeStorePathName $ Store.unStorePathName n <> if o == "out" then "" else "-" <> o
285302
pure $ pathToText $ Store.makeStorePath "/nix/store" ("output:" <> encodeUtf8 o) h name
286303

287304
toStorePaths ctx = foldl (flip addToInputs) (mempty, mempty) ctx
@@ -374,7 +391,7 @@ buildDerivationWithContext drvAttrs = do
374391
getAttrOr' n d f = case M.lookup n drvAttrs of
375392
Nothing -> lift d
376393
Just v -> withFrame' Info (ErrorCall $ "While evaluating attribute '" <> show n <> "'") $
377-
fromValue' v >>= f
394+
f =<< fromValue' v
378395

379396
getAttrOr n d f = getAttrOr' n (pure d) f
380397

0 commit comments

Comments
 (0)