Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cargo.toml can't be found when setting both src and root #258

Open
paulyoung opened this issue Aug 18, 2022 · 7 comments
Open

Cargo.toml can't be found when setting both src and root #258

paulyoung opened this issue Aug 18, 2022 · 7 comments

Comments

@paulyoung
Copy link

I've had root = ./backend; for a while but now I'm trying to get Rust to read a .env file at the root of my project (which appears to work outside of using naersk) so I also set src = ./.;

When doing this I get the following:

error: builder for '/nix/store/xpjd289b1q6acigybarlcrc463qz9wcc-rust-workspace-unknown.drv' failed with exit code 101;
       last 10 log lines:
       > [naersk] cargo_build_output_json (created): /private/tmp/nix-build-rust-workspace-unknown.drv-0/tmp.M1pQu5Ig4U
       > [naersk] crate_sources: /nix/store/vmhpc1bs0vy2v111rd6xbc1mb7f9xcna-crates-io
       > [naersk] RUSTFLAGS:
       > [naersk] CARGO_BUILD_RUSTFLAGS:
       > [naersk] CARGO_BUILD_RUSTFLAGS (updated): --remap-path-prefix /nix/store/vmhpc1bs0vy2v111rd6xbc1mb7f9xcna-crates-io=/sources
       > [naersk] pre-installing dep /nix/store/0rkjxcfsfgyrvw3cw11s56syhy4ggkkv-rust-workspace-deps-unknown
       > building
       > cargo build $cargo_release -j "$NIX_BUILD_CORES" --message-format=$cargo_message_format --package mypackage
       > error: could not find `Cargo.toml` in `/private/tmp/nix-build-rust-workspace-unknown.drv-0/jgkw76d24gajlxjwzkl0ddvxbsdjcfj6-source` or any parent directory
       > [naersk] cargo returned with exit code 101, exiting

My understanding is that root should be used to read Cargo.toml.

@Patryk27
Copy link
Contributor

Hmm, reading files should work as-is (i.e. if it works during cargo build, it should work when compiling code through Naersk, too).

Would you mind posting some code that I could check?

@Qubasa
Copy link

Qubasa commented Sep 13, 2022

@Patryk27 I have the same issue. Code to reproduce the issue lies here: https://github.com/Luis-Hebendanz/perf_kernel/blob/2d7fc0156fe8346d92d763c908f4f15aeb92d1d1/flake.nix#L106

Error:

error: builder for '/nix/store/p7vpfx0qyik4dzq369521kd19bpm6h8r-perf_kernel-0.1.0.drv' failed with exit code 101;
       last 10 log lines:
       > [naersk] cargo_bins_jq_filter: select(.reason == "compiler-artifact" and .executable != null and .profile.test == false)
       > [naersk] cargo_build_output_json (created): /build/tmp.d5shNb2HtH
       > [naersk] crate_sources: /nix/store/xwizkxl4ppkqxpbrqs7k04bb2i7gbs81-dependencies
       > [naersk] RUSTFLAGS:
       > [naersk] CARGO_BUILD_RUSTFLAGS:
       > [naersk] CARGO_BUILD_RUSTFLAGS (updated): --remap-path-prefix /nix/store/xwizkxl4ppkqxpbrqs7k04bb2i7gbs81-dependencies=/sources
       > building
       > cargo build $cargo_release -j "$NIX_BUILD_CORES" --message-format=$cargo_message_format
       > error: could not find `Cargo.toml` in `/build/jr9jyynvixwc49b7rj2nmv3l73c0f8w0-source` or any parent directory
       > [naersk] cargo returned with exit code 101, exiting

@Patryk27
Copy link
Contributor

Patryk27 commented Sep 18, 2022

Ah, I see - yes, root = seems not to be working (and, apparently, for this particular use case where src and root resolve to different directories, this option couldn't have ever worked 🤔).

In general, adding a preBuild step should do it:

buildPackage {
  src = ./.; 
  root = ./package;
  preBuild = "cd package";
}

... so e.g. in your, @luis-hebendanz, case:

diff --git a/flake.nix b/flake.nix
index 3fe10ba..5f92095 100644
--- a/flake.nix
+++ b/flake.nix
@@ -45,7 +45,10 @@
   outputs = { self, nixpkgs, naersk, nci, rust-overlay, glue-gun, nix-ipxe, nix-parse-gdt, vmsh-flake, flake-utils, nixos-codium, ... }:
     flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
       let
-        naersk-lib = pkgs.callPackage naersk { };
+        naersk-lib = pkgs.callPackage naersk {
+          cargo = myrust;
+          rustc = myrust;
+        };
         overlays = [ (import rust-overlay) ];
         pkgs = import nixpkgs {
           inherit system overlays;
@@ -105,8 +108,10 @@
       rec {
         packages.default = naersk-lib.buildPackage {
           src = ./.;
-          buildInputs = buildDeps;
           root = ./kernel;
+          preBuild = "cd kernel";
+          buildInputs = buildDeps;
+
           #remapPathPrefix = false;
           singleStep = true;
         };

(note that the compilation will still fail, but at least with a different error message - error: no matching package named compiler_builtins found; to overcome that, you could try downloading compiler_builtins and putting it explicitly in your Cargo.toml.)

@Qubasa
Copy link

Qubasa commented Sep 18, 2022

@Patryk27 Oh awesome, thank you! :-)

@paulyoung
Copy link
Author

@Patryk27 I just ran into this again for different reasons and tried your suggestion:

buildPackage {
  src = ./.; 
  root = ./backend;
  preBuild = "cd backend";
}

This didn't work for me because when preBuild runs it's already inside of the backend directory.

Trying to use src = ./.; and/or root = ./.; appears to fail before preBuild runs, with errors like error: getting status of '/nix/store/wg0yjjpq25cm8nm6zqlv6i097fc4n9dx-source/Cargo.toml': No such file or directory

@Patryk27
Copy link
Contributor

@paulyoung: would you mind preparing some repository I could git clone and nix build / nix-build? 👀

@clotodex
Copy link

clotodex commented Aug 5, 2024

with "cd ." it starts compiling but then fails post installation with the "cant find toml" error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants