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

基于Nix Flake构建后将额外文件放入bin目录的实现方案

如何在Nix Flake构建的bin目录中添加额外文件

有两种常用方法可以实现这个需求,根据你的实际场景选择:

方法一:重写installPhase直接复制文件

直接修改buildPackage的installPhase,在默认安装步骤后把额外文件复制到$out/bin目录:

bin = craneLib.buildPackage (commonArgs // {
  inherit cargoArtifacts;
  installPhase = ''
    # 执行默认的安装流程(确保原二进制文件正常安装)
    runHook preInstall
    install -Dm755 target/release/exkecutabel $out/bin/exkecutabel
    # 复制额外文件到bin目录,替换成你实际的文件路径
    install -Dm644 ./path/to/extra-file1 $out/bin/extra-file1
    # 如果是可执行文件,把权限改成755
    install -Dm755 ./path/to/extra-script.sh $out/bin/extra-script.sh
    runHook postInstall
  '';
});

方法二:用symlinkJoin合并目录

如果额外文件来自其他Nix包或者需要批量添加,可以用symlinkJoin将原构建输出和包含额外文件的目录合并:

bin = pkgs.symlinkJoin {
  name = "exkecutabel-with-extras";
  paths = [
    # 原crane构建的二进制包
    (craneLib.buildPackage (commonArgs // { inherit cargoArtifacts; }))
    # 用writeDir创建一个包含额外文件的临时目录,或者直接引用已有包的输出
    (pkgs.writeDir "extra-bin-contents" {
      "extra-file1" = ./path/to/extra-file1;
      "extra-script.sh" = ./path/to/extra-script.sh;
    })
  ];
};

注意事项

  • 调整文件权限:可执行文件用m755,普通文件用m644,确保在系统上能正常使用
  • 如果额外文件是其他Nix包提供的,直接把包名加入paths列表即可,不用自己复制

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 01:05:09