Skip to content

Commit d832d9a

Browse files
cgibbardsorki
andcommitted
core: add System.Nix.DerivedPath
Co-Authored-By: Richard Marko <[email protected]>
1 parent 82fc297 commit d832d9a

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

hnix-store-core/hnix-store-core.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ library
6464
, System.Nix.Build
6565
, System.Nix.ContentAddress
6666
, System.Nix.Derivation
67+
, System.Nix.DerivedPath
6768
, System.Nix.Hash
6869
, System.Nix.Hash.Truncation
6970
, System.Nix.Nar
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module System.Nix.DerivedPath (
4+
OutputsSpec(..)
5+
, DerivedPath(..)
6+
, parseOutputsSpec
7+
, outputsSpecToText
8+
, parseDerivedPath
9+
, derivedPathToText
10+
) where
11+
12+
import Data.Bifunctor (first)
13+
import GHC.Generics (Generic)
14+
import Data.Set (Set)
15+
import Data.Text (Text)
16+
import System.Nix.StorePath (StoreDir, StorePath, StorePathName, InvalidPathError)
17+
18+
import qualified Data.Set
19+
import qualified Data.Text
20+
import qualified System.Nix.StorePath
21+
22+
data OutputsSpec =
23+
OutputsSpec_All
24+
| OutputsSpec_Names (Set StorePathName)
25+
deriving (Eq, Generic, Ord, Show)
26+
27+
data DerivedPath =
28+
DerivedPath_Opaque StorePath
29+
| DerivedPath_Built StorePath OutputsSpec
30+
deriving (Eq, Generic, Ord, Show)
31+
32+
data ParseOutputsError =
33+
ParseOutputsError_InvalidPath InvalidPathError
34+
| ParseOutputsError_NoNames
35+
deriving (Eq, Ord, Show)
36+
37+
convertError
38+
:: Either InvalidPathError a
39+
-> Either ParseOutputsError a
40+
convertError = first ParseOutputsError_InvalidPath
41+
42+
parseOutputsSpec :: Text -> Either ParseOutputsError OutputsSpec
43+
parseOutputsSpec t
44+
| t == "*" = Right OutputsSpec_All
45+
| otherwise = do
46+
names <- mapM
47+
(convertError . System.Nix.StorePath.makeStorePathName)
48+
(Data.Text.splitOn "," t)
49+
if null names
50+
then Left ParseOutputsError_NoNames
51+
else Right $ OutputsSpec_Names (Data.Set.fromList names)
52+
53+
outputsSpecToText :: OutputsSpec -> Text
54+
outputsSpecToText = \case
55+
OutputsSpec_All -> "*"
56+
OutputsSpec_Names ns ->
57+
Data.Text.intercalate "," (fmap System.Nix.StorePath.unStorePathName (Data.Set.toList ns))
58+
59+
parseDerivedPath
60+
:: StoreDir
61+
-> Text
62+
-> Either ParseOutputsError DerivedPath
63+
parseDerivedPath root p =
64+
case Data.Text.breakOn "!" p of
65+
(s,r) ->
66+
if Data.Text.null r
67+
then DerivedPath_Opaque
68+
<$> (convertError $ System.Nix.StorePath.parsePathFromText root s)
69+
else DerivedPath_Built
70+
<$> (convertError $ System.Nix.StorePath.parsePathFromText root s)
71+
<*> parseOutputsSpec r
72+
73+
derivedPathToText :: StoreDir -> DerivedPath -> Text
74+
derivedPathToText root = \case
75+
DerivedPath_Opaque p ->
76+
System.Nix.StorePath.storePathToText root p
77+
DerivedPath_Built p os ->
78+
System.Nix.StorePath.storePathToText root p
79+
<> "!"
80+
<> outputsSpecToText os

0 commit comments

Comments
 (0)