Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust doesn't compile openssl library on MacOSX #2167

Open
ineiti opened this issue Jun 21, 2024 · 13 comments
Open

Rust doesn't compile openssl library on MacOSX #2167

ineiti opened this issue Jun 21, 2024 · 13 comments
Labels
bug Something isn't working triage Issue needs triage

Comments

@ineiti
Copy link

ineiti commented Jun 21, 2024

What happened?

I have a rust project which uses openssl. Now I would like to have a reproducible build using devbox. But when I use rustup from devbox, the compilation fails at linking time.

I tried the same repo with flox and a locally installed rust, which compiles and runs fine!

Steps to reproduce

Clone the repo, start the shell, and compile parts of it:

git clone https://github.com/ineiti/fledger
cd fledger
devbox shell
cd cli/fledger
cargo build

The final output of the linker is this:

  = note: ld: framework not found Security
          clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

Command

shell

devbox.json

{
  "$schema": "https://github.com/raw/jetify-com/devbox/0.10.7/.schema/devbox.schema.json",
  "packages": [
    "which@latest",
    "rustup@latest",
    "libiconv@latest",
    "openssl@latest",
    "vscode",
  ],
  "shell": {
    "init_hook": [
      "if [ ! -d $RUSTUP_HOME/toolchains -o ! -d $RUSTUP_HOME/toolchains/stable ]; then rustup default stable; fi",
    ],
    "scripts": {
      "test": [
        "make cargo_test",
      ],
    },
  },
}

Devbox version

0.11.1

Nix version

nix (Nix) 2.23.0

What system does this bug occur on?

macOS (Apple Silicon)

Debug logs

No response

@ineiti ineiti added bug Something isn't working triage Issue needs triage labels Jun 21, 2024
@savil
Copy link
Collaborator

savil commented Jun 21, 2024

@ineiti I'd suggest running devbox add "darwin.apple_sdk.frameworks.Security" --platform=x86_64-darwin,aarch64-darwin.

With that, I am able to run cargo build on my x86_64-darwin machine.

Let us know if that does, or does not, work for you?

@ineiti
Copy link
Author

ineiti commented Jun 22, 2024

@savil Thanks a lot, that solves the problem and it compiles fine now! I did not know about this package, and I did spend 30 minutes on Google with the error message before opening this issue. How could I have discovered this?

Some questions:

  • Why does flox manage to compile this by default? Is it because it doesn't include clang? I checked which ar, and flox gives the /usr/bin/ar, while devbox gives it's own ar in a clang subdirectory.
  • Should devbox have some of these packages as default?
  • Do you want me to close the issue? Or is it worth investigating this further?

@savil
Copy link
Collaborator

savil commented Jun 24, 2024

Those are great questions. I think Devbox should do better here, and in particular since this is a bit of an FAQ for Rust with Macs, we should make it Just Work ™️ . Lets leave this task open for a bit. I'll look into it more this week.

There are some considerations in my mind:

  • is it a good idea to use the nix packaged Security Framework instead of relying on the library in your MacOS? I'd like to look into how frequently and quickly that package is updated. I am wondering if there is a reproducibility versus security tradeoff here.
  • can we modify Devbox's Rust plugin to guide users? Hopefully, this can have users avoid the experience you had.

To your questions:

  • Not super familiar with the internals of flox (they are a GPL project, so I'll have to tread carefully there), but it sounds like they are relying on the native MacOS environment. This can hurt reproducibility since a colleague of yours on Linux may get a different set of tools, but then again, at least it worked for you so that's better than your Devbox experience.
  • Yes IMO, see my second consideration above.
  • Not yet :)

@guerinoni
Copy link

With a simple rust project I'm not able to run cargo build

{
  "$schema": "https://github.com/raw/jetify-com/devbox/0.13.4/.schema/devbox.schema.json",
  "packages": {
    "cargo":    "latest",
    "libiconv": "latest",
    "darwin.apple_sdk.frameworks.Security": {
      "platforms": ["x86_64-darwin", "aarch64-darwin"]
    }
  },
  "shell": {
    "init_hook": ["echo 'Welcome to devbox!' > /dev/null"],
    "scripts": {
      "test": ["echo \"Error: no test specified\" && exit 1"]
    }
  }
}

(fresh new mac, very nice test :) )

@ineiti
Copy link
Author

ineiti commented Oct 17, 2024

With a simple rust project I'm not able to run cargo build

What is the error message when it fails?

