如何解决在 nixpkgs 中,如何在不重新编译的情况下覆盖包的文件?
对于需要很长时间构建的 nixpkgs 包,如何在不引起完全重建的情况下进行一些简单的文件更改?
示例用法:
解决方法
创建一个新的派生,复制或符号链接原始包的所有文件(例如,使用 cp
),然后根据需要修改文件。
这是一个完整注释的示例:
# This snippet shows how to override a nix package by copying all of the
# files of an already-built package,and then modifying some of them,# thus avoiding a full rebuild.
#
# Example usage:
#
# Build the package:
# nix-build --no-out-link change-file-after-build-example.nix
# See our replacement worked:
# $ $(nix-build --no-out-link change-file-after-build-example.nix)/share/git/contrib/fast-import/git-import.sh
# USAGE: git-import branch import-message
{
pkgs ? import <nixpkgs> {},lib ? pkgs.lib,}:
let
originalPackage = pkgs.git;
# We use `overrideAttrs` instead of defining a new `mkDerivation` to keep
# the original package's `output`,`passthru`,and so on.
myOverriddenGitPackage = originalPackage.overrideAttrs (old: {
name = "git-overridden";
# Using `buildCommand` replaces the original packages build phases.
buildCommand = ''
set -euo pipefail
${
# Copy original files,for each split-output (`out`,`dev` etc.).
# E.g. `${package.dev}` to `$dev`,and so on. If none,just "out".
# Symlink all files from the original package to here (`cp -rs`),# to save disk space.
# We could alternatiively also copy (`cp -a --no-preserve=mode`).
lib.concatStringsSep "\n"
(map
(outputName:
''
echo "Copying output ${outputName}"
set -x
cp -rs --no-preserve=mode "${originalPackage.${outputName}}" "''$${outputName}"
set +x
''
)
(old.outputs or ["out"])
)
}
# Example change:
# Change `usage:` to `USAGE:` in a shell script.
# Make the file to be not a symlink by full copying using `install` first.
# This also makes it writable (files in the nix store have `chmod -w`).
install -v "${originalPackage}"/share/git/contrib/fast-import/git-import.sh "$out"/share/git/contrib/fast-import/git-import.sh
sed -i -e 's/usage:/USAGE:/g' "$out"/share/git/contrib/fast-import/git-import.sh
'';
});
in
myOverriddenGitPackage
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。