gunk-dev/the-valley

darwin.nix

sourcecaaa5bee5e11main's published tip history
4.1 KiB
1# What the flake offers on aarch64-darwin (flake.nix): the client programs
2# and the formatter. No Darwin machine runs these checks, so they prove
3# what x86_64-linux can: that each client program's Go compiles, links and
4# vets for darwin/arm64, and that every Darwin output evaluates. Nothing is
5# run on Darwin, and no Darwin derivation is built.
6{
7 pkgs,
8 lib,
9 self,
10 packages,
11 ...
12}:
13let
14 darwin = "aarch64-darwin";
15
16 # One client program's Go build (clientPrograms, nix/packages.nix)
17 # compiled for darwin/arm64 here, by valley-darwin-build.sh in place of its
18 # build: the package's own source, modules and vendored modules, with
19 # Darwin's GOOS and GOARCH and no cgo, as the Darwin package builds. The
20 # version stamp and the store paths compiled into the program are left
21 # out: they change no compile, and the stamp would change the check with
22 # every commit.
23 darwinBuild =
24 build:
25 build.overrideAttrs (old: {
26 pname = "${old.pname}-darwin";
27 version = "0";
28 ldflags = [ ];
29 env = old.env // {
30 GOOS = "darwin";
31 GOARCH = "arm64";
32 CGO_ENABLED = 0;
33 };
34 # The package's build targets, or all of its module when it names none.
35 targets = if old.subPackages or [ ] == [ ] then [ "./..." ] else map (p: "./${p}") old.subPackages;
36 buildPhase = ''
37 runHook preBuild
38 ${builtins.readFile ./valley-darwin-build.sh}
39 runHook postBuild
40 '';
41 doCheck = false;
42 installPhase = ''
43 { echo "$pname:"; sed 's/^/ /' "$TMPDIR/vetted"; } > $out
44 '';
45 dontFixup = true;
46 });
47
48 # Every derivation the flake offers on Darwin, with the output that
49 # offers it.
50 outputs =
51 lib.mapAttrsToList (n: p: {
52 name = "packages.${darwin}.${n}";
53 drv = p;
54 }) self.packages.${darwin}
55 ++ [
56 {
57 name = "formatter.${darwin}";
58 drv = self.formatter.${darwin};
59 }
60 ];
61 discard = builtins.unsafeDiscardStringContext;
62
63 # What is wrong with the Darwin outputs: a derivation built for another
64 # system, and an app whose program no Darwin derivation holds.
65 holds = app: lib.any (o: lib.hasPrefix "${discard o.drv.outPath}/" (discard app.program)) outputs;
66 wrongSystem = lib.filter (o: o.drv.system != darwin) outputs;
67 strayApps = lib.filterAttrs (_: app: !holds app) self.apps.${darwin};
68 faults =
69 map (o: "${o.name} is for ${o.drv.system}") wrongSystem
70 ++ lib.mapAttrsToList (
71 n: app: "apps.${darwin}.${n} runs ${discard app.program}, which no Darwin output holds"
72 ) strayApps;
73in
74{
75 # Each client program's Go build for darwin/arm64: its package's build
76 # targets linked, and go vet over every package of this repository they
77 # are built from. Each program is a derivation of its own, so a change to
78 # one module compiles again only the programs built from it. The check
79 # lists, per program, the packages it vetted.
80 valley-darwin-build = pkgs.runCommand "valley-darwin-build" {
81 builds = map darwinBuild (lib.attrValues packages.clientPrograms);
82 } "cat $builds > $out";
83
84 # Every Darwin package, app and formatter, held to evaluating: an input
85 # with no Darwin build fails here rather than on a Mac. Each derivation
86 # path is forced while this check is evaluated, and its string context is
87 # discarded, so the Darwin derivations are written to the store but are
88 # not inputs of this one. Building this check builds and substitutes
89 # nothing for Darwin. A derivation for another system, or an app running
90 # a program no Darwin derivation holds, fails its evaluation. The report
91 # is one line per output: the derivation it is, or the program it runs.
92 valley-darwin-eval =
93 if faults != [ ] then
94 throw "valley-darwin-eval: ${lib.concatStringsSep "; " faults}"
95 else
96 pkgs.runCommand "valley-darwin-eval" {
97 report =
98 lib.concatMapStrings (o: "${o.name} ${discard o.drv.drvPath}\n") outputs
99 + lib.concatStrings (
100 lib.mapAttrsToList (n: a: "apps.${darwin}.${n} ${discard a.program}\n") self.apps.${darwin}
101 );
102 passAsFile = [ "report" ];
103 } (builtins.readFile ./valley-darwin-eval.sh);
104}