Skip to content

Commit e6d21c1

Browse files
committed
remote: deal with Realisation.id (required for the server side and qc prop)
1 parent f79effe commit e6d21c1

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

hnix-store-core/src/System/Nix/Build.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Data.Text (Text)
1515
import GHC.Generics (Generic)
1616

1717
import System.Nix.OutputName (OutputName)
18-
import System.Nix.Realisation (Realisation)
18+
import System.Nix.Realisation (DerivationOutput, Realisation)
1919

2020
-- | Mode of the build operation
2121
-- Keep the order of these Enums to match enums from reference implementations
@@ -59,7 +59,7 @@ data BuildResult = BuildResult
5959
-- ^ Start time of this build (since 1.29)
6060
, buildResultStopTime :: Maybe UTCTime
6161
-- ^ Stop time of this build (since 1.29)
62-
, buildResultBuiltOutputs :: Maybe (Map OutputName Realisation)
62+
, buildResultBuiltOutputs :: Maybe (Map (DerivationOutput OutputName) Realisation)
6363
-- ^ Mapping of the output names to @Realisation@s (since 1.28)
6464
-- (paths with additional info and their dependencies)
6565
}

hnix-store-json/src/System/Nix/JSON.hs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ which is required for `-remote`.
1010
module System.Nix.JSON where
1111

1212
import Data.Aeson
13-
import Data.Aeson.Types (toJSONKeyText)
1413
import Deriving.Aeson
1514
import System.Nix.Base (BaseEncoding(NixBase32))
1615
import System.Nix.OutputName (OutputName)
1716
import System.Nix.Realisation (DerivationOutput, Realisation)
1817
import System.Nix.Signature (Signature)
1918
import System.Nix.StorePath (StoreDir(..), StorePath, StorePathName, StorePathHashPart)
2019

20+
import qualified Data.Aeson.KeyMap
21+
import qualified Data.Aeson.Types
2122
import qualified Data.Attoparsec.Text
2223
import qualified Data.Char
2324
import qualified Data.Text
@@ -93,7 +94,7 @@ instance ToJSON (DerivationOutput OutputName) where
9394

