gunk-dev/the-valley

check.go

source9081ff0f1d3cmain's published tip history
4.9 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// (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, and the digests of a pure run.
38type checkOutcome struct {
39 spec checkSpec
40 passed bool
41 log string
42
43 inputs string
44 derivation string
45 output string
46 observedAt string
47}
48
49// nixSystem is the flake system attribute of the machine running the
50// check. A check is built where the work was written, so this is the
51// runner's own system and never something the caller states about another
52// machine.
53func nixSystem() string {
54 arch := runtime.GOARCH
55 switch arch {
56 case "amd64":
57 arch = "x86_64"
58 case "arm64":
59 arch = "aarch64"
60 case "386":
61 arch = "i686"
62 }
63 return arch + "-" + runtime.GOOS
64}
65
66func nix(dir string, args ...string) (string, string, error) {
67 cmd := exec.Command("nix", args...)
68 cmd.Dir = dir
69 var out, errb bytes.Buffer
70 cmd.Stdout = &out
71 cmd.Stderr = &errb
72 err := cmd.Run()
73 return out.String(), errb.String(), err
74}
75
76// flakeChecks lists every check attribute a flake defines for a system โ€”
77// what "the repo's canonical checks" means when the caller names none.
78func flakeChecks(tree, system string) ([]string, error) {
79 out, errb, err := nix(tree, "eval", "--json", "--apply", "builtins.attrNames", tree+"#checks."+system)
80 if err != nil {
81 return nil, fmt.Errorf("listing checks: %w: %s", err, strings.TrimSpace(errb))
82 }
83 var names []string
84 if err := json.Unmarshal([]byte(out), &names); err != nil {
85 return nil, fmt.Errorf("listing checks: %w", err)
86 }
87 return names, nil
88}
89
90// runPure builds one flake check over the exported tree and digests what
91// the build named: the derivation's inputs, the derivation, and its
92// output. Building from the derivation path rather than the attribute a
93// second time guarantees the thing built is the thing recorded.
94func runPure(tree, system string, spec checkSpec) (checkOutcome, error) {
95 out := checkOutcome{spec: spec}
96 attr := tree + "#checks." + system + "." + spec.attribute
97
98 drv, errb, err := nix(tree, "eval", "--raw", attr+".drvPath")
99 if err != nil {
100 return out, fmt.Errorf("evaluating %s: %w: %s", attr, err, strings.TrimSpace(errb))
101 }
102 drv = strings.TrimSpace(drv)
103
104 drvBytes, err := os.ReadFile(drv)
105 if err != nil {
106 return out, err
107 }
108 out.derivation = fmt.Sprintf("%x", sha256sum(drvBytes))
109
110 inputs, err := drvInputs(drv)
111 if err != nil {
112 return out, err
113 }
114 out.inputs = inputsDigest(inputs)
115
116 built, buildLog, err := nix(tree, "build", "--no-link", "--print-out-paths", drv+"^*")
117 out.log = strings.TrimSpace(buildLog)
118 if err != nil {
119 return out, nil // a failed check is an outcome, not an error
120 }
121 paths := strings.Fields(built)
122 if len(paths) != 1 {
123 return out, fmt.Errorf("%s produced %d outputs; a check attribute must produce exactly one", spec.name, len(paths))
124 }
125 entries, err := fsEntries(paths[0])
126 if err != nil {
127 return out, err
128 }
129 out.output = treeDigest(entries)
130 out.passed = true
131 return out, nil
132}
133
134// drvInputs collects the store paths a derivation reads: the derivations
135// it depends on and the sources it takes directly. A .drv's references are
136// exactly that set, and `nix-store --query --references` prints them as
137// lines โ€” a shape that has not changed in the lifetime of nix, unlike the
138// versioned JSON of `nix derivation show`.
139func drvInputs(drv string) ([]string, error) {
140 cmd := exec.Command("nix-store", "--query", "--references", drv)
141 var out, errb bytes.Buffer
142 cmd.Stdout = &out
143 cmd.Stderr = &errb
144 if err := cmd.Run(); err != nil {
145 return nil, fmt.Errorf("querying the inputs of %s: %w: %s", drv, err, strings.TrimSpace(errb.String()))
146 }
147 inputs := strings.Fields(out.String())
148 if len(inputs) == 0 {
149 return nil, fmt.Errorf("%s has no inputs at all", drv)
150 }
151 return inputs, nil
152}
153
154// runEffectful runs a command over the exported tree and notarizes what it
155// observed. No digests: there is nothing to re-derive, which is exactly
156// what the effectful predicate type says.
157func runEffectful(tree string, spec checkSpec) checkOutcome {
158 out := checkOutcome{spec: spec}
159 cmd := exec.Command("sh", "-c", spec.command)
160 cmd.Dir = tree
161 log, err := cmd.CombinedOutput()
162 out.log = strings.TrimSpace(string(log))
163 out.observedAt = time.Now().UTC().Format("2006-01-02T15:04:05Z")
164 out.passed = err == nil
165 return out
166}