Trying cargo and getting lost

I thought I was starting to understand things. Turns out, nope at all :slight_smile:

Ok so i start a litte rust project; started with Arch, then git pull on Nix to try to compile. Previously i installed system wide rustup but i read nix-shell is more appropriate for dev, so here we go, I removed everything from system-wide, garbage collected, rebooted… I have a shell.nix file where I have cargo and rustc.

I have an error when trying to run cargo build. But curiously, outside of nix-shell, i can still run cargo! if i look at which cargo, and look at

ls -la /run/current-system/sw/bin/cargo

Then result is

/nix/store/x09izmf44ykb98j60pv83lmqh1if7d73-cargo-1.82.0/bin/cargo

When i enter the directory and run nix-shell, then which cargo directly link to the same file /nix/store/x09izmf44ykb98j60pv83lmqh1if7d73-cargo-1.82.0/bin/cargo . Is that correct? the shell cargo is available system wide ?

The cargo build command fails but first I need to understand if system is clean or not…

Thanks much

If /run/current-system/sw/bin/cargo exists then cargo is installed system wide. The nix shell doe not affect /run/current-system/sw.

1 Like

Which one?

Then you haven’t removed everything.

1 Like

Oh, not sure why but after more rebuild cargo indeed is no more system wide. At least this looks more logicial now despite I do not understand why Cargo did not “disappear” earlier.

So now I may focus on the error;. The cargo build runs fine inside Arch and has following input here

$ cargo build
warning: no edition set: defaulting to the 2015 edition while the latest is 2021
   Compiling proc-macro2 v1.0.95
   Compiling unicode-ident v1.0.18
   Compiling utf8parse v0.2.2
   Compiling anstyle v1.0.10
   Compiling anstyle-parse v0.2.6
   Compiling colorchoice v1.0.3
   Compiling anstyle-query v1.1.2
   Compiling is_terminal_polyfill v1.70.1
   Compiling strsim v0.11.1
   Compiling serde v1.0.219
   Compiling anstream v0.6.18
error: failed to run custom build command for `proc-macro2 v1.0.95`

Caused by:
  could not execute process `/home/luyten/contrib/intime/target/debug/build/proc-macro2-59269202c80ca4ba/build-script-build` (never executed)

Caused by:
  Permission denied (os error 13)
warning: build failed, waiting for other jobs to finish...

I also tried with some edition in Cargo.toml but it is the same output. The file is actually very simple at the moment

$ cat Cargo.toml
[package]
name = "intime"
version = "0.1.0"

[dependencies]
clap = { version = "4.4", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }

I tried various adjustments on shell.nix but not sure where to look at.

shell.nix is also very short

$ cat shell.nix
{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [
    pkgs.rustc
    pkgs.cargo
    pkgs.pkg-config
  ];
}

Have you used root to run cargo? Or maybe a container or something? Either way, deleting the target directory (and perhaps ~/.cargo or $CARGO_HOME if you changed it) should help.

Also, use packages instead of buildInputs, your current shell.nix will not run the pkg-config hooks, so if you use libraries they will not be found.

I never ran cargo as root :scream: but good idea indeed. I used no container.

I tried to use nix run following this . It could be acceptable even if i wanted to started with “barebone”. At least it seems to work, i still have cargo build error but it could be a small dependency missing or maybe the cargo edition.

   > error: cannot find attribute `command` in this scope
   >   --> src/main.rs:13:3
   >    |
   > 13 | #[command(author, version, about, long_about = None)]
   >    |   ^^^^^^^
   >
   > error: cannot find attribute `arg` in this scope
   >   --> src/main.rs:16:7
   >    |
   > 16 |     #[arg(short, long, value_name = "REPERTOIRE")]
   >    |       ^^^
   >
   > error[E0599]: no function or associated item named `parse` found for struct `Args` in the current scope
   >   --> src/main.rs:76:22
   >    |
   > 14 | struct Args {
   >    | ----------- function or associated item `parse` not found for this struct
   > ...
   > 76 |     let args = Args::parse();
   >    |                      ^^^^^ function or associated item not found in `Args`
   >

So at this point I probably have now to deal with a real rust question…Thanks your help appreciated :pray: i generally try to find solution but it is like there is nixos on one side and every other OS in the other side

I don’t use nix run and do a lot of rust dev on NixOS. There must be something wrong with your local cargo/rust state, not NixOS.

I wouldn’t recommend building with nix for development, since it won’t do incremental builds, so it’ll be slow - especially for rust.