OpenSSL dependency for Rust

I am trying to develop Rust applications on my NixOS machine. Poorly, when Emacs tries to check my project with Flycheck I get this error :

Error log
--- stdout
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR
cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR
cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR
cargo:rerun-if-env-changed=OPENSSL_DIR
run pkg_config fail: "`\"pkg-config\" \"--libs\" \"--cflags\" \"openssl\"` did not exit successfully: exit code: 1\n--- stderr\nPackage openssl was not found in the pkg-config search path.\nPerhaps you should add the directory containing `
openssl.pc\'\nto the PKG_CONFIG_PATH environment variable\nNo package \'openssl\' found\n"                                                                                                                                                    

--- stderr
thread 'main' panicked at '

Could not find directory of OpenSSL installation, and this `-sys` crate cannot
proceed without this knowledge. If OpenSSL is installed and this crate had
trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
compilation process.

Make sure you also have the development packages of openssl installed.
For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

If you're in a situation where you think the directory *should* be found
automatically, please open a bug at https://github.com/sfackler/rust-openssl
and include information about your system as well as this message.

    $HOST = x86_64-unknown-linux-gnu
    $TARGET = x86_64-unknown-linux-gnu
    openssl-sys = 0.9.36

/openssl-sys-0.9.36/build/main.rs:265:9

And that even after installing the package globally openssl via nix-env. I also tried to set the environment variable to the relevant part of the nix store.
All of above without success.

Is there a way to solve the issue without creating a default.nix in each of my Rust project (read “a system-wide solution”) ?

Previous occurences

I did some search before asking about that issue both in the Rust and NixOS community and this is what I could gather :

  • this post seems to be closely related to my issue but I can not understand any answers in any of the pointed resources in the post. Are those solutions correct for a system-wide access to the library (openssl-dev) without having to open my editor through a nix-shell ?
  • this config file seems to solve (?) my issue but
    1. I have no idea if it is actually the case.
    2. I do not know how to implement it (e.g. where to put it, how to “call” it).
  • this part of the wiki seems to be related to my problem but deals with it in, what I can gather to be, a granular solution rather than a global one. Am I correct ? If yes, is it possible to generalize that solution for not a per-project basis but for the system itself ?

I understand from my last bullet point that I seem to be going “against the grain” of the NixOS philosophy but it seems cumbersome to not have a system-wide configuration (at least a default base) for when a program (here my text editor) is compiling a program outside of a nix-shell. Is it the case ? Did I miss a obvious part in the Nix manual ?

Sidenote : I found a bit sad that there is no relevant section in the wiki for that. So when I find a solution I will edit it.

If all Rust projects are in some shared directory, e.g. ~/git/rust, you could place shell.nix in that directory. Though I personally just use a per-project shell.nix. I have a small nixify shell function that creates shell.nix.

You don’t have to. You can configure direnv to automatically use shell.nix if you cd into a directory:

In Emacs, I use the direnv package to automatically obtain the environment for the current file:

After some tests this week, it seems to be mostly working (maybe not to work on the compiler itself but it is a special snowflake) so I marked your answer as a solution (a sad one but a working one, thank you for that).

As stated in my original post, I then integrated my working solution in the relevant part of the wiki.

Thanks for documenting things in the wiki. You shouldn’t have to set OPENSSL_DIR/OPENSSL_LIB_DIR. The openssl-sys build script uses pkgconfig when it is available. So, it should be sufficient to add pkgconfig to your buildInputs,

Thank you for your contribution, will update the wiki in that regard. My Nix-fu is definitely not good. If you have suggestions, feel free to change the wiki !