【问题标题】:In nixpkgs, how do I override files of a package without recompilation?在 nixpkgs 中,如何在不重新编译的情况下覆盖包的文件?
【发布时间】:2021-07-26 00:20:24
【问题描述】:

对于需要很长时间构建的nixpkgs 包,我怎样才能在不导致完全重新构建的情况下进行一些简单的文件更改?

示例用法:

  • 添加/删除一些文件
  • 更改一些 shell 脚本
  • 添加缺少的pkgconfig .pc 文件

【问题讨论】:

    标签: nix


    【解决方案1】:

    创建一个复制或符号链接原始包的所有文件的新派生(例如使用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
    

    【讨论】:

      猜你喜欢
      • 2013-01-30
      • 2020-08-11
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多