# Lean image for the birch-swinnerton-dyer `build` check (checks/build.py). # # Build with a checkout of the project's main as the context, not this folder (docker/build.sh does it, with the # mirrors a host in Beijing needs): # git clone /tmp/bsd-main # sh /tmp/bsd-main/docker/build.sh /tmp/bsd-main # # The project's main is b-mehta/EllipticCurveRank at its pinned commit, without .github (upstream.json `drop`), with # this project's Statement.lean, Challenge.lean, Solution.lean, PrintAxioms.lean, comparator.json and lakefile.toml on # top, and whatever has been merged since. The image holds it fully built in /opt/proof. The check runs with # --network none: it reads a commit's tree, uses /opt/proof/.lake and runs lake build, the exporter and # `lake comparator`. Nothing is downloaded at check time. # # Every pin is a full hash in an ARG below, checked once it is fetched. See README.md for where each comes from. FROM ubuntu:24.04 # --- Where the downloads come from ------------------------------------------ # The defaults are each project's own servers. From Beijing (docker/build.sh with MIRRORS=cn): the Ubuntu archive by # plain http from Aliyun (ca-certificates is not installed yet at the first apt step), and the Lean release from # SJTU's mirror: github.com, where releases.lean-lang.org sends elan for it, gave this host the file at anything from # 17 KB/s to 20 MB/s within ten minutes (12-02). raw.githubusercontent.com is not used: Beijing cannot reach it. ARG APT_MIRROR= ARG LEAN_DL=https://github.com/leanprover/lean4/releases/download # --- Pins ------------------------------------------------------------------- # b-mehta/EllipticCurveRank (ECCompute), which the project starts from, and its lean-toolchain at that commit. The # context must hold that toolchain; LEAN_SHA256 is the digest GitHub lists for lean-4.36.0-linux.tar.zst. ARG UPSTREAM_COMMIT=9cb92e9a7ddf28666d89cb221a89fb35de75b4d3 ARG TOOLCHAIN=leanprover/lean4:v4.36.0 ARG LEAN_SHA256=2b7ab1ed8b92d2e6abfaf41344823b510064dbc9c0260cac73eb496ab2dea6eb # From the project's lake-manifest.json (lakefile.toml asks for Mathlib's tag v4.36.0). ARG MATHLIB_COMMIT=fc4f59e577feb1a4956671bc04ac18aaaf8cd842 # Comparator: since v4.35.0 Lean ships it as `lake comparator` (sandboxed with bubblewrap), with the exporter and # nanoda_bin beside it, so nothing is built for it here. # --- System packages -------------------------------------------------------- # python3 runs the check; curl and zstd are what Lake and Mathlib's cache tool use; bubblewrap is the sandbox # `lake comparator` runs in where user namespaces are allowed (the check's container has none: checks/build.py). ENV DEBIAN_FRONTEND=noninteractive RUN if [ -n "$APT_MIRROR" ]; then sed -i "s#http://archive.ubuntu.com/ubuntu#$APT_MIRROR#; s#http://security.ubuntu.com/ubuntu#$APT_MIRROR#" /etc/apt/sources.list.d/ubuntu.sources; fi \ && apt-get update && apt-get install -y --no-install-recommends \ ca-certificates curl git python3 build-essential zstd bubblewrap \ && rm -rf /var/lib/apt/lists/* # The check runs Lake as another user than the one that built; let git (Lake asks it about each package) read every # repository in the image. RUN git config --system --add safe.directory '*' # clone-at : that one commit of a repository, without its history, checked out and checked. From # Beijing a connection to github.com times out, stalls or breaks off every few tries (12-02), and Lake clones each # package once and does not try again: so every package of the manifest is fetched this way before Lake runs, a fetch # that stalls is cut off, and each is tried ten times. RUN printf '%s\n' '#!/bin/sh' 'set -eu' \ 'n=0; until rm -rf "$2" && git init -q "$2" && git -C "$2" remote add origin "$1" && timeout 600 git -C "$2" -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=30 fetch -q --depth 1 origin "$3"; do n=$((n+1)); [ "$n" -lt 10 ]; sleep 5; done' \ 'git -C "$2" -c advice.detachedHead=false checkout -q FETCH_HEAD' \ 'test "$(git -C "$2" rev-parse HEAD)" = "$3"' > /usr/local/bin/clone-at \ && chmod 755 /usr/local/bin/clone-at # --- Lean ------------------------------------------------------------------- # elan from its GitHub release. The toolchain as its release file, checked against the SHA-256 GitHub lists for the # asset, unpacked where elan keeps a toolchain of that name (elan then takes it for installed), and made the default. ENV ELAN_HOME=/opt/elan ENV PATH=/usr/local/bin:/opt/elan/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin RUN curl -sSfL --retry 8 --retry-all-errors --connect-timeout 20 https://github.com/leanprover/elan/releases/download/v4.1.2/elan-x86_64-unknown-linux-gnu.tar.gz -o /tmp/elan.tgz \ && tar -xzf /tmp/elan.tgz -C /tmp && /tmp/elan-init -y --no-modify-path --default-toolchain none && rm /tmp/elan-init /tmp/elan.tgz RUN v="${TOOLCHAIN#*:v}" && dir="/opt/elan/toolchains/$(echo "${TOOLCHAIN}" | sed 's#/#--#; s#:#---#')" \ && curl -sSfL --retry 8 --retry-all-errors --connect-timeout 20 -C - "${LEAN_DL}/v$v/lean-$v-linux.tar.zst" -o /tmp/lean.tar.zst \ && echo "${LEAN_SHA256} /tmp/lean.tar.zst" | sha256sum -c - \ && mkdir -p "$dir" && tar --zstd -xf /tmp/lean.tar.zst -C "$dir" --strip-components 1 && rm /tmp/lean.tar.zst \ && elan default "${TOOLCHAIN}" && chmod -R a+rX /opt/elan \ && lean --version | grep -q "version $v" # --- The dependencies ------------------------------------------------------- # From the three files that name them, so that a change elsewhere in main does not fetch Mathlib's cache (5 GB) again. # Each package of the manifest at its commit first (clone-at), then Mathlib's oleans: a few of the cache's downloads # fail now and then, and each try fetches only what is missing. Its download store (~/.cache/mathlib) is unpacked # into .lake by then and removed. The check compiles a commit as `nobody`, and the cache unpacks its files 0600: they # are made readable here, in the layer that makes them (a chmod in a later layer copies every file it visits). # GIT_MIRROR (docker/build.sh with MIRRORS=cn: https://mirror.sjtu.edu.cn/git/lean4-packages) serves the seven # packages it holds, Mathlib among them, at the same commits (each is checked below or by Lake against the manifest); # the others still come from github.com. The packages keep their github.com addresses, and the setting, root's own, # is removed once they are fetched. On 12-02 github.com could not be reached from Beijing for minutes at a time. ARG GIT_MIRROR= WORKDIR /opt/proof COPY lakefile.toml lake-manifest.json lean-toolchain /opt/proof/ RUN test "$(cat lean-toolchain)" = "${TOOLCHAIN}" \ && if [ -n "$GIT_MIRROR" ]; then for r in leanprover-community/mathlib4 leanprover-community/batteries leanprover-community/aesop \ leanprover-community/quote4 leanprover-community/ProofWidgets4 leanprover-community/import-graph leanprover/lean4-cli; do \ git config --global url."$GIT_MIRROR/${r#*/}".insteadOf "https://github.com/$r"; done; fi \ && python3 -c 'import json; [print(p["url"], ".lake/packages/" + p["name"], p["rev"]) for p in json.load(open("lake-manifest.json"))["packages"]]' \ | while read -r url dir rev; do clone-at "$url" "$dir" "$rev" || exit 1; done \ && rm -f /root/.gitconfig \ && test "$(git -C .lake/packages/mathlib rev-parse HEAD)" = "${MATHLIB_COMMIT}" \ && (lake exe cache get || lake exe cache get || lake exe cache get) \ && test "$(git -C .lake/packages/mathlib rev-parse HEAD)" = "${MATHLIB_COMMIT}" \ && rm -rf /root/.cache/mathlib \ && chmod -R a+rX /opt/proof # --- The project ------------------------------------------------------------ # The context is a checkout of the project's main. A full build of every library of lakefile.toml (the default # targets ECCompute, Curves and Solution, then Statement and Challenge) and of the axiom print follows, so a pull request's build recompiles only what it changed. The 480 # curve files are the work: each reduces its certificate in the kernel (README.md has the time); # and the build becomes `nobody`'s (checks/build.py can then build a commit on top of it in place, `in_place` in # checks/build.json, instead of copying it for every job). Only what lacks read access is chmod-ed: a chmod of all of # /opt/proof here would copy Mathlib into this layer. COPY . /opt/proof RUN rm -rf .git \ && test -f Statement.lean && test -f Challenge.lean && test -f Curves.lean && test ! -e lakefile.lean \ && grep -q "${UPSTREAM_COMMIT}" docker/lean.Dockerfile \ && lake build ECCompute Curves Statement Solution Challenge \ && lake env lean PrintAxioms.lean > /dev/null \ && grep -q "${MATHLIB_COMMIT}" lake-manifest.json \ && find .lake/build -name "*.setup.json" -delete \ && chown -R nobody:nogroup .lake/build \ && find /opt/proof \( -type f ! -perm -004 -o -type d ! -perm -005 \) -exec chmod a+rX {} + # --- Comparator's second kernel ---------------------------------------------- # The toolchain's own nanoda_bin and leanchecker, on PATH for `lake comparator`. # (elan 4.1.2 has no `elan which --toolchain`; the default toolchain's prefix is the same folder.) RUN ln -s "$(lean --print-prefix)/bin/nanoda_bin" /usr/local/bin/nanoda_bin \ && ln -s "$(lean --print-prefix)/bin/leanchecker" /usr/local/bin/leanchecker \ && test -x /usr/local/bin/nanoda_bin && test -x /usr/local/bin/leanchecker \ && lake comparator --help >/dev/null # Every tool must be on PATH. RUN for t in bwrap nanoda_bin leanchecker lake lean git python3 which; do \ command -v "$t" >/dev/null || { echo "missing: $t"; exit 1; }; \ done ENV PROOF_BASE=/opt/proof WORKDIR /opt/proof CMD ["/bin/bash"]