Also, did you actually install cargo? Because with your devbox.json you only install rustup - look at my init_hook from above.

@guerinoni
Copy link

{
  "$schema": "https://github.com/raw/jetify-com/devbox/0.13.4/.schema/devbox.schema.json",
  "packages": {
    "cargo":    "latest",
    "libiconv": "latest",
    "darwin.apple_sdk.frameworks.Security": {
      "platforms": ["x86_64-darwin", "aarch64-darwin"],
    },
    "rustup": "latest",
  },
  "shell": {
    "init_hook": [
      "if [ ! -d $RUSTUP_HOME/toolchains -o ! -d $RUSTUP_HOME/toolchains/stable ]; then rustup default stable; fi",
    ],
    "scripts": {
      "test": [
        "echo \"Error: no test specified\" && exit 1",
      ],
    },
  },
}

Error is

  = note: ld: framework not found SystemConfiguration
          clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

@guerinoni
Copy link

Adding also
devbox add "darwin.apple_sdk.frameworks.SystemConfiguration" --platform=x86_64-darwin,aarch64-darwin
doesn't help

@guerinoni
Copy link

Ok now it is working, for some reasons I removed .devboxfolder and re-did everything from scratch and it is working :)

@ineiti
Copy link
Author

ineiti commented Oct 18, 2024

@savil - Is there a place this information should be added? The rustup default stable might be nice to echo when devbox add rustup is called. But the darwin.apple_sdk.frameworks information is really crucial! Do you think creating a PR against

https://github.com/NixOS/nixpkgs/blob/c858402c2a629211153137fb8d39be9fde4694ff/pkgs/development/tools/rust/rustup/default.nix#L69

is a good idea and will be included?

@savil
Copy link
Collaborator

savil commented Oct 18, 2024

@ineiti

A few thoughts:

  1. A small update we could do is print a Notice in the Devbox rustup Plugin that one may encounter errors on MacOS pertaining to missing frameworks, such as note: ld: framework not found SystemConfiguration and one solution is to add the package as devbox add "darwin.apple_sdk.frameworks.SystemConfiguration" --platform=x86_64-darwin,aarch64-darwin.
  2. We should also add this note to https://www.jetify.com/docs/devbox/devbox_examples/languages/rust/ and our FAQ.
  3. Circling back to a discussion point from earlier (above). Apple SDKs on nixpkgs should soon be up-to-date much more frequently. Previously, they had tended to lag behind. See https://discourse.nixos.org/t/on-the-future-of-darwin-sdks-or-how-you-can-stop-worrying-and-put-the-sdk-in-build-inputs/50574.

re: adding the apple frameworks by default to the nixpkgs definition of rustup in MacOS
I'd suggest asking in the NixOS Discourse (https://discourse.nixos.org) if that would be welcome. I can imagine some arguments for and against it, but it seems like a good discussion to have. Maybe @reckenrode from the above NixOS Discourse post may have some thoughts.

@reckenrode
Copy link

reckenrode commented Oct 19, 2024

After this staging-next cycle, the stdenv in master (and when 24.11 releases) will include a full SDK. It should have all the frameworks needed to build macOS applications and libraries (like OpenSSL). It also propagates libiconv, which should remove the need to conditionally add it for Rust applications.

I have some documentation I need to write for the release, but the gist of using a new SDK is you add it as a build input. The SDK uses setup hooks to set the latest one from your inputs plus propagated inputs, so there is no way to accidentally mix them.

Edit: for reference, the following SDKs will be available in 24.11: 10.12.2 (default on x86_64-darwin), 10.13.2, 10.14.6, 10.15.6, 11.3 (default on aarch64-darwin), 12.3, 13.3, 14.4, 15.0.

The default and minimum version for x86_64-darwin is being updated to match aarch64-darwin for 25.05. They will be updated more regularly to better match the platform’s supported versions. Additionally, Darwin will use clang-19 by default and will(probably) use darwin.ICU and darwin.libpcap by default for icu and libpcap respectively.

@reckenrode
Copy link

I also wanted to add that source release packages (like libiconv, ICU, and libpcap) have been updated to the source releases for macOS 15. They will be updated as new ones are made available.

@ineiti
Copy link
Author

ineiti commented Oct 21, 2024

@reckenrode thanks a lot for the update - so I guess I'll just wait for the 24.11 release and won't bother with adding the notes to the rustup nix package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Issue needs triage
Development

No branches or pull requests

4 participants