# Lean image for the riemann-hypothesis `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/rh-main # sh /tmp/rh-main/docker/build.sh /tmp/rh-main # # The project's main is leanprover-community/riemann-hypothesis at its pinned commit, without its GitHub CI # (upstream.json `drop`), with this project's files 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 ------------------------------------------------------------------- # leanprover-community/riemann-hypothesis, 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.37.0-rc1-linux.tar.zst. ARG UPSTREAM_COMMIT=630d1e43b442cab27717c3ebf94d218dea608374 ARG TOOLCHAIN=leanprover/lean4:v4.37.0-rc1 ARG LEAN_SHA256=2e1bbb832870bb81b7131cb70b13aec2cc7ed9f22e2bdbb236b86076ae20382d # From the project's lake-manifest.json; checked once the dependencies are fetched. ARG MATHLIB_COMMIT=31ce06eaadf4a200c0e1407c5f10953cf606b8f8 ARG TAUCETI_COMMIT=2a9233e5be32675476ae438bef6f6dd4ba93eb64 # 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 (3.12) and curl (8.5) run the check and meet the cache scripts' needs (>= 3.11, >= 7.71); bubblewrap is # the sandbox `lake comparator` runs in where user namespaces are allowed (the check's container has none). 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). # (Mathlib's cache by its own name: `lake exe cache` is this project's script, which needs the whole tree.) # 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}" \ && test "$(git -C .lake/packages/TauCeti rev-parse HEAD)" = "${TAUCETI_COMMIT}" \ && export MATHLIB_CACHE_GET_URL=https://cache.mathlib.org/mathlib4 \ && (lake exe mathlib/cache get || lake exe mathlib/cache get || lake exe mathlib/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. Two more caches come first, as scripts/lake-cache-get.sh fetches # them after Mathlib's (`lake exe cache get` itself would fetch Mathlib's 5 GB again): Tau Ceti's oleans, and the # project's own, which upstream's CI published for the src tree, lakefile.toml, lake-manifest.json and toolchain of # its commits. The project's is looked up by the Git tree of src, so the tree is made a commit for the lookup. Exit 3 # means no record matches (our main has moved away from upstream's): `lake build` then compiles what the caches did # not supply, about 4 CPU-hours from nothing. The full build of every library of lakefile.toml (the default targets Lib, New and Main, # then Challenge) follows either way, so a pull request's build recompiles only what it changed. Lake's download store # (.lake/cache, the packed copies of what is now unpacked in .lake/build) is removed once `lake build --no-build` # shows that nothing needs it; # 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 Challenge.lean && test -f src/Statements.lean && test ! -e lakefile.lean \ && grep -q "${UPSTREAM_COMMIT}" docker/lean.Dockerfile \ && git init -q . && echo /.lake/ > .git/info/exclude && git add -A \ && git -c user.name=image -c user.email=image@localhost commit -q -m "main, for the cache lookup" \ && export LAKE_CACHE_DIR=/opt/proof/.lake/cache && ulimit -Sn "$(ulimit -Hn)" \ && (python3 scripts/tauceti-cache-get.py || python3 scripts/tauceti-cache-get.py || python3 scripts/tauceti-cache-get.py) \ && { s=0; for i in 1 2 3; do s=0; python3 scripts/ci-cache-get.py || s=$?; [ "$s" = 0 ] || [ "$s" = 3 ] && break; sleep 10; done; \ echo "project cache: exit $s (0 restored, 3 no record for these inputs)"; [ "$s" = 0 ] || [ "$s" = 3 ]; } \ && unset LAKE_CACHE_DIR && rm -rf .git \ && lake build Lib New Main Challenge \ && grep -q "${MATHLIB_COMMIT}" lake-manifest.json \ && du -sh .lake/cache .lake/build \ && mv .lake/cache /tmp/lake-cache \ && if lake build --no-build && lake build --no-build Challenge; then rm -rf /tmp/lake-cache; else mv /tmp/lake-cache .lake/cache; fi \ && 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"]