You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用nix-shell构建SHADERed时pkg-config配置异常问题排查

Nix-shell构建SHADERed时CMake找不到PkgConfig的排查方案

问题场景

我尝试用nix-shell搭建构建SHADERed的环境,shell.nix配置如下:

{ pkgs ? import <nixpkgs> { } }:

pkgs.mkShell {
  nativeBuildInputs = with pkgs; [
    cmake
    pkg-config
  ];
  buildInputs = with pkgs; [
    SDL2
    glew
    glm
    python3
    sfml
  ];
  shellHook = ''
  export PKG_CONFIG_EXECUTABLE="${pkgs.pkg-config}/bin/pkg-config"
  '';
}

运行cmake .时出现报错:

Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)

但执行echo $PKG_CONFIG_EXECUTABLE能得到正确路径:/nix/store/a3752l19hc76dmzh1wgrxbsxh6lfx67z-pkg-config-wrapper-0.29.2/bin/pkg-config。明明将pkg-config加入nativeBuildInputs应该自动配置环境变量,但添加或移除shellHook均无效果。

排查与解决方法

  • 清理CMake缓存文件:如果之前运行过cmake,会生成CMakeCache.txt文件,其中可能残留旧的配置信息。直接删除该文件,再重新运行cmake .。
  • 显式指定CMake变量:运行cmake时直接传入PKG_CONFIG_EXECUTABLE参数,跳过环境变量自动识别的问题:
    cmake -DPKG_CONFIG_EXECUTABLE="$PKG_CONFIG_EXECUTABLE" .
    
  • 修正shellHook的变量导出方式:将shellHook里的硬编码路径换成动态获取,避免路径不一致:
    shellHook = ''
      export PKG_CONFIG_EXECUTABLE=$(which pkg-config)
    '';
    
  • 使用Nix适配的CMake版本:确保nativeBuildInputs中的cmake是Nix官方包装的版本,部分情况下直接使用pkgs.cmakePackages.cmake能更好地兼容Nix环境的依赖查找:
    nativeBuildInputs = with pkgs; [
      cmakePackages.cmake
      pkg-config
    ];
    

内容的提问来源于stack exchange,提问作者Atrus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 12:14:55