cachix / devenv

Fast, Declarative, Reproducible, and Composable Developer Environments
https://devenv.sh
Apache License 2.0
3.56k stars 259 forks source link

Broken Clang on MacOS (nix-darwin) #951

Closed alecandido closed 3 weeks ago

alecandido commented 3 months ago

Describe the bug Trying to compile a simple hello.cpp program is failing, because of c++abi not found.

To reproduce I'm actually using a flake

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    systems.url = "github:nix-systems/default";
    devenv.url = "github:cachix/devenv";
  };

  outputs = {
    self,
    nixpkgs,
    devenv,
    systems,
    ...
  } @ inputs: let
    forEachSystem = nixpkgs.lib.genAttrs (import systems);
  in {
    devShells =
      forEachSystem
      (system: let
        pkgs = nixpkgs.legacyPackages.${system};
      in {
        # ----------------------------------------------------
        default = pkgs.mkShell {
          buildInputs = with pkgs; [clang];
        };
        # ----------------------------------------------------
        # default = devenv.lib.mkShell {
        #   inherit inputs pkgs;
        #
        #   modules = [
        #     {
        #       packages = with pkgs; [clang];
        #     }
        #   ];
        # };
        # ----------------------------------------------------
        # default = devenv.lib.mkShell {
        #   inherit inputs pkgs;
        #
        #   modules = [
        #     {
        #       languages.cplusplus.enable = true;
        #     }
        #   ];
        # };
        # ----------------------------------------------------
      });
  };

  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };
}

I defined three shells (uncommenting each one of the subdivided sections), and run

nix develop --impure
clang++ hello.cpp

with the customary hello.cpp:

#include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}

In the first case (no devenv) everything works smoothly (even without --impure, of course), and running ./a.out I obtain the usual Hello, World! output. In the other two cases, I'm unable to compile. The result is:

❯ nix develop --impure
(devenv) bash-5.2$ clang++ hello.cpp
ld: warning: directory not found for option '-L/nix/store/qfdj687ndydj5mfd60y5ip43d1jgqb6j-devenv-profile/lib'
ld: library not found for -lc++abi
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

(the hash may change, I tried with multiple versions of nixpkgs, including nixos-23.11 and `nixpkgs-23.11-darwin, on top of the two options in the flake)

I'm not sure what is actually happening...

Version

I attach the whole flake.lock.

flake.zip

I'm on MacOS, with ARM architecture (Apple Silicon).

❯ sw_vers
ProductName:            macOS
ProductVersion:         14.2.1
BuildVersion:           23C71
domenkozar commented 3 months ago

I think we have a clang option somewhere that you have to enable, but I might be wrong.

alecandido commented 3 months ago

The only references to Clang in the devenv.nix docs are: https://github.com/cachix/devenv/blob/ad0ae333b210e31237e1fc4a7ddab71a01785add/docs/reference/options.md?plain=1#L3844-L3850

EDIT: and those are actually the only references to Clang in the whole repo, other than the explicit dependency on pkgs.clang in the c and cplusplus language presets https://github.com/search?q=repo%3Acachix%2Fdevenv+clang&type=code

DaniruKun commented 3 months ago

My workaround was adding pkgs.libcxxabi

mrfelton commented 3 months ago

I've found that clang breaks in this way if I have multiple languages enabled.

For example if I have the following then I get problems with clang when using golang.

languages.ruby.enable = true;
languages.rust.enable = true;
languages.go.enable = true;

But with only go enabled as below, it works just fine.

languages.go.enable = true;
hurricanehrndz commented 2 months ago

I can confirm the issue.

hurricanehrndz commented 2 months ago

My workaround was adding pkgs.libcxxabi

This didn't work for me

domenkozar commented 2 months ago

Does it also happen on #745

domenkozar commented 1 month ago

Could someone try with 1.0.1 that was released yesterday?

alecandido commented 1 month ago

I am going to test it immediately

alecandido commented 1 month ago

All the examples above are now working, so I'd close the issue if there are no further comments

(to be completely honest: I didn't test with 1.0.1, but I left the flake locking the dependency on its own, and it happened to be fixed to rev 984707a775ad1540317ba455640e1748c27b163f, that is 3 commits ahead...)

domenkozar commented 3 weeks ago

Closing, please reopen if still broken.