/
dhall-build.hs
145 lines (126 loc) · 4.15 KB
/
dhall-build.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{-# LANGUAGE OverloadedStrings #-}
module Main ( main ) where
import Dhall.Core ( Expr(..) )
import qualified Dhall.Map as Map
import Control.Monad.Trans.State.Strict ( evalStateT, runStateT )
import Lens.Family
import Control.Applicative ( (<**>) )
import Control.Exception ( throwIO )
import qualified Dhall.Import
import qualified Data.Text.IO as Text
import qualified Dhall.Context
import qualified Dhall.Map
import qualified Dhall.Core
import qualified Dhall.Parser
import qualified Dhall.TypeCheck
import qualified Options.Applicative as OptParse
import qualified DhallBuild.DerivationTree as DhallBuild
import qualified Nix.Daemon
commandLineParser :: OptParse.ParserInfo FilePath
commandLineParser =
OptParse.info ( parser <**> OptParse.helper ) mempty
where
parser =
OptParse.strArgument ( OptParse.metavar "FILE" )
main :: IO ()
main = do
f <-
OptParse.execParser commandLineParser
t <-
Text.readFile f
parsedExpr <-
case Dhall.Parser.exprFromText mempty t of
Left e ->
throwIO e
Right a ->
return a
( res, derivationTrees ) <-
runStateT
( evalStateT
( Dhall.Import.loadWith parsedExpr )
( Dhall.Import.emptyStatus "."
& Dhall.Import.normalizer .~ Dhall.Core.ReifiedNormalizer DhallBuild.dhallBuildNormalizer
& Dhall.Import.startingContext .~ context
)
)
[]
mapM_ print derivationTrees
Nix.Daemon.withDaemon $ \nixDaemon ->
mapM_ ( DhallBuild.addDerivationTree nixDaemon ) derivationTrees
Text.putStrLn ( Dhall.Core.pretty res )
-- context :: Dhall.Context.Context (Expr s X)
context =
Dhall.Context.insert
"derivation"
( Pi
"_"
( Record
( Map.fromList
[ ( "args", List `App` Text )
, ( "builder"
, Union
( Map.fromList
[ ( "Builtin"
, Union
( Map.fromList
[ ( "Fetch-Url", Record mempty ) ]
)
)
, ( "Exe", Text )
]
)
)
, ( "environment"
, List
`App`
Record
( Map.fromList
[ ( "name", Text )
, ( "value"
, Union
( Map.fromList
[ ( "Bool", Bool ), ( "Text", Text ) ]
)
)
]
)
)
, ( "name", Text )
, ( "output-hash"
, Optional
`App`
Record
( Map.fromList
[ ("algorithm"
, Union
( Map.fromList
[ ( "SHA256", Record mempty ) ]
)
)
, ( "hash", Text )
, ( "mode"
, Union
( Map.fromList
[ ("Flat", Record mempty)
, ("Recursive", Record mempty)
]
)
)
]
)
)
, ( "outputs", List `App` Text )
, ( "system"
, Union
( Map.fromList
[ ( "builtin", Record mempty )
, ( "x86_64-linux", Record mempty )
]
)
)
]
)
)
Text
)
Dhall.Context.empty