postjevsql.git / nix / offline_archive.bzl
1# The nix package build's stand-in for reindeer's http_archive: the
2# sandbox has no network, and buck2's downloader takes no file:// URL
3# (measured 2026-09-26: "Error building request: invalid format"). nix
4# fetches every crate by the sha256 third-party/BUCK records and places
5# it at third-party/crates/<name>; package.nix loads this over the
6# builtin, so the BUCK file itself is unchanged. The output directory is
7# named after the target, as http_archive's is, because the generated
8# crate_roots are written relative to it.
9
10def _extract_impl(ctx):
11    out = ctx.actions.declare_output(ctx.label.name, dir = True)
12    ctx.actions.run(
13        cmd_args(
14            "sh",
15            "-c",
16            'mkdir -p "$1" && tar -xzf "$2" -C "$1" --strip-components="$3"',
17            "--",
18            out.as_output(),
19            ctx.attrs.src,
20            str(ctx.attrs.strip_components),
21        ),
22        category = "extract_crate",
23    )
24    return [DefaultInfo(default_output = out)]
25
26_extract = rule(
27    impl = _extract_impl,
28    attrs = {
29        "src": attrs.source(),
30        "strip_components": attrs.int(),
31    },
32)
33
34def http_archive(name, sha256, strip_prefix, urls, visibility = None, **_kwargs):
35    _extract(
36        name = name,
37        src = "crates/" + name,
38        strip_components = len(strip_prefix.split("/")),
39        visibility = visibility,
40    )