gunk-dev/the-valley

check.go

source5ed515359461main's published tip history
6.0 KiB
1package main
2
3// Running a check, and the digests a run yields.
4//
5// The nix runner claims purity strongly: it builds a flake check
6// derivation, so the check's inputs, the derivation itself and its output
7// are all named, and any verifier holding the same tree can re-derive
8// them. The command runner claims nothing beyond having run — its
9// statement is a notarization naming the environment and the time
10// (archive/design/verification.md).
11
12import (
13 "bytes"
14 "encoding/json"
15 "fmt"
16 "os"
17 "os/exec"
18 "runtime"
19 "strings"
20 "time"
21)
22
23type checkSpec struct {
24 name string
25 attribute string // nix runner
26 command string // command runner
27}
28
29func (c checkSpec) runner() string {
30 if c.command != "" {
31 return "command"
32 }
33 return "nix"
34}
35
36// checkOutcome is what running one check produced: whether it passed, the
37// output a reader needs when it did not, how it was satisfied, and the
38// digests of a pure run.
39type checkOutcome struct {
40 spec checkSpec
41 passed bool
42 log string
43 satisfied string
44
45 inputs string
46 derivation string
47 output string
48 observedAt string
49}
50
51// nixSystem is the flake system attribute of the machine running the
52// check. A check is built where the work was written, so this is the
53// runner's own system and never something the caller states about another
54// machine.
55func nixSystem() string {
56 arch := runtime.GOARCH
57 switch arch {
58 case "amd64":
59 arch = "x86_64"
60 case "arm64":
61 arch = "aarch64"
62 case "386":
63 arch = "i686"
64 }
65 return arch + "-" + runtime.GOOS
66}
67
68func nix(dir string, args ...string) (string, string, error) {
69 cmd := exec.Command("nix", args...)
70 cmd.Dir = dir
71 var out, errb bytes.Buffer
72 cmd.Stdout = &out
73 cmd.Stderr = &errb
74 err := cmd.Run()
75 return out.String(), errb.String(), err
76}
77
78func nixStore(args ...string) (string, string, error) {
79 cmd := exec.Command("nix-store", args...)
80 var out, errb bytes.Buffer
81 cmd.Stdout = &out
82 cmd.Stderr = &errb
83 err := cmd.Run()
84 return out.String(), errb.String(), err
85}
86
87// flakeChecks lists every check attribute a flake defines for a system —
88// what "the repo's canonical checks" means when the caller names none.
89func flakeChecks(tree, system string) ([]string, error) {
90 out, errb, err := nix(tree, "eval", "--json", "--apply", "builtins.attrNames", tree+"#checks."+system)
91 if err != nil {
92 return nil, fmt.Errorf("listing checks: %w: %s", err, strings.TrimSpace(errb))
93 }
94 var names []string
95 if err := json.Unmarshal([]byte(out), &names); err != nil {
96 return nil, fmt.Errorf("listing checks: %w", err)
97 }
98 return names, nil
99}
100
101// closure is what evaluating a pure check names before anything is built:
102// the derivation, and the digest of its input closure. Both are functions
103// of the tree alone, so an integrator can recompute the closure digest on a
104// rebased tree without running the check — which is the whole of the pure
105// transfer rule (dcr-439b771).
106type closure struct {
107 drv string
108 derivation string
109 inputs string
110}
111
112// evalClosure evaluates one flake check over an exported tree and digests
113// what the evaluation named. Nothing is built.
114func evalClosure(tree, system string, spec checkSpec) (closure, error) {
115 attr := tree + "#checks." + system + "." + spec.attribute
116 drv, errb, err := nix(tree, "eval", "--raw", attr+".drvPath")
117 if err != nil {
118 return closure{}, fmt.Errorf("evaluating %s: %w: %s", attr, err, strings.TrimSpace(errb))
119 }
120 drv = strings.TrimSpace(drv)
121
122 drvBytes, err := os.ReadFile(drv)
123 if err != nil {
124 return closure{}, err
125 }
126 inputs, err := drvInputs(drv)
127 if err != nil {
128 return closure{}, err
129 }
130 return closure{
131 drv: drv,
132 derivation: fmt.Sprintf("%x", sha256sum(drvBytes)),
133 inputs: inputsDigest(inputs),
134 }, nil
135}
136
137// runPure builds one flake check over the exported tree and digests what
138// the build named: the derivation's inputs, the derivation, and its
139// output. Building from the derivation path rather than the attribute a
140// second time guarantees the thing built is the thing recorded.
141func runPure(tree, system string, spec checkSpec) (checkOutcome, error) {
142 out := checkOutcome{spec: spec}
143 c, err := evalClosure(tree, system, spec)
144 if err != nil {
145 return out, err
146 }
147 out.derivation, out.inputs = c.derivation, c.inputs
148 out.satisfied = outputsCached(c.drv)
149
150 built, buildLog, err := nix(tree, "build", "--no-link", "--print-out-paths", c.drv+"^*")
151 out.log = strings.TrimSpace(buildLog)
152 if err != nil {
153 return out, nil // a failed check is an outcome, not an error
154 }
155 paths := strings.Fields(built)
156 if len(paths) != 1 {
157 return out, fmt.Errorf("%s produced %d outputs; a check attribute must produce exactly one", spec.name, len(paths))
158 }
159 entries, err := fsEntries(paths[0])
160 if err != nil {
161 return out, err
162 }
163 out.output = treeDigest(entries)
164 out.passed = true
165 return out, nil
166}
167
168// drvInputs collects the store paths a derivation reads: the derivations
169// it depends on and the sources it takes directly. A .drv's references are
170// exactly that set, and `nix-store --query --references` prints them as
171// lines — a shape that has not changed in the lifetime of nix, unlike the
172// versioned JSON of `nix derivation show`.
173func drvInputs(drv string) ([]string, error) {
174 out, errb, err := nixStore("--query", "--references", drv)
175 if err != nil {
176 return nil, fmt.Errorf("querying the inputs of %s: %w: %s", drv, err, strings.TrimSpace(errb))
177 }
178 inputs := strings.Fields(out)
179 if len(inputs) == 0 {
180 return nil, fmt.Errorf("%s has no inputs at all", drv)
181 }
182 return inputs, nil
183}
184
185// runEffectful runs a command over the exported tree and notarizes what it
186// observed. No digests: there is nothing to re-derive, which is exactly
187// what the effectful predicate type says.
188func runEffectful(tree string, spec checkSpec) checkOutcome {
189 out := checkOutcome{spec: spec, satisfied: satisfiedRan}
190 cmd := exec.Command("sh", "-c", spec.command)
191 cmd.Dir = tree
192 log, err := cmd.CombinedOutput()
193 out.log = strings.TrimSpace(string(log))
194 out.observedAt = time.Now().UTC().Format("2006-01-02T15:04:05Z")
195 out.passed = err == nil
196 return out
197}