[SOLVED] How do I build cargo on NixOS?

After a recent upgrade of my NixOS installation I somehow lost a valuable ability to build cargo from source (which I achieved previously in some terribly hacky way).

How should I use nix-shell to properly add openssl and zlib to my environment?

Currently I have in default.nix the following

with import <nixpkgs> {}; {
  cargoEnv = stdenv.mkDerivation {
    name = "cargo";
    buildInputs = [ stdenv openssl zlib ];

    ZLIB_INCLUDE_DIR="${zlib.dev}/include";
    ZLIB_LIBRARY="${zlib.out}/lib/";

    OPENSSL_INCLUDE_DIR="${openssl.dev}/include/openssl";
    OPENSSL_LIBRARIES="${openssl.out}/lib";
    OPENSSL_ROOT_DIR="${openssl.out}";
  };
}

And when I do

$ nix-shell . --command zsh     
$ cargo build

I get

  Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
1 Like

here is what works for me

default.nix

let
  pkgs = import <nixpkgs> {};
in
  pkgs.rustStable.cargo
  #pkgs.rustBeta.cargo
  #pkgs.rustUnstable.cargo

And then run

% nix-shell --run zsh
(nix-shell) % export SSL_CERT_FILE='/etc/ssl/certs/ca-bundle.crt'   # for some reason SSL_CERT_FILE is not present in nix-shell
(nix-shell) % cargo build

Above default.nix takes expression from nixpkgs (https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/compilers/rust/cargo.nix). If some change is needed in nixpkgs expression of rust you can use overrideDerivation to change it.

does it help?

1 Like

Yep! Thanks a lot!

I've recently accidently the whole cargo project directory, and this page showed up in google, so here are instruction for developing Cargo with Nixos and nix-shell for future me:

$ git clone https://github.com/rust-lang/cargo.git && cd cargo
$ cat > default.nix <<EOF
with import <nixpkgs> {}; {
  cargoEnv = stdenv.mkDerivation {
    name = "cargo";
    buildInputs = [ pkgconfig openssl cmake zlib libgit2 ];
    shellHook = ''
      export CFG_DISABLE_CROSS_TESTS=1
      jumpapp idea-community .
    '';
  };
}
EOF
$ echo "default.nix" >> .git/info/exclude
$ nix-shell --run zsh
$ cargo test

EDIT:

my current version is

# in file ~/projects/cargo/shell.nix
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
pkgs.mkShell {
  buildInputs = [ pkgconfig openssl cmake zlib libgit2 ];
  shellHook = ''export CFG_DISABLE_CROSS_TESTS=1'';
}

Hey @matklad, thanks for this!
I'm also using Intellij (btw thanks for the plugin!) and was wondering what was 'jumpapp' doing?

1 Like

jumpapp is a small utility which either brings focus to existing application window or launches the application. Basically, it's poor-mans version of awesome windows Win+number shortcuts, which, for some reason, don't have equivalents in any popular linux desktop environment.

1 Like

Hey, thank you, @matklad from the past! I have a suggestion for improving your workflow: you can use shell.nix instead of default.nix, mkShell instead of mkDerivation and direnv to automatically activate the env.

This is my current setup

11:22:52|~/projects/cargo|master⚡*?
λ cat shell.nix 
with import <nixpkgs> {};
mkShell {
  buildInputs = [ pkgconfig openssl cmake zlib libgit2 ];
  CFG_DISABLE_CROSS_TESTS = "1";
}

11:22:54|~/projects/cargo|master⚡*?
λ cat .envrc 
use nix
1 Like