railwayapp / nixpacks

App source + Nix packages + Docker = Image
https://nixpacks.com
MIT License
2.51k stars 235 forks source link

Rust with custom rust-toolchain file fails with '/app/.nixpacks/rust-toolchain.toml': No such file or directory #686

Closed ErnWong closed 1 year ago

ErnWong commented 1 year ago

Is there an existing issue for this?

Describe the bug

Trying to build an image for a rust project with a custom rust-toolchain file gives: error: opening file '/app/.nixpacks/rust-toolchain.toml': No such file or directory when trying to evaluate the generated nix file.

Full logs ``` ╔══════════════════════════════ Nixpacks v0.14.0 ══════════════════════════════╗ ║ setup │ binutils, gcc, (rust-bin.fromRustupToolchainFile ./rust- ║ ║ │ toolchain.toml) ║ ║──────────────────────────────────────────────────────────────────────────────║ ║ build │ mkdir -p bin ║ ║ │ cargo build --release ║ ║ │ cp target/release/rust-custom-toolchain bin ║ ║──────────────────────────────────────────────────────────────────────────────║ ║ start │ ./bin/rust-custom-toolchain ║ ╚══════════════════════════════════════════════════════════════════════════════╝ [+] Building 22.7s (8/13) => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.35kB 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for ghcr.io/railwayapp/nixpacks:debian-1668470745 0.0s => [internal] load build context 0.0s => => transferring context: 3.44kB 0.0s => [stage-0 1/9] FROM ghcr.io/railwayapp/nixpacks:debian-1668470745 0.0s => CACHED [stage-0 2/9] WORKDIR /app/ 0.0s => CACHED [stage-0 3/9] COPY .nixpacks/nixpkgs-a0b7e70db7a55088d3de0cc370a59f9fbcc906c3.nix .nixpacks/nixpkgs-a0b7e70db7a55088d3de0cc370a59f9fbcc906c3.nix 0.0s => ERROR [stage-0 4/9] RUN nix-env -if .nixpacks/nixpkgs-a0b7e70db7a55088d3de0cc370a59f9fbcc906c3.nix && nix-collect-garbage -d 22.6s ------ > [stage-0 4/9] RUN nix-env -if .nixpacks/nixpkgs-a0b7e70db7a55088d3de0cc370a59f9fbcc906c3.nix && nix-collect-garbage -d: #8 21.36 installing 'a0b7e70db7a55088d3de0cc370a59f9fbcc906c3-env' #8 21.69 error: opening file '/app/.nixpacks/rust-toolchain.toml': No such file or directory #8 21.69 (use '--show-trace' to show detailed location information) ------ executor failed running [/bin/bash -ol pipefail -c nix-env -if .nixpacks/nixpkgs-a0b7e70db7a55088d3de0cc370a59f9fbcc906c3.nix && nix-collect-garbage -d]: exit code: 1 Error: Docker build failed ```

I wondered if it was caused by this early return...

https://github.com/railwayapp/nixpacks/blob/1a37a464e187781d5670d8b0abf9d3a823574663/src/nixpacks/builder/docker/dockerfile_generation.rs#L342-L344

...which would mean that the following file-dependency wouldn't be copied over...

https://github.com/railwayapp/nixpacks/blob/1a37a464e187781d5670d8b0abf9d3a823574663/src/providers/rust.rs#L66

However, even if I comment out the early return and convince Nixpacks to generate a COPY docker command for the rust-toolchain file, it seems that:

  1. It would be copied too late, because it seems like the nix file is being evaluated before the setup phase in which the rust-toolchain file is being copied.
  2. I think the nix file will be trying to read the rust toolchain from the wrong directory (/app/.nixpacks) other than the one it gets copied to (presumably /app/).

Btw, just a heads up in case you're not aware of it (but algoods if you're already aware :smile: ), it seems that examples/rust-custom-toolchain is not covered by the tests at the moment.

To reproduce

In Nixpacks repo commit https://github.com/railwayapp/nixpacks/commit/1a37a464e187781d5670d8b0abf9d3a823574663, run

cargo run -- build ./examples/rust-custom-toolchain

Expected behavior

Nixpacks generates the image successfully.

Environment

I first found this when trying to deploy https://github.com/ErnWong/dango-tribute/commit/c0fd1ee1ff332fc35705ad3fa426f8b97b14518a onto https://railway.app/ (which was using Nixpacks version 14.0)

I also encountered this error when running that example locally on my machine:

ErnWong commented 1 year ago

Thanks for the fix! 🍻