move all ruby modules to development/ruby-modules

This commit is contained in:
zimbatm
2016-03-19 22:06:26 +00:00
parent 3547ffa89f
commit 5a64bc44ea
12 changed files with 6 additions and 8 deletions
@@ -0,0 +1,209 @@
# This builds gems in a way that is compatible with bundler.
#
# Bundler installs gems from git sources _very_ differently from how RubyGems
# installes gem packages, though they both install gem packages similarly.
#
# We monkey-patch Bundler to remove any impurities and then drive its internals
# to install git gems.
#
# For the sake of simplicity, gem packages are installed with the standard `gem`
# program.
#
# Note that bundler does not support multiple prefixes; it assumes that all
# gems are installed in a common prefix, and has no support for specifying
# otherwise. Therefore, if you want to be able to use the resulting derivations
# with bundler, you need to create a symlink forrest first, which is what
# `bundlerEnv` does for you.
#
# Normal gem packages can be used outside of bundler; a binstub is created in
# $out/bin.
{ lib, ruby, bundler, fetchurl, fetchgit, makeWrapper, git,
buildRubyGem, darwin
} @ defs:
lib.makeOverridable (
{ name ? null
, gemName
, version ? null
, type ? "gem"
, document ? [] # e.g. [ "ri" "rdoc" ]
, platform ? "ruby"
, ruby ? defs.ruby
, stdenv ? ruby.stdenv
, namePrefix ? (let
rubyName = builtins.parseDrvName ruby.name;
in "${rubyName.name}${rubyName.version}-")
, buildInputs ? []
, doCheck ? false
, meta ? {}
, patches ? []
, gemPath ? []
, dontStrip ? true
, remotes ? ["https://rubygems.org"]
# Assume we don't have to build unless strictly necessary (e.g. the source is a
# git checkout).
# If you need to apply patches, make sure to set `dontBuild = false`;
, dontBuild ? true
, propagatedBuildInputs ? []
, propagatedUserEnvPkgs ? []
, buildFlags ? null
, passthru ? {}
, ...} @ attrs:
let
shellEscape = x: "'${lib.replaceChars ["'"] [("'\\'" + "'")] x}'";
src = attrs.src or (
if type == "gem" then
fetchurl {
urls = map (remote: "${remote}/gems/${gemName}-${version}.gem") remotes;
inherit (attrs) sha256;
}
else if type == "git" then
fetchgit {
inherit (attrs) url rev sha256 fetchSubmodules;
leaveDotGit = true;
}
else
throw "buildRubyGem: don't know how to build a gem of type \"${type}\""
);
documentFlag =
if document == []
then "-N"
else "--document ${lib.concatStringsSep "," document}";
in
stdenv.mkDerivation (attrs // {
inherit ruby;
inherit doCheck;
inherit dontBuild;
inherit dontStrip;
inherit type;
buildInputs = [
ruby makeWrapper
] ++ lib.optionals (type == "git") [ git bundler ]
++ lib.optional stdenv.isDarwin darwin.libobjc
++ buildInputs;
name = attrs.name or "${namePrefix}${gemName}-${version}";
inherit src;
phases = attrs.phases or [ "unpackPhase" "patchPhase" "buildPhase" "installPhase" "fixupPhase" ];
unpackPhase = attrs.unpackPhase or ''
runHook preUnpack
if [[ -f $src && $src == *.gem ]]; then
if [[ -z "$dontBuild" ]]; then
# we won't know the name of the directory that RubyGems creates,
# so we'll just use a glob to find it and move it over.
gempkg="$src"
sourceRoot=source
gem unpack $gempkg --target=container
cp -r container/* $sourceRoot
rm -r container
# copy out the original gemspec, for convenience during patching /
# overrides.
gem specification $gempkg --ruby > original.gemspec
gemspec=$(readlink -f .)/original.gemspec
else
gempkg="$src"
fi
else
# Fall back to the original thing for everything else.
dontBuild=""
preUnpack="" postUnpack="" unpackPhase
fi
runHook postUnpack
'';
buildPhase = attrs.buildPhase or ''
runHook preBuild
if [[ "$type" == "gem" ]]; then
if [[ -z "$gemspec" ]]; then
gemspec="$(find . -name '*.gemspec')"
echo "found the following gemspecs:"
echo "$gemspec"
gemspec="$(echo "$gemspec" | head -n1)"
fi
exec 3>&1
output="$(gem build $gemspec | tee >(cat - >&3))"
exec 3>&-
gempkg=$(echo "$output" | grep -oP 'File: \K(.*)')
echo "gem package built: $gempkg"
fi
runHook postBuild
'';
# Note:
# We really do need to keep the $out/${ruby.gemPath}/cache.
# This is very important in order for many parts of RubyGems/Bundler to not blow up.
# See https://github.com/bundler/bundler/issues/3327
installPhase = attrs.installPhase or ''
runHook preInstall
export GEM_HOME=$out/${ruby.gemPath}
mkdir -p $GEM_HOME
echo "buildFlags: $buildFlags"
${lib.optionalString (type == "git") ''
ruby ${./nix-bundle-install.rb} \
${gemName} \
${attrs.url} \
${src} \
${attrs.rev} \
${version} \
${shellEscape (toString buildFlags)}
''}
${lib.optionalString (type == "gem") ''
if [[ -z "$gempkg" ]]; then
echo "failure: \$gempkg path unspecified" 1>&2
exit 1
elif [[ ! -f "$gempkg" ]]; then
echo "failure: \$gempkg path invalid" 1>&2
exit 1
fi
gem install \
--local \
--force \
--http-proxy 'http://nodtd.invalid' \
--ignore-dependencies \
--build-root '/' \
--backtrace \
${documentFlag} \
$gempkg $gemFlags -- $buildFlags
# looks like useless files which break build repeatability and consume space
rm -fv $out/${ruby.gemPath}/doc/*/*/created.rid || true
rm -fv $out/${ruby.gemPath}/gems/*/ext/*/mkmf.log || true
# write out metadata and binstubs
spec=$(echo $out/${ruby.gemPath}/specifications/*.gemspec)
ruby ${./gem-post-build.rb} "$spec"
''}
runHook postInstall
'';
propagatedBuildInputs = gemPath ++ propagatedBuildInputs;
propagatedUserEnvPkgs = gemPath ++ propagatedUserEnvPkgs;
passthru = passthru // { isRubyGem = true; };
inherit meta;
})
)
@@ -0,0 +1,76 @@
require 'rbconfig'
require 'rubygems'
require 'rubygems/specification'
require 'fileutils'
ruby = File.join(ENV["ruby"], "bin", RbConfig::CONFIG['ruby_install_name'])
out = ENV["out"]
bin_path = File.join(ENV["out"], "bin")
gem_home = ENV["GEM_HOME"]
gem_path = ENV["GEM_PATH"].split(":")
install_path = Dir.glob("#{gem_home}/gems/*").first
gemspec_path = ARGV[0]
if defined?(Encoding.default_internal)
Encoding.default_internal = Encoding::UTF_8
Encoding.default_external = Encoding::UTF_8
end
gemspec_content = File.read(gemspec_path)
spec = nil
if gemspec_content[0..2] == "---" # YAML header
spec = Gem::Specification.from_yaml(gemspec_content)
else
spec = Gem::Specification.load(gemspec_path)
end
FileUtils.mkdir_p("#{out}/nix-support")
# write meta-data
meta = "#{out}/nix-support/gem-meta"
FileUtils.mkdir_p(meta)
FileUtils.ln_s(gemspec_path, "#{meta}/spec")
File.open("#{meta}/name", "w") do |f|
f.write(spec.name)
end
File.open("#{meta}/install-path", "w") do |f|
f.write(install_path)
end
File.open("#{meta}/require-paths", "w") do |f|
f.write(spec.require_paths.join(" "))
end
File.open("#{meta}/executables", "w") do |f|
f.write(spec.executables.join(" "))
end
# add this gem to the GEM_PATH for dependencies
File.open("#{out}/nix-support/setup-hook", "a") do |f|
f.puts("addToSearchPath GEM_PATH #{gem_home}")
spec.require_paths.each do |dir|
f.puts("addToSearchPath RUBYLIB #{install_path}/#{dir}")
end
end
# create regular rubygems binstubs
FileUtils.mkdir_p(bin_path)
spec.executables.each do |exe|
File.open("#{bin_path}/#{exe}", "w") do |f|
f.write(<<-EOF)
#!#{ruby}
#
# This file was generated by Nix.
#
# The application '#{exe}' is installed as part of a gem, and
# this file is here to facilitate running it.
#
Gem.use_paths "#{gem_home}", #{gem_path.to_s}
require 'rubygems'
load Gem.bin_path(#{spec.name.inspect}, #{exe.inspect})
EOF
end
FileUtils.chmod("+x", "#{bin_path}/#{exe}")
end
@@ -0,0 +1,152 @@
require 'rbconfig'
require 'bundler/vendored_thor'
require 'bundler'
require 'rubygems/command'
require 'fileutils'
require 'pathname'
require 'tmpdir'
# Options:
#
# name - the gem name
# uri - git repo uri
# repo - path to local checkout
# ref - the commit hash
# version - gem version
# build-flags - build arguments
ruby = File.join(ENV["ruby"], "bin", RbConfig::CONFIG['ruby_install_name'])
out = ENV["out"]
bin_dir = File.join(ENV["out"], "bin")
name = ARGV[0]
uri = ARGV[1]
REPO = ARGV[2]
ref = ARGV[3]
version = ARGV[4]
build_flags = ARGV[5]
# options to pass to bundler
options = {
"name" => name,
"uri" => uri,
"ref" => ref,
"version" => version,
}
# Monkey-patch Bundler to use our local checkout.
# I wish we didn't have to do this, but bundler does not expose an API to do
# these kinds of things.
Bundler.module_eval do
def self.requires_sudo?
false
end
def self.root
# we don't have a Gemfile, so it doesn't make sense to try to make paths
# relative to the (non existent) parent directory thereof, so we give a
# nonsense path here.
Pathname.new("/no-root-path")
end
def self.bundle_path
Pathname.new(ENV["GEM_HOME"])
end
def self.locked_gems
nil
end
end
Bundler::Source::Git.class_eval do
def allow_git_ops?
true
end
end
Bundler::Source::Git::GitProxy.class_eval do
def checkout
unless path.exist?
FileUtils.mkdir_p(path.dirname)
FileUtils.cp_r(File.join(REPO, ".git"), path)
system("chmod -R +w #{path}")
end
end
def copy_to(destination, submodules=false)
unless File.exist?(destination.join(".git"))
FileUtils.mkdir_p(destination.dirname)
FileUtils.cp_r(REPO, destination)
system("chmod -R +w #{destination}")
end
end
end
# UI
verbose = false
no_color = false
Bundler.ui = Bundler::UI::Shell.new({"no-color" => no_color})
Bundler.ui.level = "debug" if verbose
# Install
source = Bundler::Source::Git.new(options)
spec = source.specs.search_all(name).first
Bundler.rubygems.with_build_args [build_flags] do
source.install(spec)
end
msg = spec.post_install_message
if msg
Bundler.ui.confirm "Post-install message from #{name}:"
Bundler.ui.info msg
end
# Write out the binstubs
if spec.executables.any?
FileUtils.mkdir_p(bin_dir)
spec.executables.each do |exe|
wrapper = File.join(bin_dir, exe)
File.open(wrapper, "w") do |f|
f.write(<<-EOF)
#!#{ruby}
#
# This file was generated by Nix.
#
# The application '#{exe}' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path(#{spec.name.inspect}, #{exe.inspect})
EOF
end
FileUtils.chmod("+x", wrapper)
end
end
# Write out metadata
meta = "#{out}/nix-support/gem-meta"
FileUtils.mkdir_p(meta)
FileUtils.ln_s(spec.loaded_from.to_s, "#{meta}/spec")
File.open("#{meta}/name", "w") do |f|
f.write spec.name
end
File.open("#{meta}/install-path", "w") do |f|
f.write source.install_path.to_s
end
File.open("#{meta}/require-paths", "w") do |f|
f.write spec.require_paths.join(" ")
end
File.open("#{meta}/executables", "w") do |f|
f.write spec.executables.join(" ")
end
# make the lib available during bundler/git installs
File.open("#{out}/nix-support/setup-hook", "a") do |f|
spec.require_paths.each do |dir|
f.puts("addToSearchPath RUBYLIB #{source.install_path}/#{dir}")
end
end