ruby - 如何使用具有 native 扩展的 Nix 构建 Ruby Gem

标签 ruby nix

我正在尝试为 rails 构建一个 bundlerEnv。 Nix 不知何故知道 nokogiri(rails 的依赖项)需要 zliblibxml2libxslt 但编译失败。

#Gemfile
gem "rails"

#default.nix
{ pkgs ? import <nixpkgs> {} }:

with pkgs;

bundlerEnv {
  name = "smt";
  inherit ruby;

  gemfile  = ./Gemfile;
  lockfile = ./Gemfile.lock;
  gemset   = ./gemset.nix;
}

full output

$ nix-build
...
buildFlags: --use-system-libraries --with-zlib-dir=/nix/store/dn9l2kd6ai731k34pzlzr5dz3in0rn31-zlib-1.2.8-dev --with-xml2-lib=/nix/store/9w15hz68wz5qnihfhsbr281d0sfs20a8-libxml2-2.9.3/lib --with-xml2-include=/nix/store/rq94vzxa92p30prfgv9jz83xiy8hkn2k-libxml2-2.9.3-dev/include/libxml2 --with-xslt-lib=/nix/store/fv137j5wxp4dg1wjqdbk8i2l087163b7-libxslt-1.1.28/lib --with-xslt-include=/nix/store/j7y29aggnzmlgh5bccp9f7vmiknh16fm-libxslt-1.1.28-dev/include --with-exslt-lib=/nix/store/fv137j5wxp4dg1wjqdbk8i2l087163b7-libxslt-1.1.28/lib --with-exslt-include=/nix/store/j7y29aggnzmlgh5bccp9f7vmiknh16fm-libxslt-1.1.28-dev/include
WARNING:  You build with buildroot.
  Build root: /
  Bin dir: /nix/store/z26mymfryhqqh7y8w94qr0xc88q46dvr-ruby2.3.1-p0-nokogiri-1.6.8/lib/ruby/gems/2.3.1/bin
  Gem home: /nix/store/z26mymfryhqqh7y8w94qr0xc88q46dvr-ruby2.3.1-p0-nokogiri-1.6.8/lib/ruby/gems/2.3.1

...
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

不幸的是,运行 find/nix -name mkmf.log 失败。但是使用 Nix 环境手动配置 nokogiri 成功 ( detais )

最佳答案

我曾有过这样的配置:

with import <nixpkgs>{};

let
  bundler_ = bundler.override {
    ruby = ruby_2_3;
  };
  bundix_ = bundix.override {
    bundler = bundler_;
};

  gems = bundlerEnv {
    name = "company-bundler";
    inherit ruby_2_3;
    gemfile = ./Gemfile;
    lockfile = ./Gemfile.lock;
    gemset = ./gemset.nix;
  };

in

stdenv.mkDerivation rec {
  name = "company-tools";
  version = "0.1.0";
  buildInputs = [
    # Ruby
    ruby_2_3
    bundler_
    bundix_
    rake
    gems

    # Nokogiri
    libiconv
    libxml2
    libxslt
    zlib
    ];

    shellHook = ''
      # use a Gem directory within nix
      export GEM_HOME=$out/.gems
      export PATH=$out/.gems/bin:$PATH

      # For nokogiri
      export XSLT_LIB=${stdenv.lib.makeLibraryPath [ libxslt ]}
      bundle config build.nokogigi --with-xslt-lib=$XSLT_LIB
    '';
    }

我认为关键位是 XSLT_LIB var 和 bundle config

关于ruby - 如何使用具有 native 扩展的 Nix 构建 Ruby Gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37933375/

相关文章:

ruby-on-rails - 使用 'paperclip' gem 和 Ruby on Rails 3 时出现问题

ruby - 使用 ruby​​ on rails 在服务器中调用数据库函数

ruby - 为什么我在 nginx 下使用 Sinatra with Passenger 会出现 404 错误?

Docker — 无法连接到 unix :///var/run/docker. sock 上的 Docker 守护进程

nix - 如何通过在本地构建它们而不是下载预构建的二进制文件来强制 Nix 为 "install packages"?

git - Nix 包管理器和 GitLab 的问题

ruby-on-rails - 在 Windows 7 中安装 Ruby on Rails

ruby - 如何在 Ruby 中访问对象数组中的属性?

vim - 在 nixos 中,如何为 nix 语言获得 vim 语法高亮?

nix - nix-env 如何提前知道存储路径?