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

Nix Flake构建YASMIN C++库时遇CMake路径错误

解决Nix Flake构建YASMIN库时的CMake找不到CMakeLists.txt错误

问题背景

使用Nix Flake开发C++项目,引入YASMIN状态机库,构建时遇到CMake错误,提示找不到CMakeLists.txt。

原flake配置:

{
  description = "flake for an autonomous rover";

  inputs = {

    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";

    flake-utils.url = "github:numtide/flake-utils";

  };

  outputs = { self, nixpkgs , flake-utils }:
    flake-utils.lib.eachDefaultSystem (system: 
    let pkgs = nixpkgs.legacyPackages.${system};
    in rec {

      packages.yasmin = with pkgs;
        stdenv.mkDerivation {
          name = "yasmin";
          src = fetchFromGitHub {
            owner = "uleroboticsgroup";
            repo = "yasmin";
            rev = "073e5e33cf2f29d42a1d7dcb3fe737390cfcb3a2"; # v3.3.0
            sha256 = "sha256-fcTP8Xa2W8K4TBmmwMk3zgU9ISH/MP87GxLl4Xnt0Xc=";
          };
          buildInputs = [ cmake ];
          buidlPhase = ''
            mkdir build && cd build 
            cmake ../yasmin/ -DCMAKE_INSTALL_LIBDIR=lib -DCMAKE_INSTALL_PREFIX=$out
          '';
          installPhase = ''
            make install
          '';
        };

      devShells.default =
        pkgs.mkShell { packages = with pkgs; [ cmake packages.yasmin ]; };

    });
}

错误日志:

warning: Git tree '/home/yasharth/workspace/mario' is dirty
error: builder for '/nix/store/72jwsp5r9xn6hv6zf1gbwln4vqjajfwx-yasmin.drv' failed with exit code 1;
       last 16 log lines:
       > Running phase: unpackPhase
       > unpacking source archive /nix/store/yq05yxn9dlvybinmaiyp29rxdffyljrk-source
       > source root is source
       > Running phase: patchPhase
       > Running phase: updateAutotoolsGnuConfigScriptsPhase
       > Running phase: configurePhase
       > fixing cmake files...
       > cmake flags: -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF -DCMAKE_INSTALL_LOCALEDIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/share/locale -DCMAKE_INSTALL_LIBEXECDIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/libexec -DCMAKE_INSTALL_LIBDIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/lib -DCMAKE_INSTALL_DOCDIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/share/doc/yasmin -DCMAKE_INSTALL_INFODIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/share/info -DCMAKE_INSTALL_MANDIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/share/man -DCMAKE_INSTALL_INCLUDEDIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/include -DCMAKE_INSTALL_SBINDIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/sbin -DCMAKE_INSTALL_BINDIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/bin -DCMAKE_INSTALL_NAME_DIR=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin/lib -DCMAKE_POLICY_DEFAULT_CMP0025=NEW -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_STRIP=/nix/store/pbqah1qk4b5y14fqinr1h8zvhqy71v81-gcc-wrapper-14.3.0/bin/strip -DCMAKE_RANLIB=/nix/store/pbqah1qk4b5y14fqinr1h8zvhqy71v81-gcc-wrapper-14.3.0/bin/ranlib -DCMAKE_AR=/nix/store/pbqah1qk4b5y14fqinr1h8zvhqy71v81-gcc-wrapper-14.3.0/bin/ar -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=/nix/store/30drcy63ldq4k60mmlgklbzpld4gfmhg-yasmin
       > CMake Warning:
       >   Ignoring extra path from command line:
       >
       >    ".."
       >
       > 
       > CMake Error: The source directory "/build/source" does not appear to contain CMakeLists.txt.
       > Specify --help for usage, or press the help button on the CMake GUI.

错误原因分析

  1. 拼写错误:配置中buidlPhase是拼写错误,正确应为buildPhase,导致自定义构建步骤未执行,默认的configurePhase被触发。
  2. 默认阶段未跳过:stdenv.mkDerivation默认会执行configurePhase,如果自定义构建流程,需要显式禁用该阶段。
  3. 源码路径错误:YASMIN仓库解压后根目录直接包含CMakeLists.txt,不需要指定../yasmin/,该路径不存在导致CMake找不到文件。

修复方案

  • 修正buidlPhase为buildPhase
  • 添加configurePhase = "";跳过默认配置阶段
  • 调整CMake命令的源码路径为../(因为build目录创建在源码根目录下,上级目录就是源码根)
  • 规范依赖位置:将cmake移至nativeBuildInputs(构建依赖放在此处更符合Nix惯例)

修正后的完整配置

{
  description = "flake for an autonomous rover";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs , flake-utils }:
    flake-utils.lib.eachDefaultSystem (system: 
    let pkgs = nixpkgs.legacyPackages.${system};
    in rec {

      packages.yasmin = with pkgs;
        stdenv.mkDerivation {
          name = "yasmin";
          src = fetchFromGitHub {
            owner = "uleroboticsgroup";
            repo = "yasmin";
            rev = "073e5e33cf2f29d42a1d7dcb3fe737390cfcb3a2"; # v3.3.0
            sha256 = "sha256-fcTP8Xa2W8K4TBmmwMk3zgU9ISH/MP87GxLl4Xnt0Xc=";
          };
          nativeBuildInputs = [ cmake ];
          configurePhase = "";
          buildPhase = ''
            mkdir build && cd build 
            cmake ../ -DCMAKE_INSTALL_LIBDIR=lib -DCMAKE_INSTALL_PREFIX=$out
          '';
          installPhase = ''
            make install
          '';
        };

      devShells.default =
        pkgs.mkShell { packages = with pkgs; [ cmake packages.yasmin ]; };

    });
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 18:05:55