naersk icon indicating copy to clipboard operation
naersk copied to clipboard

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

Open paulyoung opened this issue 2 years ago • 7 comments

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.

paulyoung avatar Aug 18 '22 20:08 paulyoung

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?

Patryk27 avatar Aug 20 '22 17:08 Patryk27

@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

Qubasa avatar Sep 13 '22 23:09 Qubasa

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.)

Patryk27 avatar Sep 18 '22 12:09 Patryk27

@Patryk27 Oh awesome, thank you! :-)

Qubasa avatar Sep 18 '22 13:09 Qubasa

@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

paulyoung avatar Jan 04 '23 00:01 paulyoung

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

Patryk27 avatar Jan 15 '23 16:01 Patryk27