# Rust & OpenSSL woes

**URL:** https://discourse.nixos.org/t/rust-openssl-woes/12340
**Category:** Help
**Created:** [April 6, 2021, 11:29pm UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340 "2021-04-06T23:29:31Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![srid](https://discourse.nixos.org/user_avatar/discourse.nixos.org/srid/32/1051_2.png) [@srid](https://discourse.nixos.org/u/srid)
#### Post date: [April 6, 2021, 11:29pm UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/1 "2021-04-06T23:29:31Z")

</div>

I read [this thread](https://discourse.nixos.org/t/openssl-dependency-for-rust/3186), but there is no solution for my problem there.

It looks like Rust support in Nix is rather suboptimal compared to that of Haskell. I’m attempting to build [GitHub - soywod/himalaya: CLI to manage emails](https://github.com/soywod/himalaya) with the following `default.nix`

```nix
let 
  pkgs = import <nixpkgs> {};
in 
pkgs.rustPlatform.buildRustPackage rec {
  pname = "himalaya";
  version = "0.0.1";

  src = ./.;

  buildInputs = with pkgs; [
    pkg-config 
    openssl
    openssl.dev
  ];

  cargoSha256 = "sha256-i8YKE5Z2xoNyjFktRRVA45zD6Qa0aPNC0AOjHUIglgk=";

  meta = with pkgs.stdenv.lib; {
    description = "...";
    homepage = "https://github.com/...";
  };
}

```

This throws,

```auto
run pkg_config fail: "Failed to run `\"pkg-config\" \"--libs\" \"--cflags\" \"openssl\"`: No such file or directory (os error 2)"

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

```

Why? I don’t understand. OpenSSL is specified as a build-time dependency.

* * *

After I added `OPENSSL_DIR = pkgs.openssl.dev;`,

```auto
thread 'main' panicked at 'OpenSSL libdir at `/nix/store/nwyjl66jl3hyyp45rphgbknjm1pqq29j-openssl-1.1.1k-dev/lib` does not contain the required files to either statically or dynamically link OpenSSL', /build/himalaya-0.0.1-vendor.tar.gz/openssl-sys/build/main.rs:326:13

```

`/nix/store/nwyjl66jl3hyyp45rphgbknjm1pqq29j-openssl-1.1.1k-dev/lib/pkgconfig/` contains:

```auto
libcrypto.pc libssl.pc openssl.pc

```

---

<div class="post-metadata">

### Author: ![colemickens](https://discourse.nixos.org/user_avatar/discourse.nixos.org/colemickens/32/624_2.png) [@colemickens](https://discourse.nixos.org/u/colemickens)
#### Post date: [April 6, 2021, 11:39pm UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/2 "2021-04-06T23:39:08Z")

</div>

Try putting `pkg-config` in `nativeBuildInputs` (which is the more appropriate place for it anyway).

---

<div class="post-metadata">

### Author: ![srid](https://discourse.nixos.org/user_avatar/discourse.nixos.org/srid/32/1051_2.png) [@srid](https://discourse.nixos.org/u/srid)
#### Post date: [April 6, 2021, 11:52pm UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/3 "2021-04-06T23:52:52Z")

</div>

For the record, adding the following worked:

```nix
  PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";

```

If this is supposed to be configured automatically, then something in nixos-unstable is broken.

---

<div class="post-metadata">

### Author: ![ChocolateLoverRaj](https://discourse.nixos.org/user_avatar/discourse.nixos.org/chocolateloverraj/32/16594_2.png) [@ChocolateLoverRaj](https://discourse.nixos.org/u/ChocolateLoverRaj)
#### Post date: [August 8, 2024, 1:26am UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/4 "2024-08-08T01:26:26Z")

</div>

Here is a full working example that worked for me:

```nix
{ pkgs, ... }:

let
  auto-sound-system = with pkgs; rustPlatform.buildRustPackage rec {
    pname = "auto-sound-system";
    version = "1.0.0";
    src = /home/rajas/Documents/rust-esp32c3-examples/smart-power-button;
    buildAndTestSubdir = "computer";
    cargoLock = {
      lockFile = ./Cargo.lock;
    };
    nativeBuildInputs = [
      pkg-config
      openssl
      openssl.dev
    ];
    PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
  };
in
{
  systemd.services.auto-sound-system = {
    enable = true;
    description = "Automatically turn on/off the sound system";
    serviceConfig = {
      Type = "exec";
      ExecStart = "${auto-sound-system}/bin/smart-power-button-computer";
    };
    wantedBy = ["multi-user.target"];
  };
}

```

---

<div class="post-metadata">

### Author: ![TLATER](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tlater/32/4257_2.png) [@TLATER](https://discourse.nixos.org/u/TLATER)
#### Post date: [August 8, 2024, 2:03am UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/5 "2024-08-08T02:03:27Z")

</div>

Hrm, I’d caution against using that as a template, the input handling is a bit messy. It’ll work for local builds, but not for the right reasons.

> [@ChocolateLoverRaj](#):
>
> ```auto
> nativeBuildInputs = [
> pkg-config
> openssl
> openssl.dev
> ];
> 
> ```

`nativeBuildInputs` is for stuff that should run on the build machine, `buildInputs` is for libraries & stuff that should be linked against. You also should not explicitly need `openssl.dev` in `buildInputs`, since the `dev` output should automatically be used.

`pkg-config` comes with a [setup hook](https://github.com/NixOS/nixpkgs/blob/0039426728da074af7bf4851886758f9f3ee98e7/pkgs/build-support/pkg-config-wrapper/setup-hook.sh). This only executes if it’s in `nativeBuildInputs`. This setup hook will look for `buildInputs` which have a `pkgconfig` directory and add it to the `PKG_CONFIG_PATH`.

So, if we put packages in the correct input locations to begin with like so:

```auto
auto-sound-system = rustPlatform.buildRustPackage {
  pname = "auto-sound-system";
  version = "1.0.0";
  src = /home/rajas/Documents/rust-esp32c3-examples/smart-power-button;
  buildAndTestSubdir = "computer";
  cargoLock.lockFile = ./Cargo.lock;

  nativeBuildInputs = with pkgs; [
    pkg-config
  ];
  buildInputs = with pkgs; [
    openssl
  ];
};

```

The build will work without having to hack around with `PKG_CONFIG_PATH`, and cross-compilation won’t break.

---

<div class="post-metadata">

### Author: ![ChocolateLoverRaj](https://discourse.nixos.org/user_avatar/discourse.nixos.org/chocolateloverraj/32/16594_2.png) [@ChocolateLoverRaj](https://discourse.nixos.org/u/ChocolateLoverRaj)
#### Post date: [August 8, 2024, 5:10am UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/6 "2024-08-08T05:10:32Z")

</div>

I’ll try that. I thought that I had to use `PKG_CONFIG_PATH` because of [Rust & OpenSSL woes - #3 by srid](https://discourse.nixos.org/t/rust-openssl-woes/12340/3).

---

<div class="post-metadata">

### Author: ![TLATER](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tlater/32/4257_2.png) [@TLATER](https://discourse.nixos.org/u/TLATER)
#### Post date: [August 8, 2024, 11:47am UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/7 "2024-08-08T11:47:18Z")

</div>

Yeah, I haven’t actively tested that this works, of course. 3 years may have changed things, or there may be a bug.

---

<div class="post-metadata">

### Author: ![TLATER](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tlater/32/4257_2.png) [@TLATER](https://discourse.nixos.org/u/TLATER)
#### Post date: [February 18, 2025, 6:32pm UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/8 "2025-02-18T18:32:12Z")

</div>

For future readers: You may want to try [crate2nix](https://nix-community.github.io/crate2nix/), which exposes the nixpkgs built-in [`buildRustCrate`](https://nixos.org/manual/nixpkgs/stable/#compiling-rust-crates-using-nix-instead-of-cargo) builder instead - they have special features to set up your build env correctly for the dependency crates you use most of the time, that way you don’t need to worry about adding openssl yourself at all.

---

<div class="post-metadata">

### Author: ![harryprayiv](https://discourse.nixos.org/user_avatar/discourse.nixos.org/harryprayiv/32/8171_2.png) [@harryprayiv](https://discourse.nixos.org/u/harryprayiv)
#### Post date: [February 18, 2025, 6:51pm UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/9 "2025-02-18T18:51:03Z")

</div>

I ran into this exact issue when I tried to help (and fork for my own idea) the Lemmy project by adding a nix flakey alternative to spinning up their complicated, Docker-dependent dev environment. I think I’ve _almost_ recovered enough from the burnout of failing to do it the first time to take another stab!

> **[GitHub - Lemventory/lemmy: 🐀 A link aggregator and forum for the fediverse](https://github.com/Lemventory/lemmy)**
>
> 🐀 A link aggregator and forum for the fediverse

---

<div class="post-metadata">

### Author: ![TLATER](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tlater/32/4257_2.png) [@TLATER](https://discourse.nixos.org/u/TLATER)
#### Post date: [February 18, 2025, 6:52pm UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/10 "2025-02-18T18:52:33Z")

</div>

> [@harryprayiv](#):
>
> Docker-dependent dev environment

This may be a tough nut, nix builds are sandboxed and so don’t play well with host dependencies (like running docker services), share if you ever figure it out 🙂

---

<div class="post-metadata">

### Author: ![harryprayiv](https://discourse.nixos.org/user_avatar/discourse.nixos.org/harryprayiv/32/8171_2.png) [@harryprayiv](https://discourse.nixos.org/u/harryprayiv)
#### Post date: [February 18, 2025, 6:53pm UTC](https://discourse.nixos.org/t/rust-openssl-woes/12340/11 "2025-02-18T18:53:25Z")

</div>

Edited my comment to be clearer. My mission was to entirely bypass their docker dependency and build it all natively with nix.

Some said that I was reinventing the wheel since nixpkgs already had packaged Lemmy. But, this is in the context of dev environment not running an instance.
