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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// Package store defines the Store interface used by the gonix WASM host.
// The WASM evaluator calls back into this interface for all store I/O.
package store
import (
"context"
"github.com/nixos/lix/gonix/codec"
)
// PathInfo is the result of QueryPathInfo.
type PathInfo struct {
NarHash []byte // raw hash bytes
HashType byte // 1=md5 2=sha1 3=sha256 4=sha512
NarSize uint64
References []string
Deriver string // empty if absent
}
// Derivation represents a Nix derivation.
type Derivation struct {
Name string
Platform string
Builder string
Args []string
Env map[string]string
InputSrcs []string
InputDrvs map[string][]string // drv path → output names
Outputs map[string]Output
}
// Output is a derivation output.
type Output struct {
Type string // "ia" (InputAddressed) or "ca-fixed"
// InputAddressed:
Path string
// CAFixed:
Method string
Hash []byte
HashType byte
}
// Store is the interface the WASM host implements.
// All methods are called synchronously from within the WASM module's execution.
type Store interface {
IsValidPath(ctx context.Context, path string) (bool, error)
QueryPathInfo(ctx context.Context, path string) (*PathInfo, error)
ComputeFSClosure(ctx context.Context, path string) ([]string, error)
EnsurePath(ctx context.Context, path string) error
AddTextToStore(ctx context.Context, name, content string, refs []string) (string, error)
WriteDerivation(ctx context.Context, drv *Derivation) (string, error)
ReadDerivation(ctx context.Context, path string) (*Derivation, error)
}
// ---------------------------------------------------------------------------
// Codec helpers for Derivation
// ---------------------------------------------------------------------------
// EncodeDrv encodes a Derivation into the wire format.
func EncodeDrv(d *Derivation) codec.Value {
// env pairs
envItems := make([]codec.Value, 0, len(d.Env)*2)
for k, v := range d.Env {
envItems = append(envItems, codec.String(k), codec.String(v))
}
// inputSrcs list
srcItems := make([]codec.Value, len(d.InputSrcs))
for i, s := range d.InputSrcs {
srcItems[i] = codec.String(s)
}
// inputDrvs pairs: drv → list of output names
inputDrvItems := make([]codec.Value, 0, len(d.InputDrvs)*2)
for drv, outs := range d.InputDrvs {
outItems := make([]codec.Value, len(outs))
for i, o := range outs {
outItems[i] = codec.String(o)
}
inputDrvItems = append(inputDrvItems, codec.String(drv), codec.List(outItems...))
}
// outputs pairs: name → output struct
outputItems := make([]codec.Value, 0, len(d.Outputs)*2)
for name, out := range d.Outputs {
outputItems = append(outputItems, codec.String(name), encodeOutput(out))
}
args := make([]codec.Value, len(d.Args))
for i, a := range d.Args {
args[i] = codec.String(a)
}
return codec.Record(
"name", codec.String(d.Name),
"platform", codec.String(d.Platform),
"builder", codec.String(d.Builder),
"args", codec.List(args...),
"env", codec.List(envItems...),
"inputSrcs", codec.List(srcItems...),
"inputDrvs", codec.List(inputDrvItems...),
"outputs", codec.List(outputItems...),
)
}
func encodeOutput(o Output) codec.Value {
if o.Type == "ia" {
return codec.Variant("ia", codec.String(o.Path))
}
// ca-fixed
return codec.Variant("ca-fixed", codec.Record(
"method", codec.String(o.Method),
"hash", codec.Scalar(o.Hash),
"hashType", codec.Scalar([]byte{o.HashType}),
))
}
// DecodeDrv decodes a Derivation from a codec Value (record).
func DecodeDrv(v codec.Value) (*Derivation, error) {
d := &Derivation{
Env: make(map[string]string),
InputDrvs: make(map[string][]string),
Outputs: make(map[string]Output),
}
if f, ok := v.Field("name"); ok { d.Name, _ = f.AsString() }
if f, ok := v.Field("platform"); ok { d.Platform, _ = f.AsString() }
if f, ok := v.Field("builder"); ok { d.Builder, _ = f.AsString() }
if f, ok := v.Field("args"); ok {
for _, item := range f.Items {
s, _ := item.AsString()
d.Args = append(d.Args, s)
}
}
// env: flat list of k, v, k, v...
if f, ok := v.Field("env"); ok {
items := f.Items
for i := 0; i+1 < len(items); i += 2 {
k, _ := items[i].AsString()
val, _ := items[i+1].AsString()
d.Env[k] = val
}
}
if f, ok := v.Field("inputSrcs"); ok {
for _, item := range f.Items {
s, _ := item.AsString()
d.InputSrcs = append(d.InputSrcs, s)
}
}
// inputDrvs: flat list of drv, list-of-outputs, drv, list-of-outputs...
if f, ok := v.Field("inputDrvs"); ok {
items := f.Items
for i := 0; i+1 < len(items); i += 2 {
drv, _ := items[i].AsString()
var outs []string
for _, o := range items[i+1].Items {
s, _ := o.AsString()
outs = append(outs, s)
}
d.InputDrvs[drv] = outs
}
}
// outputs: flat list of name, output-variant, ...
if f, ok := v.Field("outputs"); ok {
items := f.Items
for i := 0; i+1 < len(items); i += 2 {
name, _ := items[i].AsString()
out, err := decodeOutput(items[i+1])
if err != nil {
return nil, err
}
d.Outputs[name] = out
}
}
return d, nil
}
func decodeOutput(v codec.Value) (Output, error) {
if v.Kind != codec.KindVariant {
return Output{}, nil
}
o := Output{Type: v.Tag}
switch v.Tag {
case "ia":
o.Path, _ = v.Payload.AsString()
case "ca-fixed":
if f, ok := v.Payload.Field("method"); ok { o.Method, _ = f.AsString() }
if f, ok := v.Payload.Field("hash"); ok { o.Hash = f.Bytes }
if f, ok := v.Payload.Field("hashType"); ok {
if len(f.Bytes) > 0 { o.HashType = f.Bytes[0] }
}
}
return o, nil
}
|