Skip to content

Commit

Permalink
Merge pull request #276 from haskell-nix/srk/daemon
Browse files Browse the repository at this point in the history
Some more server
  • Loading branch information
sorki authored Dec 12, 2023
2 parents 11da925 + 619687b commit 21040fb
Show file tree
Hide file tree
Showing 21 changed files with 911 additions and 732 deletions.
1 change: 1 addition & 0 deletions docs/01-Contributors.org
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ in order of appearance:
+ Ryan Trinkle @ryantrinkle
+ Travis Whitaker @TravisWhitaker
+ Andrea Bedini @andreabedini
+ Dan Bornside @danbornside
5 changes: 5 additions & 0 deletions hnix-store-core/src/System/Nix/DerivedPath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ import qualified System.Nix.StorePath

data OutputsSpec =
OutputsSpec_All
-- ^ Wildcard spec (^*) meaning all outputs
| OutputsSpec_Names (Set OutputName)
-- ^ Set of specific outputs
deriving (Eq, Generic, Ord, Show)

data DerivedPath =
DerivedPath_Opaque StorePath
-- ^ Fully evaluated store path that can't be built
-- but can be fetched
| DerivedPath_Built StorePath OutputsSpec
-- ^ Derivation path and the outputs built from it
deriving (Eq, Generic, Ord, Show)

data ParseOutputsError =
Expand Down
15 changes: 13 additions & 2 deletions hnix-store-core/src/System/Nix/Realisation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module System.Nix.Realisation (
, derivationOutputBuilder
, derivationOutputParser
, Realisation(..)
, RealisationWithId(..)
) where

import Crypto.Hash (Digest)
Expand Down Expand Up @@ -80,8 +81,7 @@ derivationOutputBuilder outputName DerivationOutput{..} =
--
-- realisationId is ommited since it is a key
-- of type @DerivationOutput OutputName@ so
-- we will use a tuple like @(DerivationOutput OutputName, Realisation)@
-- instead.
-- we will use @RealisationWithId@ newtype
data Realisation = Realisation
{ realisationOutPath :: StorePath
-- ^ Output path
Expand All @@ -90,3 +90,14 @@ data Realisation = Realisation
, realisationDependencies :: Map (DerivationOutput OutputName) StorePath
-- ^ Dependent realisations required for this one to be valid
} deriving (Eq, Generic, Ord, Show)

-- | For wire protocol
--
-- We store this normalized in @Build.buildResultBuiltOutputs@
-- as @Map (DerivationOutput OutputName) Realisation@
-- but wire protocol needs it de-normalized so we
-- need a special (From|To)JSON instances for it
newtype RealisationWithId = RealisationWithId
{ unRealisationWithId :: (DerivationOutput OutputName, Realisation)
}
deriving (Eq, Generic, Ord, Show)
12 changes: 6 additions & 6 deletions hnix-store-json/src/System/Nix/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Data.Aeson
import Deriving.Aeson
import System.Nix.Base (BaseEncoding(NixBase32))
import System.Nix.OutputName (OutputName)
import System.Nix.Realisation (DerivationOutput, Realisation)
import System.Nix.Realisation (DerivationOutput, Realisation, RealisationWithId(..))
import System.Nix.Signature (Signature)
import System.Nix.StorePath (StoreDir(..), StorePath, StorePathName, StorePathHashPart)

Expand Down Expand Up @@ -159,18 +159,18 @@ deriving
instance FromJSON Realisation

