·5 min read
A Nim image that downloads the compiler, or bootstraps it when there is no tarball
The Nim image for my Pterodactyl yolks had one awkward requirement: nim-lang.org publishes a prebuilt linux_x64 tarball for every release, but a prebuilt linux_arm64 one only for recent releases. A Dockerfile that assumes the tarball exists is a Dockerfile that breaks the day someone builds an older version on an arm64 machine. So the image has to decide, at build time, whether it downloads a compiler or builds one.
- Project
- Pterodactyl yolks
- Date
- Oct 2026
- Stack
- Docker, Bash, GitHub Actions, Nim
- Commit
- f12bf95
The download that is allowed to fail
The core of the Dockerfile is a single RUN with a case on TARGETARCH and an if/else around curl:
case "${TARGETARCH:-amd64}" in \
amd64) arch=x64 ;; \
arm64) arch=arm64 ;; \
*) echo "unsupported arch ${TARGETARCH}" >&2; exit 1 ;; \
esac; \
base="https://nim-lang.org/download"; \
if curl -fsSL -o /tmp/nim.tar.xz "${base}/nim-${NIM_VERSION}-linux_${arch}.tar.xz"; then \
tar -xJf /tmp/nim.tar.xz -C /opt/nim --strip-components=1; \
else \
curl -fsSL -o /tmp/nim.tar.xz "${base}/nim-${NIM_VERSION}.tar.xz"; \
...The -f flag is the whole trick. Without it, curl treats an HTTP 404 as a successful transfer and writes the error page into nim.tar.xz; tar then fails with a confusing message two lines later. With -f, a missing prebuilt archive makes curl exit non-zero, and the if takes the other branch.
Nim's own names differ from Docker's: Docker says amd64, Nim's URLs say x64. The case is the translation table, and it also makes an unknown architecture fail loudly instead of downloading something wrong.
Building the compiler when there is no tarball
The fallback branch downloads the source tarball, which contains the C sources Nim bootstraps from, and then does the full bootstrap:
cd /opt/nim; \
sh build.sh; \
./bin/nim c -d:release --skipUserCfg --skipParentCfg --hints:off koch; \
./koch boot -d:release --skipUserCfg --skipParentCfg --hints:off; \
./koch tools --skipUserCfg --skipParentCfg --hints:off; \
rm -rf c_code nimcache csources* build; \build.sh compiles the C sources into a first, minimal nim. That binary builds koch, the build tool, and koch boot -d:release rebuilds the compiler with itself. koch tools adds nimble and the other tools, which the start script needs. The --skipUserCfg --skipParentCfg flags keep stray config files in the build context from changing the result.
The last line matters for image size. The bootstrap leaves c_code, nimcache and the csources behind, and since it all happens inside one RUN, removing them in the same layer actually shrinks the image. Deleting them in a later RUN would leave them stored in the earlier layer. I did not measure the size difference, so I will not quote one; the reasoning is just how layers work.
This is also why the base image installs build-essential and libssl-dev. Nim compiles to C, so the final image needs gcc at runtime anyway, and -d:ssl links against OpenSSL. The source build costs nothing extra in packages.
Reading the entry point from the .nimble file
The runtime side is nim-start. A Pterodactyl user uploads a project; the script has to work out what to compile. The rule is: NIM_MAIN if set, otherwise the first bin the .nimble file names, under its srcDir. Rather than run nimble dump (which needs a working package to evaluate), the script extracts the value with sed:
nimble_value() {
sed -n "s/^[[:space:]]*$1[[:space:]]*=[[:space:]]*@\{0,1\}\[\{0,1\}[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$nimble_file" | head -n 1
}That one pattern accepts both bin = "server" and bin = @["server"], which are the two shapes a .nimble file uses, and takes the first quoted value. It is deliberately dumb. It is not a Nim parser and does not try to be; if it finds nothing, the script says so and tells the user to set NIM_MAIN instead:
say "$nimble_file names no bin = @[\"...\"], so there is nothing to run. Add one, or set NIM_MAIN on the Startup tab to your entry .nim file."The error messages are written for the person looking at a panel console, not for me. Two .nimble files in one folder is also refused outright, because guessing which one the user meant would mean silently building the wrong project.
After that the script runs nimble install -y --depsOnly, then nim c -d:release --hints:off ${NIM_FLAGS:-} --out:".renode-app/$bin" "$main", and execs the result. NIM_FLAGS exists so -d:ssl or --mm:orc can be set from the Startup tab without editing the script.
Everything goes to stderr
The header comment of nim-start says: "Everything this script says goes to stderr: some entrypoints capture the startup command's stdout and execute it." The say helper does printf ... >&2, and the nimble and compiler output are redirected with >&2 as well. It is a one-line habit, and it is there because the image is meant to work with eggs written for other yolks, whose entrypoints do things with stdout that a chatty build script would corrupt.
Signals
The Dockerfile ends with STOPSIGNAL SIGINT and ENTRYPOINT ["/usr/bin/tini", "-g", "--"]. tini -g forwards signals to the whole process group, so the compiled server, which exec made the direct descendant, gets the stop signal rather than a wrapper shell swallowing it. Together with exec in the start script, there is no shell sitting between Wings and the Nim binary at shutdown.
What I took from it
The pattern I would reuse is "let the fast path fail cleanly, and make the slow path the same shape". One RUN, one set of tools at the end (nim, nimble, symlinked into /usr/local/bin), whichever branch ran, and a final nim --version; nimble --version that fails the build if either binary is not usable. The CI matrix builds 2.2.12 and 2.0.16 for linux/amd64,linux/arm64, with a monthly cron to pick up Debian security updates, so both branches get exercised by the same workflow rather than by a hand test.
#docker#nim#pterodactyl#arm64#bash
Keep reading
Have a project in mind?