LnL7 / nix-darwin

nix modules for darwin
MIT License
2.82k stars 431 forks source link

`pkg-config` not functioning as expected on fresh install and shells #934

Closed yonkeltron closed 5 months ago

yonkeltron commented 5 months ago

Hello,

When trying to compile various libraries in a fresh nix-darwin install (using the flake approach), I get all manner of missing library errors. This seems to be related to an underlying issue with pkg-config as illustrated here, which also shows that the PKG_CONFIG_PATH environment variable is not set:

❯ nix shell 'nixpkgs#libsodium.dev' 'nixpkgs#libsodium' 'nixpkgs#pkg-config'
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish

❯ pkg-config --libs libsodium
Package libsodium was not found in the pkg-config search path.
Perhaps you should add the directory containing `libsodium.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libsodium' found

❯ pkg-config --list-all

❯ echo $PKG_CONFIG_PATH

❯ exit

I am on macOS Sonoma 14.4.1 with the following environment:

❯ nix --version
nix (Nix) 2.18.2

❯ uname -a
Darwin <HOSTNAME> 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 arm64

Please let me know if I can provide any additional information or help with testing.

Love, +Jonathan

Samasaur1 commented 5 months ago

Using nix shell by itself doesn't run shell hooks. You can either make a dev shell in a flake and use nix develop:

{
  description = "A very basic flake";

  outputs = { self, nixpkgs}:
    let
      allSystems = nixpkgs.lib.systems.flakeExposed;
      forAllSystems = nixpkgs.lib.genAttrs allSystems;
      define = f: forAllSystems (system:
        let
          pkgs = import nixpkgs {
            inherit system;
            config.allowUnfree = true;
          };
        in
          f pkgs
      );
    in
    {
      devShells = define (pkgs: {
        default = pkgs.mkShell {
            buildInputs = [
              pkgs.pkg-config
              pkgs.libsodium
            ];
          };
        }
      );
    };
}

or you can use nix-shell:

nix-shell -p libsodium pkg-config
yonkeltron commented 5 months ago

Sorry, so does this mean it shouldn't work in my normal, day-to-day shell either? Because that's what i'm struggling with a bit more.

Can confirm it works with nix-shell -p libsodium pkg-config but not in my regular shell.

Love, +Jonathan

Samasaur1 commented 5 months ago

Yes, installing these packages systemwide will also not run the necessary setup hooks. The "right" way to do per-project dependencies is with a dev shell

yonkeltron commented 5 months ago

The "right" way to do per-project dependencies is with a dev shell

Ok, this mostly makes sense. What about if I want to compile and install a binary for my user? Like even some tool with cargo?

NINJA EDIT: Also asking because I have tried to install things with nix-darwin which have actually needed compilation and I didn't realize it until the install failed.

Samasaur1 commented 5 months ago

What about if I want to compile and install a binary for my user?

Then you would write a Nix derivation for that package, which would include the dependencies, but you still wouldn't install the dependencies systemwide

yonkeltron commented 5 months ago

What about if I want to compile and install a binary for my user?

Then you would write a Nix derivation for that package, which would include the dependencies, but you still wouldn't install the dependencies systemwide

Ok because I have had issues with unavailable libraries trying to install the bupstash package, which is what first brought this issue to my attention. Did I do something wrong?

Samasaur1 commented 5 months ago

bupstash works fine for me on aarch64-darwin, but doesn't work on x86_64-darwin. Not sure what's wrong with it there, but if you're on x86_64-darwin, then you didn't do anything wrong

yonkeltron commented 5 months ago

Since I am on aarch, it seems something is actually wrong. How can I troubleshoot or provide more info? Seems like this could be a bug after all.

Samasaur1 commented 5 months ago

What exactly are you doing? I'm running

nix shell nixpkgs#bupstash

and it's working for me.

Can you let me know what exactly you're running, and provide the output of that command?

yonkeltron commented 5 months ago

That works, but not installing it as a package for my user more generally.

# ...snip...
          packages = with pkgs; [
# ...snip...
              bupstash
              clang
              cmake
              darwin.apple_sdk.frameworks.CoreServices
              darwin.apple_sdk.frameworks.Foundation
              darwin.Libnotify
# ...snip...
              libsodium
              libsodium.dev
              llvmPackages_latest.lld
              llvmPackages_latest.llvm
# ...snip...

This used to produce an error, but doesn't for whatever reason. It must no longer be trying to compile the package. Ok, then. I believe we are good.