9495
instance ToJSONKey (DerivationOutput OutputName) where
9596
toJSONKey =
96-
toJSONKeyText
97+
Data.Aeson.Types.toJSONKeyText
9798
$ Data.Text.Lazy.toStrict
9899
. Data.Text.Lazy.Builder.toLazyText
99100
. System.Nix.Realisation.derivationOutputBuilder
@@ -156,3 +157,20 @@ deriving
156157
]
157158
] Realisation
158159
instance FromJSON Realisation
160+
161+
-- For a keyed version of Realisation
162+
-- we use (DerivationOutput OutputName, Realisation)
163+
-- instead of Realisation.id :: (DerivationOutput OutputName)
164+
-- field.
165+
instance {-# OVERLAPPING #-} ToJSON (DerivationOutput OutputName, Realisation) where
166+
toJSON (drvOut, r) =
167+
case toJSON r of
168+
Object o -> Object $ Data.Aeson.KeyMap.insert "id" (toJSON drvOut) o
169+
_ -> error "absurd"
170+
171+
instance {-# OVERLAPPING #-} FromJSON (DerivationOutput OutputName, Realisation) where
172+
parseJSON v@(Object o) = do
173+
r <- parseJSON @Realisation v
174+
drvOut <- o .: "id"
175+
pure (drvOut, r)
176+
parseJSON x = fail $ "Expected Object but got " ++ show x

hnix-store-remote/src/System/Nix/Store/Remote/Serializer.hs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ module System.Nix.Store.Remote.Serializer
3131
, set
3232
, hashSet
3333
, mapS
34+
, vector
35+
, json
3436
-- * ProtoVersion
3537
, protoVersion
3638
-- * StorePath
@@ -45,6 +47,7 @@ module System.Nix.Store.Remote.Serializer
4547
-- * Realisation
4648
, derivationOutputTyped
4749
, realisation
50+
, realisationWithId
4851
-- * Signatures
4952
, signature
5053
, narSignature
@@ -93,6 +96,7 @@ import Control.Monad.Trans (MonadTrans, lift)
9396
import Control.Monad.Trans.Reader (ReaderT, runReaderT, withReaderT)
9497
import Control.Monad.Trans.Except (ExceptT, mapExceptT, runExceptT, withExceptT)
9598
import Crypto.Hash (Digest, HashAlgorithm, SHA256)
99+
import Data.Aeson (FromJSON, ToJSON)
96100
import Data.ByteString (ByteString)
97101
import Data.Dependent.Sum (DSum((:=>)))
98102
import Data.Fixed (Uni)
@@ -242,6 +246,7 @@ data SError
242246
| SError_HashAlgo String
243247
| SError_IllegalBool Word64
244248
| SError_InvalidNixBase32
249+
| SError_JSONDecoding String
245250
| SError_NarHashMustBeSHA256
246251
| SError_NotYetImplemented String (ForPV ProtoVersion)
247252
| SError_Name InvalidNameError
@@ -447,6 +452,22 @@ vector =
447452
Data.Vector.toList
448453
. list
449454

455+
json
456+
:: ( FromJSON a
457+
, ToJSON a
458+
)
459+
=> NixSerializer r SError a
460+
json =
461+
mapPrismSerializer
462+
( Data.Bifunctor.first SError_JSONDecoding
463+
. Data.Aeson.eitherDecode
464+
)
465+
Data.Aeson.encode
466+
$ mapIsoSerializer
467+
Data.ByteString.Lazy.fromStrict
468+
Data.ByteString.Lazy.toStrict
469+
byteString
470+
450471
-- * ProtoVersion
451472

452473
-- protoVersion_major & 0xFF00
@@ -614,17 +635,11 @@ derivationOutputTyped =
614635
)
615636
text
616637

617-
realisation
618-
:: HasStoreDir r
619-
=> NixSerializer r SError Realisation
620-
realisation = Serializer
621-
{ getS = do
622-
rb <- getS byteString
623-
case Data.Aeson.eitherDecode (Data.ByteString.Lazy.fromStrict rb) of
624-
Left e -> error e
625-
Right r -> pure r
626-
, putS = putS byteString . Data.ByteString.Lazy.toStrict . Data.Aeson.encode
627-
}
638+
realisation :: NixSerializer r SError Realisation
639+
realisation = json
640+
641+
realisationWithId :: NixSerializer r SError (System.Nix.Realisation.DerivationOutput OutputName, Realisation)
642+
realisationWithId = json
628643

629644
-- * Signatures
630645

@@ -818,9 +833,10 @@ buildResult = Serializer
818833
if protoVersion_minor pv >= 28
819834
then
820835
pure
821-
. Data.Map.Strict.mapKeys
822-
System.Nix.Realisation.derivationOutputName
823-
<$> getS (mapS derivationOutputTyped realisation)
836+
. Data.Map.Strict.fromList
837+
. map (\(_, (a, b)) -> (a, b))
838+
. Data.Map.Strict.toList
839+
<$> getS (mapS derivationOutputTyped realisationWithId)
824840
else pure Nothing
825841
pure BuildResult{..}
826842

@@ -835,8 +851,10 @@ buildResult = Serializer
835851
putS time $ Data.Maybe.fromMaybe t0 buildResultStartTime
836852
putS time $ Data.Maybe.fromMaybe t0 buildResultStopTime
837853
Control.Monad.when (protoVersion_minor pv >= 28)
838-
-- TODO realisation.id
839-
$ putS (mapS outputName realisation)
854+
$ putS (mapS derivationOutputTyped realisationWithId)
855+
$ Data.Map.Strict.fromList
856+
$ map (\(a, b) -> (a, (a, b)))
857+
$ Data.Map.Strict.toList
840858
$ Data.Maybe.fromMaybe mempty buildResultBuiltOutputs
841859
}
842860
where

0 commit comments

Comments
 (0)