Files
ianitor/scripts/release-pin.mjs
T

128 lines
5.1 KiB
JavaScript

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// The release's Nix pin (docs/verify.md): .fabrica/nix/pin.json, written by scripts/nix-pin.sh on
// Linux at a clean commit and committed before tagging, becomes target/nix/ianitor-nix.json,
// published under SHA256SUMS signed by domi-release.
//
// The pin is a claim about the code it was built from, so it is refused unless the tagged commit
// holds exactly that code: its source tree (every tracked file except pin.json itself) must be
// the one the pin recorded. That is `git diff --name-only <pinned> <tag>` being empty or only
// pin.json, checked without the pinned commit's history (a runner's clone is shallow).
//
// The build reference verifiers use is the tagged commit: it has the pinned source by that check,
// and it surely exists. The commit the pin names is kept for the record only; when its objects
// are here it must hold the recorded source tree too (a forged commit is refused), and when they
// are not (a shallow clone, or a commit rebased away) it is published unchecked.
//
// node scripts/release-pin.mjs <X.Y.Z> <tag> <commit> write target/nix/ianitor-nix.json
// node scripts/release-pin.mjs --source-tree <commit> print that commit's source-tree hash
// $IANITOR_REPO names another repository (tests). Exit codes: 0 written, 1 refused, 2 usage.
import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const PIN = ".fabrica/nix/pin.json";
const repo = process.env.IANITOR_REPO
? resolve(process.env.IANITOR_REPO)
: resolve(dirname(fileURLToPath(import.meta.url)), "..");
function git(...args) {
return execFileSync("git", args, { cwd: repo, encoding: "utf8", maxBuffer: 64 << 20 });
}
/// The source tree of a commit: sha256 over `git ls-tree -r --full-tree` without the pin's line.
export function sourceTree(commit) {
const lines = git("ls-tree", "-r", "--full-tree", commit)
.split("\n")
.filter((l) => l && !l.endsWith(`\t${PIN}`));
return createHash("sha256").update(lines.join("\n") + "\n").digest("hex");
}
function fail(msg) {
console.error(msg);
process.exit(1);
}
const args = process.argv.slice(2);
if (args[0] === "--source-tree" && args.length === 2) {
try {
console.log(sourceTree(args[1]));
} catch (e) {
fail(`cannot read the tree of ${args[1]}: ${e.message}`);
}
process.exit(0);
}
const [version, tag, commit] = args;
if (args.length !== 3 || !/^\d+\.\d+\.\d+$/.test(version) || !/^[0-9a-f]{40}$/.test(commit ?? "")) {
console.error("usage: node scripts/release-pin.mjs <X.Y.Z> <tag> <commit sha> | --source-tree <commit>");
process.exit(2);
}
let pin;
try {
pin = JSON.parse(git("show", `${commit}:${PIN}`));
} catch (e) {
fail(`no usable ${PIN} at ${commit} (run scripts/nix-pin.sh on Linux and commit it): ${e.message.split("\n")[0]}`);
}
if (pin.version !== version) {
fail(`${PIN} is for ${pin.version}, the tag is ${version}: run scripts/nix-pin.sh again`);
}
if (!/^[0-9a-f]{40}$/.test(pin.commit ?? "") || !/^[0-9a-f]{64}$/.test(pin.sourceTree ?? "")) {
fail(`${PIN} names no commit and source tree (made by an older nix-pin.sh): run scripts/nix-pin.sh again`);
}
for (const attr of ["ianitor", "ianitor-web"]) {
const o = pin.outputs?.[attr];
if (!o || !/^\/nix\/store\/[0-9a-z]{32}-/.test(o.storePath) || !/^sha256[-:][A-Za-z0-9+/=]{43,52}$/.test(o.narHash)) {
fail(`${PIN}: ${attr} has no store path and NAR hash`);
}
}
const here = sourceTree(commit);
if (here !== pin.sourceTree) {
fail(
`${PIN} was made at ${pin.commit}, and ${tag} (${commit}) is not the code of that commit ` +
`(its files differ beyond pin.json): run scripts/nix-pin.sh at the commit to tag`,
);
}
let commitChecked = false;
try {
git("cat-file", "-e", `${pin.commit}^{commit}`);
commitChecked = true;
} catch {
// Not here: informational only.
}
if (commitChecked && sourceTree(pin.commit) !== pin.sourceTree) {
fail(
`${PIN} names ${pin.commit}, which does not hold the source tree it records: ` +
`the pin was edited by hand; run scripts/nix-pin.sh again`,
);
}
const out = {
name: "Ianitor Domi",
version,
tag,
// Build this one (docs/verify.md): the tagged commit, whose source the pin is of.
taggedCommit: commit,
flake: `git+https://gitea.marek-novak.cz/mnovak/Ianitor.git?rev=${commit}`,
// The commit nix-pin.sh ran at, for the record; checked against the source tree when present.
commit: pin.commit,
commitChecked,
sourceTree: pin.sourceTree,
nixpkgs: pin.nixpkgs ?? null,
system: pin.system,
outputs: pin.outputs,
verify: "docs/verify.md",
};
mkdirSync(join(repo, "target/nix"), { recursive: true });
writeFileSync(join(repo, "target/nix/ianitor-nix.json"), JSON.stringify(out, null, 2) + "\n");
console.log(
`target/nix/ianitor-nix.json: ${version} at ${commit} (pinned at ${pin.commit}, ${commitChecked ? "checked" : "not here, unchecked"})`,
);