From cb29c855070833c612ce67c1033d74a4cb3a7eef Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 24 Mar 2021 00:35:37 +0100 Subject: [PATCH 1/2] add a readonly computeStorePathForPath --- .../src/System/Nix/ReadonlyStore.hs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/hnix-store-core/src/System/Nix/ReadonlyStore.hs b/hnix-store-core/src/System/Nix/ReadonlyStore.hs index ad966ae0..62464caa 100644 --- a/hnix-store-core/src/System/Nix/ReadonlyStore.hs +++ b/hnix-store-core/src/System/Nix/ReadonlyStore.hs @@ -2,6 +2,8 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE AllowAmbiguousTypes #-} +{-# LANGUAGE TypeFamilies #-} module System.Nix.ReadonlyStore where @@ -12,7 +14,9 @@ import qualified Data.Text as T import qualified Data.HashSet as HS import Data.Text.Encoding import System.Nix.Hash +import System.Nix.Nar import System.Nix.StorePath +import Control.Monad.State.Strict makeStorePath @@ -69,3 +73,22 @@ makeFixedOutputPath fp recursive h = computeStorePathForText :: FilePath -> StorePathName -> ByteString -> (StorePathSet -> StorePath) computeStorePathForText fp nm = makeTextPath fp nm . hash + +computeStorePathForPath :: forall a. (ValidAlgo a, NamedAlgo a) + => StorePathName -- ^ Name part of the newly created `StorePath` + -> FilePath -- ^ Local `FilePath` to add + -> Bool -- ^ Add target directory recursively + -> (FilePath -> Bool) -- ^ Path filter function + -> Bool -- ^ Only used by local store backend + -> IO StorePath +computeStorePathForPath name pth recursive _pathFilter _repair = do + selectedHash <- if recursive then recursiveContentHash else flatContentHash + pure $ makeFixedOutputPath "/nix/store" recursive selectedHash name + where + recursiveContentHash :: IO (Digest a) + recursiveContentHash = finalize @a <$> execStateT streamNarUpdate (initialize @a) + streamNarUpdate :: StateT (AlgoCtx a) IO () + streamNarUpdate = streamNarIO (modify . flip (update @a)) narEffectsIO pth + + flatContentHash :: IO (Digest a) + flatContentHash = hashLazy <$> narReadFile narEffectsIO pth From 18b7c213ef4abaa5bc340648f50bbe18e896cec4 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 24 Mar 2021 03:06:27 +0100 Subject: [PATCH 2/2] computeStorePathForPath: force SHA256 as it's the only valid choice --- .../src/System/Nix/ReadonlyStore.hs | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/hnix-store-core/src/System/Nix/ReadonlyStore.hs b/hnix-store-core/src/System/Nix/ReadonlyStore.hs index 62464caa..5e3c5a41 100644 --- a/hnix-store-core/src/System/Nix/ReadonlyStore.hs +++ b/hnix-store-core/src/System/Nix/ReadonlyStore.hs @@ -2,8 +2,6 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE AllowAmbiguousTypes #-} -{-# LANGUAGE TypeFamilies #-} module System.Nix.ReadonlyStore where @@ -49,9 +47,7 @@ makeTextPath fp nm h refs = makeStorePath fp ty h nm makeFixedOutputPath :: forall hashAlgo - . ( ValidAlgo hashAlgo - , NamedAlgo hashAlgo - ) + . (NamedAlgo hashAlgo) => FilePath -> Bool -> Digest hashAlgo @@ -74,21 +70,21 @@ computeStorePathForText :: FilePath -> StorePathName -> ByteString -> (StorePathSet -> StorePath) computeStorePathForText fp nm = makeTextPath fp nm . hash -computeStorePathForPath :: forall a. (ValidAlgo a, NamedAlgo a) - => StorePathName -- ^ Name part of the newly created `StorePath` - -> FilePath -- ^ Local `FilePath` to add - -> Bool -- ^ Add target directory recursively - -> (FilePath -> Bool) -- ^ Path filter function - -> Bool -- ^ Only used by local store backend - -> IO StorePath +computeStorePathForPath + :: StorePathName -- ^ Name part of the newly created `StorePath` + -> FilePath -- ^ Local `FilePath` to add + -> Bool -- ^ Add target directory recursively + -> (FilePath -> Bool) -- ^ Path filter function + -> Bool -- ^ Only used by local store backend + -> IO StorePath computeStorePathForPath name pth recursive _pathFilter _repair = do selectedHash <- if recursive then recursiveContentHash else flatContentHash pure $ makeFixedOutputPath "/nix/store" recursive selectedHash name where - recursiveContentHash :: IO (Digest a) - recursiveContentHash = finalize @a <$> execStateT streamNarUpdate (initialize @a) - streamNarUpdate :: StateT (AlgoCtx a) IO () - streamNarUpdate = streamNarIO (modify . flip (update @a)) narEffectsIO pth + recursiveContentHash :: IO (Digest 'SHA256) + recursiveContentHash = finalize <$> execStateT streamNarUpdate (initialize @'SHA256) + streamNarUpdate :: StateT (AlgoCtx 'SHA256) IO () + streamNarUpdate = streamNarIO (modify . flip (update @'SHA256)) narEffectsIO pth - flatContentHash :: IO (Digest a) + flatContentHash :: IO (Digest 'SHA256) flatContentHash = hashLazy <$> narReadFile narEffectsIO pth