Need Help building a Rust package

Hello everyone, noob nixos user here !

I have been enjoying NixOS up until now, it’s really different and I like the way the configurations are.

I tried to build the Neovim-gtk Rust crate, but the compiling crashes when trying to link libraries.

Here is the result of me executing the cargo build command:
log

I would like to know how to properly compile this crate or any other crate I might want to install (or work with) with cargo.

Thank you in advance !

Not sure that’s relevant to your problem since it’s old but neovim-gtk: init at 0.1.1 by teto · Pull Request #36109 · NixOS/nixpkgs · GitHub might help

Well, that answers a good part of my question!
I’m sure I’ll get it to build with this example, but it is not exactly what I’m looking for, let me explain with the following questions :

  • this method builds the crate as a nixpkg if I’m not mistaken, but what if I actually want to compile it as a crate?
  • although this will work for compiling this specific package, what about working on a project that has that kind of linking? Would something like nix-shell be the solution for that?

Hey, as I got problems posting via mail, here is my response sent via the web interface.

Hey Luxed,

as Cargo is only a language-level package manager, it will only
automatically install Rust crates for you. neovim-gtk has additional
system dependencies, as you can see in the lengthy apt install command
in their build instructions.

My preferred Nix way of suppliying these dependencies is via nix-shell.
I placed a shell.nix file with the following content in the directory:

with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "mydevshell";

  buildInputs = with pkgs; [
    gtkd
    atk
    cairo
    gdk_pixbuf
    glib
    gnome2.pango

    rustc
    cargo
    pkgconfig
 ];
}

The names of the Nix packages differ from those in the apt command. I
found them by grepping Nixpkgs and looking at the PR Matthieu Coudron
mentioned.

With having the shell.nix file in place, you can run nix-shell --pure in the repo to get a development shell in which only the
mentioned packages are installed. This has the advantage, that you don’t
clutter your system with packages you only need once.

I was able to build neovim-gtk inside this shell with being
the current master revision. This is important, as the rustc version in
nixos-18.03 is too old. If you don’t happen too be on something like
nixos-unstable, you may run nix-shell like this:

nix-shell --pure -I nixpkgs=/path/to/your/nixpkgs/master/checkout/

Hope this helps,
erictapen

1 Like

I have also been struggling with Rust.
I was trying to make a little package for “spotifyd” which seemed pretty simple.
However, rustPlatform.buildRustPackage has been giving me such a headache.

There seems to be some issues with the “replace” declarations of Cargo that leads to multiple versions in the vendor. I have seen a few similar issues pop up in the GitHub Issue tracker, but no solutions. :frowning:

There’s also a convenience function pkgs.mkShell to cut down the boilerplate required for writing shell.nix.

Here’s a shell.nix I use to develop Cargo:

with import <nixpkgs> {};
pkgs.mkShell {
  buildInputs = [
    pkgconfig openssl cmake zlib libgit2
  ];
}
2 Likes

Thanks for all the help guys !
But now the issue is that the program doesn’t launch. I do cargo build inside the environment, everything compiles just fine, but when I go ahead and do target/debug/nvim-gtk, nothing happens. No log, no nothing. It’s just like if I ran an empty program. I also tried with cargo build --release with no success.

Got the same problem but I have no idea, sorry.

When out of options, try to run strace -fF against the program. It takes a while to sift through the logs but it gives a lot of good clues about what the program is doing. Especially open, stat and execve syscalls, they will tell you if files or programs that were supposed to exist are missing.

2 Likes

I can build fine using the shell, the whole problem stems from “rustPlatform.buildRustPackage” not correctly replacing cargo packages from the .yaml
:frowning:

Well thank you for that !
I am going through the entire 5k lines right now, but it’s clear that a lot of things are still missing.
It’s weird and I don’t really understand.
From what I see, even though I have included things like glib, I still see No such file errors related to glib.

I want to use NixOS, but for now I cannot use it at all, I’m just lost in dependency hell.