2015-01-20 01:02:47 +00:00
|
|
|
{ stdenv, runCommand, writeText, writeScript, writeScriptBin, ruby, lib
|
2015-01-22 01:33:19 +00:00
|
|
|
, callPackage, defaultGemConfig, fetchurl, fetchgit, buildRubyGem , bundler_HEAD
|
2015-01-20 01:02:47 +00:00
|
|
|
, git
|
2015-01-18 23:47:28 +00:00
|
|
|
}@defs:
|
|
|
|
|
|
|
|
# This is a work-in-progress.
|
2015-01-20 01:02:47 +00:00
|
|
|
# The idea is that his will replace load-ruby-env.nix.
|
2015-01-18 23:47:28 +00:00
|
|
|
|
2015-01-22 01:33:19 +00:00
|
|
|
{ name, gemset, gemfile, lockfile, ruby ? defs.ruby, gemConfig ? defaultGemConfig
|
2015-01-21 04:33:57 +00:00
|
|
|
, enableParallelBuilding ? false # TODO: this might not work, given the env-var shinanigans.
|
2015-01-25 21:01:48 +00:00
|
|
|
, postInstall ? null
|
2015-01-21 20:20:42 +00:00
|
|
|
, documentation ? false
|
|
|
|
, meta ? {}
|
|
|
|
}@args:
|
2015-01-18 23:47:28 +00:00
|
|
|
|
|
|
|
let
|
2015-01-21 04:33:57 +00:00
|
|
|
|
|
|
|
shellEscape = x: "'${lib.replaceChars ["'"] [("'\\'" + "'")] x}'";
|
2015-01-18 23:47:28 +00:00
|
|
|
const = x: y: x;
|
2015-01-20 01:02:47 +00:00
|
|
|
bundler = bundler_HEAD.override { inherit ruby; };
|
|
|
|
inherit (builtins) attrValues;
|
2015-01-18 23:47:28 +00:00
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
gemName = attrs: "${attrs.name}-${attrs.version}.gem";
|
2015-01-20 01:02:47 +00:00
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
fetchers.path = attrs: attrs.source.path;
|
|
|
|
fetchers.gem = attrs: fetchurl {
|
|
|
|
url = "${attrs.source.source or "https://rubygems.org"}/downloads/${gemName attrs}";
|
|
|
|
inherit (attrs.source) sha256;
|
|
|
|
};
|
2015-01-18 23:47:28 +00:00
|
|
|
fetchers.git = attrs: fetchgit {
|
2015-01-20 06:07:55 +00:00
|
|
|
inherit (attrs.source) url rev sha256 fetchSubmodules;
|
2015-01-18 23:47:28 +00:00
|
|
|
leaveDotGit = true;
|
|
|
|
};
|
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
applySrc = attrs:
|
|
|
|
attrs // {
|
|
|
|
src = (fetchers."${attrs.source.type}" attrs);
|
|
|
|
};
|
|
|
|
|
2015-01-22 01:33:19 +00:00
|
|
|
applyGemConfigs = attrs:
|
|
|
|
if gemConfig ? "${attrs.name}"
|
|
|
|
then attrs // gemConfig."${attrs.name}" attrs
|
2015-01-20 06:07:55 +00:00
|
|
|
else attrs;
|
|
|
|
|
2015-01-21 00:09:02 +00:00
|
|
|
needsPatch = attrs:
|
|
|
|
(attrs ? patches) || (attrs ? prePatch) || (attrs ? postPatch);
|
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
# patch a gem or source tree.
|
|
|
|
# for gems, the gem is unpacked, patched, and then repacked.
|
|
|
|
# see: https://github.com/fedora-ruby/gem-patch/blob/master/lib/rubygems/patcher.rb
|
2015-01-21 00:09:02 +00:00
|
|
|
applyPatches = attrs:
|
|
|
|
if !needsPatch attrs
|
2015-01-20 06:07:55 +00:00
|
|
|
then attrs
|
|
|
|
else attrs // { src =
|
|
|
|
stdenv.mkDerivation {
|
|
|
|
name = gemName attrs;
|
|
|
|
phases = [ "unpackPhase" "patchPhase" "installPhase" ];
|
2015-01-21 00:09:02 +00:00
|
|
|
buildInputs = [ ruby ] ++ attrs.buildInputs or [];
|
|
|
|
patches = attrs.patches or [ ];
|
|
|
|
prePatch = attrs.prePatch or "true";
|
|
|
|
postPatch = attrs.postPatch or "true";
|
2015-01-20 06:07:55 +00:00
|
|
|
unpackPhase = ''
|
|
|
|
runHook preUnpack
|
|
|
|
|
|
|
|
if [[ -f ${attrs.src} ]]; then
|
|
|
|
isGem=1
|
2015-01-21 00:09:02 +00:00
|
|
|
# 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.
|
|
|
|
gem unpack ${attrs.src} --target=container
|
|
|
|
cp -r container/* contents
|
|
|
|
rm -r container
|
2015-01-20 06:07:55 +00:00
|
|
|
else
|
|
|
|
cp -r ${attrs.src} contents
|
|
|
|
chmod -R +w contents
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd contents
|
|
|
|
runHook postUnpack
|
|
|
|
'';
|
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
|
|
|
|
if [[ -n "$isGem" ]]; then
|
|
|
|
${writeScript "repack.rb" ''
|
|
|
|
#!${ruby}/bin/ruby
|
|
|
|
require 'rubygems'
|
2015-01-21 00:09:02 +00:00
|
|
|
require 'rubygems/package'
|
2015-01-20 06:07:55 +00:00
|
|
|
require 'fileutils'
|
|
|
|
|
2015-01-21 00:09:02 +00:00
|
|
|
if defined?(Encoding.default_internal)
|
|
|
|
Encoding.default_internal = Encoding::UTF_8
|
|
|
|
Encoding.default_external = Encoding::UTF_8
|
|
|
|
end
|
|
|
|
|
|
|
|
if Gem::VERSION < '2.0'
|
|
|
|
load "${./package-1.8.rb}"
|
|
|
|
end
|
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
out = ENV['out']
|
|
|
|
files = Dir['**/{.[^\.]*,*}']
|
2015-01-21 00:09:02 +00:00
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
package = Gem::Package.new("${attrs.src}")
|
|
|
|
patched_package = Gem::Package.new(package.spec.file_name)
|
|
|
|
patched_package.spec = package.spec.clone
|
|
|
|
patched_package.spec.files = files
|
|
|
|
|
2015-01-21 00:09:02 +00:00
|
|
|
patched_package.build(false)
|
|
|
|
|
|
|
|
FileUtils.cp(patched_package.spec.file_name, out)
|
2015-01-20 06:07:55 +00:00
|
|
|
''}
|
|
|
|
else
|
2015-01-25 21:01:48 +00:00
|
|
|
cp -r . $out
|
2015-01-20 06:07:55 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2015-01-18 23:47:28 +00:00
|
|
|
|
|
|
|
instantiate = (attrs:
|
2015-01-22 01:33:19 +00:00
|
|
|
applyPatches (applyGemConfigs (applySrc attrs))
|
2015-01-18 23:47:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
instantiated = lib.flip lib.mapAttrs (import gemset) (name: attrs:
|
|
|
|
instantiate (attrs // { inherit name; })
|
|
|
|
);
|
|
|
|
|
2015-01-21 01:21:03 +00:00
|
|
|
needsPreInstall = attrs:
|
2015-01-21 03:20:28 +00:00
|
|
|
(attrs ? preInstall) || (attrs ? buildInputs) || (attrs ? nativeBuildInputs);
|
2015-01-21 01:21:03 +00:00
|
|
|
|
2015-01-21 20:20:42 +00:00
|
|
|
# TODO: support cross compilation? look at stdenv/generic/default.nix.
|
|
|
|
runPreInstallers = lib.fold (next: acc:
|
2015-01-21 01:21:03 +00:00
|
|
|
if !needsPreInstall next
|
|
|
|
then acc
|
|
|
|
else acc + ''
|
2015-01-21 20:20:42 +00:00
|
|
|
${writeScript "${next.name}-pre-install" ''
|
2015-01-21 04:33:57 +00:00
|
|
|
#!${stdenv.shell}
|
2015-01-21 03:20:28 +00:00
|
|
|
|
2015-01-21 20:20:42 +00:00
|
|
|
export nativeBuildInputs="${toString ((next.nativeBuildInputs or []) ++ (next.buildInputs or []))}"
|
2015-01-21 03:20:28 +00:00
|
|
|
|
2015-01-21 01:21:03 +00:00
|
|
|
source ${stdenv}/setup
|
|
|
|
|
2015-01-21 20:20:42 +00:00
|
|
|
header "running pre-install script for ${next.name}"
|
|
|
|
|
2015-01-21 01:21:03 +00:00
|
|
|
${next.preInstall or ""}
|
|
|
|
|
2015-01-21 03:20:28 +00:00
|
|
|
${ruby}/bin/ruby -e 'print ENV.inspect' > env/${next.name}
|
2015-01-21 20:20:42 +00:00
|
|
|
|
|
|
|
stopNest
|
|
|
|
''}
|
2015-01-21 01:21:03 +00:00
|
|
|
''
|
|
|
|
) "" (attrValues instantiated);
|
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
# copy *.gem to ./gems
|
|
|
|
copyGems = lib.fold (next: acc:
|
2015-01-20 01:02:47 +00:00
|
|
|
if next.source.type == "gem"
|
2015-01-20 06:07:55 +00:00
|
|
|
then acc + "cp ${next.src} gems/${gemName next}\n"
|
2015-01-20 01:02:47 +00:00
|
|
|
else acc
|
2015-01-20 06:07:55 +00:00
|
|
|
) "" (attrValues instantiated);
|
2015-01-20 01:02:47 +00:00
|
|
|
|
2015-01-18 23:47:28 +00:00
|
|
|
runRuby = name: env: command:
|
|
|
|
runCommand name env ''
|
|
|
|
${ruby}/bin/ruby ${writeText name command}
|
|
|
|
'';
|
|
|
|
|
|
|
|
# TODO: include json_pure, so the version of ruby doesn't matter.
|
|
|
|
# not all rubies have support for JSON built-in,
|
|
|
|
# so we'll convert JSON to ruby expressions.
|
2015-01-20 06:07:55 +00:00
|
|
|
json2rb = writeScript "json2rb" ''
|
2015-01-18 23:47:28 +00:00
|
|
|
#!${ruby}/bin/ruby
|
|
|
|
begin
|
|
|
|
require 'json'
|
|
|
|
rescue LoadError => ex
|
|
|
|
require 'json_pure'
|
|
|
|
end
|
|
|
|
|
|
|
|
puts JSON.parse(STDIN.read).inspect
|
|
|
|
'';
|
|
|
|
|
|
|
|
# dump the instantiated gemset as a ruby expression.
|
|
|
|
serializedGemset = runCommand "gemset.rb" { json = builtins.toJSON instantiated; } ''
|
2015-01-20 06:07:55 +00:00
|
|
|
printf '%s' "$json" | ${json2rb} > $out
|
2015-01-18 23:47:28 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
# this is a mapping from a source type and identifier (uri/path/etc)
|
|
|
|
# to the pure store path.
|
|
|
|
# we'll use this from the patched bundler to make fetching sources pure.
|
|
|
|
sources = runRuby "sources.rb" { gemset = serializedGemset; } ''
|
|
|
|
out = ENV['out']
|
|
|
|
gemset = eval(File.read(ENV['gemset']))
|
|
|
|
|
|
|
|
sources = {
|
|
|
|
"git" => { },
|
|
|
|
"path" => { },
|
|
|
|
"gem" => { },
|
|
|
|
"svn" => { }
|
|
|
|
}
|
|
|
|
|
|
|
|
gemset.each_value do |spec|
|
|
|
|
type = spec["source"]["type"]
|
|
|
|
val = spec["src"]
|
|
|
|
key =
|
|
|
|
case type
|
|
|
|
when "gem"
|
|
|
|
spec["name"]
|
|
|
|
when "git"
|
|
|
|
spec["source"]["url"]
|
|
|
|
when "path"
|
|
|
|
spec["source"]["originalPath"]
|
|
|
|
when "svn"
|
|
|
|
nil # TODO
|
|
|
|
end
|
|
|
|
|
|
|
|
sources[type][key] = val if key
|
|
|
|
end
|
|
|
|
|
|
|
|
File.open(out, "wb") do |f|
|
|
|
|
f.print sources.inspect
|
|
|
|
end
|
|
|
|
'';
|
|
|
|
|
|
|
|
# rewrite PATH sources to point into the nix store.
|
2015-01-20 06:07:55 +00:00
|
|
|
purifiedLockfile = runRuby "purifiedLockfile" {} ''
|
2015-01-20 01:02:47 +00:00
|
|
|
out = ENV['out']
|
|
|
|
sources = eval(File.read("${sources}"))
|
|
|
|
paths = sources["path"]
|
2015-01-18 23:47:28 +00:00
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
lockfile = File.read("${lockfile}")
|
2015-01-18 23:47:28 +00:00
|
|
|
|
|
|
|
paths.each_pair do |impure, pure|
|
|
|
|
lockfile.gsub!(/^ remote: #{Regexp.escape(impure)}/, " remote: #{pure}")
|
|
|
|
end
|
|
|
|
|
2015-01-20 06:07:55 +00:00
|
|
|
File.open(out, "wb") do |f|
|
|
|
|
f.print lockfile
|
|
|
|
end
|
2015-01-18 23:47:28 +00:00
|
|
|
'';
|
|
|
|
|
2015-01-22 01:33:19 +00:00
|
|
|
needsBuildFlags = attrs: attrs ? buildFlags;
|
2015-01-21 04:33:57 +00:00
|
|
|
|
2015-01-22 01:33:19 +00:00
|
|
|
mkBuildFlags = spec:
|
|
|
|
"export BUNDLE_BUILD__${lib.toUpper spec.name}='${lib.concatStringsSep " " (map shellEscape spec.buildFlags)}'";
|
2015-01-21 04:33:57 +00:00
|
|
|
|
2015-01-22 01:33:19 +00:00
|
|
|
allBuildFlags =
|
2015-01-21 04:33:57 +00:00
|
|
|
lib.concatStringsSep "\n"
|
2015-01-22 01:33:19 +00:00
|
|
|
(map mkBuildFlags
|
|
|
|
(lib.filter needsBuildFlags (attrValues instantiated)));
|
2015-01-21 04:33:57 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
derivation = stdenv.mkDerivation {
|
|
|
|
inherit name;
|
2015-01-18 23:47:28 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
buildInputs = [
|
|
|
|
ruby
|
|
|
|
bundler
|
|
|
|
git
|
|
|
|
];
|
2015-01-21 20:20:42 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
phases = [ "installPhase" "fixupPhase" ];
|
2015-01-21 20:20:42 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
outputs = [
|
|
|
|
"out" # the installed libs/bins
|
|
|
|
"bundle" # supporting files for bundler
|
|
|
|
];
|
2015-01-21 20:20:42 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
installPhase = ''
|
|
|
|
mkdir -p $bundle
|
|
|
|
export BUNDLE_GEMFILE=$bundle/Gemfile
|
|
|
|
cp ${gemfile} $BUNDLE_GEMFILE
|
|
|
|
cp ${purifiedLockfile} $BUNDLE_GEMFILE.lock
|
2015-01-21 20:20:42 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
export NIX_GEM_SOURCES=${sources}
|
|
|
|
export NIX_BUNDLER_GEMPATH=${bundler}/${ruby.gemPath}
|
2015-01-18 23:47:28 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
export GEM_HOME=$out/${ruby.gemPath}
|
|
|
|
export GEM_PATH=$NIX_BUNDLER_GEMPATH:$GEM_HOME
|
|
|
|
mkdir -p $GEM_HOME
|
2015-01-18 23:47:28 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
${allBuildFlags}
|
2015-01-18 23:47:28 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
mkdir gems
|
|
|
|
cp ${bundler}/${bundler.ruby.gemPath}/cache/bundler-*.gem gems
|
|
|
|
${copyGems}
|
2015-01-22 01:46:50 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
${lib.optionalString (!documentation) ''
|
|
|
|
mkdir home
|
|
|
|
HOME="$(pwd -P)/home"
|
|
|
|
echo "gem: --no-rdoc --no-ri" > $HOME/.gemrc
|
|
|
|
''}
|
2015-01-20 01:02:47 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
mkdir env
|
|
|
|
${runPreInstallers}
|
2015-01-21 04:33:57 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
mkdir $out/bin
|
|
|
|
cp ${./monkey_patches.rb} monkey_patches.rb
|
|
|
|
export RUBYOPT="-rmonkey_patches.rb -I $(pwd -P)"
|
|
|
|
bundler install --frozen --binstubs ${lib.optionalString enableParallelBuilding "--jobs $NIX_BUILD_CORES"}
|
|
|
|
RUBYOPT=""
|
2015-01-22 01:46:50 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
runHook postInstall
|
|
|
|
'';
|
2015-01-25 21:01:48 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
inherit postInstall;
|
2015-01-21 20:20:42 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
passthru = {
|
|
|
|
inherit ruby;
|
|
|
|
inherit bundler;
|
|
|
|
|
|
|
|
env = let
|
|
|
|
irbrc = builtins.toFile "irbrc" ''
|
|
|
|
if not ENV["OLD_IRBRC"].empty?
|
|
|
|
require ENV["OLD_IRBRC"]
|
|
|
|
end
|
|
|
|
require 'rubygems'
|
|
|
|
require 'bundler/setup'
|
|
|
|
'';
|
|
|
|
in stdenv.mkDerivation {
|
|
|
|
name = "interactive-${name}-environment";
|
|
|
|
nativeBuildInputs = [ ruby derivation ];
|
|
|
|
shellHook = ''
|
|
|
|
export BUNDLE_GEMFILE=${derivation.bundle}/Gemfile
|
|
|
|
export GEM_HOME=${derivation}/${ruby.gemPath}
|
|
|
|
export NIX_BUNDLER_GEMPATH=${bundler}/${ruby.gemPath}
|
|
|
|
export GEM_PATH=$NIX_BUNDLER_GEMPATH:$GEM_HOME
|
|
|
|
export OLD_IRBRC="$IRBRC"
|
|
|
|
export IRBRC=${irbrc}
|
|
|
|
'';
|
|
|
|
buildCommand = ''
|
|
|
|
echo >&2 ""
|
|
|
|
echo >&2 "*** Ruby 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
|
|
|
|
echo >&2 ""
|
|
|
|
exit 1
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2015-01-25 21:01:48 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
inherit meta;
|
2015-01-18 23:47:28 +00:00
|
|
|
};
|
2015-01-21 20:20:42 +00:00
|
|
|
|
2015-02-04 16:41:37 +00:00
|
|
|
in derivation
|