-- For a keyed version of Realisation
-- we use (DerivationOutput OutputName, Realisation)
-- we use RealisationWithId (DerivationOutput OutputName, Realisation)
-- instead of Realisation.id :: (DerivationOutput OutputName)
-- field.
instance {-# OVERLAPPING #-} ToJSON (DerivationOutput OutputName, Realisation) where
toJSON (drvOut, r) =
instance ToJSON RealisationWithId where
toJSON (RealisationWithId (drvOut, r)) =
case toJSON r of
Object o -> Object $ Data.Aeson.KeyMap.insert "id" (toJSON drvOut) o
_ -> error "absurd"

instance {-# OVERLAPPING #-} FromJSON (DerivationOutput OutputName, Realisation) where
instance FromJSON RealisationWithId where
parseJSON v@(Object o) = do
r <- parseJSON @Realisation v
drvOut <- o .: "id"
pure (drvOut, r)
pure (RealisationWithId (drvOut, r))
parseJSON x = fail $ "Expected Object but got " ++ show x
9 changes: 4 additions & 5 deletions hnix-store-remote/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ via `nix-daemon`.
```haskell
{-# LANGUAGE OverloadedStrings #-}

import Control.Monad (void)
import Control.Monad.IO.Class (liftIO)
import System.Nix.StorePath (mkStorePathName)
import System.Nix.Store.Remote

main :: IO ()
main = do
void $ runStore $ do
runStore $ do
syncWithGC
roots <- findRoots
liftIO $ print roots

res <- case mkStorePathName "hnix-store" of
Left e -> error (show e)
Expand All @@ -33,5 +30,7 @@ main = do
(StoreText name "Hello World!")
mempty
RepairMode_DontRepair
liftIO $ print res

pure (roots, res)
>>= print
```
16 changes: 0 additions & 16 deletions hnix-store-remote/app/BuildDerivation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,23 @@
module Main where

import Data.Default.Class (Default(def))
import Data.Text (Text)
import System.Nix.Derivation (Derivation)
import System.Nix.StorePath (StorePath)

import qualified Data.Text
import qualified Data.Text.IO
import qualified Data.Attoparsec.Text
import qualified System.Environment
import qualified System.Nix.Build
import qualified System.Nix.Derivation
import qualified System.Nix.StorePath
import qualified System.Nix.Store.Remote

parseDerivation :: FilePath -> IO (Derivation StorePath Text)
parseDerivation source = do
contents <- Data.Text.IO.readFile source
case Data.Attoparsec.Text.parseOnly
(System.Nix.Derivation.parseDerivation def) contents of
Left e -> error e
Right drv -> pure drv

main :: IO ()
main = System.Environment.getArgs >>= \case
[filename] -> do
case System.Nix.StorePath.parsePathFromText def (Data.Text.pack filename) of
Left e -> error $ show e
Right p -> do
d <- parseDerivation filename
out <-
System.Nix.Store.Remote.runStore
$ System.Nix.Store.Remote.buildDerivation
p
d
System.Nix.Build.BuildMode_Normal
print out
_ -> error "No input derivation file"
Expand Down
14 changes: 6 additions & 8 deletions hnix-store-remote/hnix-store-remote.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ common commons
, ViewPatterns
default-language: Haskell2010

common tests
import: commons
build-tool-depends:
tasty-discover:tasty-discover

flag io-testsuite
default:
False
Expand Down Expand Up @@ -119,7 +114,9 @@ library
, data-default-class
, dependent-sum > 0.7
, dependent-sum-template >= 0.2.0.1 && < 0.3
-- , directory
, dlist >= 1.0
, exceptions
, generic-arbitrary < 1.1
, hashable
, text
Expand All @@ -139,7 +136,6 @@ executable build-derivation
buildable: False
build-depends:
base >=4.12 && <5
, attoparsec
, hnix-store-core
, hnix-store-remote
, data-default-class
Expand All @@ -163,7 +159,7 @@ executable remote-readme
ghc-options: -pgmL markdown-unlit -Wall

test-suite remote
import: tests
import: commons
type: exitcode-stdio-1.0
main-is: Driver.hs
hs-source-dirs: tests
Expand All @@ -187,7 +183,7 @@ test-suite remote
, QuickCheck

test-suite remote-io
import: tests
import: commons

if !flag(io-testsuite) || os(darwin)
buildable: False
Expand All @@ -206,9 +202,11 @@ test-suite remote-io
, hnix-store-remote
, hnix-store-tests
, bytestring
, concurrency
, containers
, crypton
, directory
, exceptions
, filepath
, hspec
, hspec-expectations-lifted
Expand Down
Loading

0 comments on commit 21040fb

Please sign in to comment.