I run the container using nix run .#serverContainer (or podman run docker-archive:/dev/stdin).
The container loads and then throws this error:
{"msg":"exec container process `/nix/store/ii4fzmg0m5zaixdsv0wv3w4myw2jgq9m-nixflymc-server-0.0.0/bin/nixflymc-server`: Exec format error","level":"error","time":"2024-10-31T22:02:38.880181Z"}
Then it exits.
However, if I call the Nix package directly with nix run .#server the software works as expected.
Why?
Steps to Reproduce
git clone https://github.com/mboyea/nixflymc
cd nixflymc
nix run .#server
Observe that this starts a Minecraft server
nix run .#serverContainer
Observe that this does not start a Minecraft server
Additional Information
Get a local copy of the Docker container ./result using nix build .#serverImage
Well no unfortunately, assuming you mean the mkDerivation installPhase in src/server.nix.
server.nix works fine when run directly by nix run .#server so the script itself must work.
installPhase is wrapped by mkDerivation to include other bash lines like set -euo pipefail, so a shebang wouldn’t do anything because it’s not at the top of the execution (kernel would never see it).
I went ahead and tried to add the shebang as you suggested, and it made no difference.
In bed on phone so can’t fiddle with it, unfortunately. I meant the $out/bin/${pname} script might need a shebang, but I agree, I can’t see why it would work outside the container…
Gah, older versions of nix seem to use execvp() for nix run, which will try to run a script with /bin/sh if there’s no shebang. Guessing your environment has that link and the docker image doesn’t?
I’ve compiled a “distroless” Docker image, which means it doesn’t include an operating system, and by extension, perhaps it doesn’t include a shell by default. Maybe if I were to manually include a shell in the image, and also a shebang, the program could run.
Of course it can’t run without a shell…
I’ll post back here with what I find.
But it turns out that fromImage = bash; isn’t necessary, and it does suffice to just add the shebang by itself. Perhaps bash is included with the base image unlike Docker’s “scratch” image, or maybe it detects the dependency. Either way, I’m pretty impressed!