diff --git a/hnix-store-core/hnix-store-core.cabal b/hnix-store-core/hnix-store-core.cabal index 2760010..42cf3a3 100644 --- a/hnix-store-core/hnix-store-core.cabal +++ b/hnix-store-core/hnix-store-core.cabal @@ -17,12 +17,10 @@ extra-source-files: ChangeLog.md, README.md cabal-version: >=1.10 library - exposed-modules: Crypto.Hash.Truncated + exposed-modules: Crypto.Hash.Truncated, System.Nix.Store build-depends: base >=4.10 && <4.11, - cryptonite, - memory, -- Drop foundation when we can drop cryptonite <0.25 - foundation, - basement + cryptonite, memory, foundation, basement, + text, regex-base, regex-tdfa-text hs-source-dirs: src default-language: Haskell2010 diff --git a/hnix-store-core/src/System/Nix/Store.hs b/hnix-store-core/src/System/Nix/Store.hs new file mode 100644 index 0000000..4eeb8e7 --- /dev/null +++ b/hnix-store-core/src/System/Nix/Store.hs @@ -0,0 +1,41 @@ +{-| +Description : Types and effects for interacting with the Nix store. +Maintainer : Shea Levy +-} +{-# LANGUAGE DataKinds #-} +module System.Nix.Store + ( PathName, pathNameContents, pathName + , PathHashAlgo, Path(..) + ) where + +import Crypto.Hash (Digest) +import Crypto.Hash.Truncated (Truncated) +import Crypto.Hash.Algorithms (SHA256) +import Data.Text (Text) +import Text.Regex.Base.RegexLike (makeRegex, matchTest) +import Text.Regex.TDFA.Text (Regex) + +-- | The name portion of a Nix path. +-- +-- Must be composed of a-z, A-Z, 0-9, +, -, ., _, ?, and =, can't +-- start with a ., and must have at least one character. +newtype PathName = PathName + { pathNameContents :: Text -- ^ The contents of the path name + } + +-- | A regular expression for matching a valid 'PathName' +nameRegex :: Regex +nameRegex = + makeRegex "[a-zA-Z0-9\\+\\-\\_\\?\\=][a-zA-Z0-9\\+\\-\\.\\_\\?\\=]*" + +-- | Construct a 'PathName', assuming the provided contents are valid. +pathName :: Text -> Maybe PathName +pathName n = case matchTest nameRegex n of + True -> Just $ PathName n + False -> Nothing + +-- | The hash algorithm used for store path hashes. +type PathHashAlgo = Truncated SHA256 20 + +-- | A path in a store. +data Path = Path !(Digest PathHashAlgo) !PathName