# Lean image for the p-vs-np `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 mirror a host in Beijing needs): # git clone /tmp/pnp-main # sh /tmp/pnp-main/docker/build.sh /tmp/pnp-main # # The project is not a copy of a repository that can be cloned: it is the # import closure of the P vs NP files of lean-dojo/LeanMillenniumPrizeProblems # at 603053dc (3 of its Lean files, see upstream.json), with this project's own # lakefile.toml and lake-manifest.json, which require Mathlib alone. Cloning # upstream would bring the other six problems and its Physlib dependency. So the # image is built from the project's own tree: the context is a checkout of # main, and COPY puts it in /opt/proof. The check runs with --network none: it # copies a commit's tree to a scratch directory, gives it /opt/proof/.lake, and # runs lake build. Nothing is downloaded at check time. # # P vs NP is open: checks/build.json names no theorem and no Comparator # configuration, so the image holds no Comparator, exporter, sandbox or second # kernel. When a person who maintains the project adds a Comparator challenge # (a claimed solution), the image gains them, as problems/navier-stokes's does # for a toolchain before v4.35.0. # # Every pin is a full commit hash in an ARG below, and each is checked. See # README.md for where each pin comes from. FROM ubuntu:24.04 # --- Pins ------------------------------------------------------------------- # lean-dojo/LeanMillenniumPrizeProblems, whose P vs NP files the project # starts from, and its lean-toolchain at that commit. The context must hold # that toolchain. ARG UPSTREAM_REPO=https://github.com/lean-dojo/LeanMillenniumPrizeProblems ARG UPSTREAM_COMMIT=603053dc267cf3efe422f438eb78098c0ececd6f ARG TOOLCHAIN=leanprover/lean4:v4.31.0 # From the project's lake-manifest.json (upstream's pin of Mathlib, tag # v4.31.0, and Mathlib's own dependencies as upstream's manifest lists them); # checked after the dependencies are fetched. ARG MATHLIB_COMMIT=fabf563a7c95a166b8d7b6efca11c8b4dc9d911f ARG BATTERIES_COMMIT=fa08db58b30eb033edcdab331bba000827f9f785 ARG AESOP_COMMIT=e3cb2f741431ce31bf73549fb52316a57368b06f # --- System packages -------------------------------------------------------- # On the Beijing host: --build-arg APT_MIRROR=http://mirrors.aliyun.com/ubuntu # (plain http: ca-certificates is not installed yet at this step). ARG APT_MIRROR= # python3, curl and zstd are what Lake and Mathlib's cache tool use; python3 is # also what runs checks/build.py and checks/challenge.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 \ && rm -rf /var/lib/apt/lists/* # The check may run as another user than the one that built; let git (and # Lake, which calls git in each package) read every repository in the image. RUN git config --system --add safe.directory '*' # --- Lean ------------------------------------------------------------------- # elan from its GitHub release: raw.githubusercontent.com, where elan-init.sh # lives, is unreachable from the Beijing host. The check compiles a commit as # `nobody`, so everything under /opt is made readable in the layer that makes it. # The toolchain comes through elan from github.com (where releases.lean-lang.org # sends it): SJTU's mirror, which the other problems' images use from Beijing, # does not hold v4.31.0. On 12-02 the 558 MB took 23 minutes there. 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 5 https://github.com/leanprover/elan/releases/download/v4.1.2/elan-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C /tmp \ && /tmp/elan-init -y --no-modify-path --default-toolchain none && rm /tmp/elan-init RUN (elan toolchain install "${TOOLCHAIN}" || elan toolchain install "${TOOLCHAIN}" || elan toolchain install "${TOOLCHAIN}") \ && elan default "${TOOLCHAIN}" \ && chmod -R a+rX /opt/elan # 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 # --- 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}" \ && test "$(git -C .lake/packages/batteries rev-parse HEAD)" = "${BATTERIES_COMMIT}" \ && test "$(git -C .lake/packages/aesop rev-parse HEAD)" = "${AESOP_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 (see the top of this file): the closure of upstream's P vs NP files # at UPSTREAM_COMMIT with this project's files. A full build of every library of lakefile.toml (Problems, Tests: both default targets) follows, so a # pull request's build recompiles only what it changed; Lake must not have rewritten the manifest; # 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 lakefile.toml && test ! -e lakefile.lean \ && test -f Problems/PVersusNP/Millennium.lean \ && grep -q "${UPSTREAM_COMMIT}" docker/lean.Dockerfile \ && lake build Problems Tests \ && grep -q "${MATHLIB_COMMIT}" lake-manifest.json \ && chown -R nobody:nogroup .lake/build \ && find /opt/proof \( -type f ! -perm -004 -o -type d ! -perm -005 \) -exec chmod a+rX {} + RUN for t in 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"]