at 18.03-beta 6.1 kB view raw
1# This builds gems in a way that is compatible with bundler. 2# 3# Bundler installs gems from git sources _very_ differently from how RubyGems 4# installes gem packages, though they both install gem packages similarly. 5# 6# We monkey-patch Bundler to remove any impurities and then drive its internals 7# to install git gems. 8# 9# For the sake of simplicity, gem packages are installed with the standard `gem` 10# program. 11# 12# Note that bundler does not support multiple prefixes; it assumes that all 13# gems are installed in a common prefix, and has no support for specifying 14# otherwise. Therefore, if you want to be able to use the resulting derivations 15# with bundler, you need to create a symlink forrest first, which is what 16# `bundlerEnv` does for you. 17# 18# Normal gem packages can be used outside of bundler; a binstub is created in 19# $out/bin. 20 21{ lib, fetchurl, fetchgit, makeWrapper, git, darwin 22, ruby, bundler 23} @ defs: 24 25lib.makeOverridable ( 26 27{ name ? null 28, gemName 29, version ? null 30, type ? "gem" 31, document ? [] # e.g. [ "ri" "rdoc" ] 32, platform ? "ruby" 33, ruby ? defs.ruby 34, stdenv ? ruby.stdenv 35, namePrefix ? (let 36 rubyName = builtins.parseDrvName ruby.name; 37 in "${rubyName.name}${rubyName.version}-") 38, buildInputs ? [] 39, meta ? {} 40, patches ? [] 41, gemPath ? [] 42, dontStrip ? true 43# Assume we don't have to build unless strictly necessary (e.g. the source is a 44# git checkout). 45# If you need to apply patches, make sure to set `dontBuild = false`; 46, dontBuild ? true 47, propagatedBuildInputs ? [] 48, propagatedUserEnvPkgs ? [] 49, buildFlags ? [] 50, passthru ? {} 51, ...} @ attrs: 52 53let 54 src = attrs.src or ( 55 if type == "gem" then 56 fetchurl { 57 urls = map ( 58 remote: "${remote}/gems/${gemName}-${version}.gem" 59 ) (attrs.source.remotes or [ "https://rubygems.org" ]); 60 inherit (attrs.source) sha256; 61 } 62 else if type == "git" then 63 fetchgit { 64 inherit (attrs.source) url rev sha256 fetchSubmodules; 65 leaveDotGit = true; 66 } 67 else if type == "url" then 68 fetchurl attrs.source 69 else 70 throw "buildRubyGem: don't know how to build a gem of type \"${type}\"" 71 ); 72 documentFlag = 73 if document == [] 74 then "-N" 75 else "--document ${lib.concatStringsSep "," document}"; 76 77in 78 79stdenv.mkDerivation ((builtins.removeAttrs attrs ["source"]) // { 80 inherit ruby; 81 inherit dontBuild; 82 inherit dontStrip; 83 inherit type; 84 85 buildInputs = [ 86 ruby makeWrapper 87 ] ++ lib.optionals (type == "git") [ git ] 88 ++ lib.optionals (type != "gem") [ bundler ] 89 ++ lib.optional stdenv.isDarwin darwin.libobjc 90 ++ buildInputs; 91 92 #name = builtins.trace (attrs.name or "no attr.name" ) "${namePrefix}${gemName}-${version}"; 93 name = attrs.name or "${namePrefix}${gemName}-${version}"; 94 95 inherit src; 96 97 unpackPhase = attrs.unpackPhase or '' 98 runHook preUnpack 99 100 if [[ -f $src && $src == *.gem ]]; then 101 if [[ -z "$dontBuild" ]]; then 102 # we won't know the name of the directory that RubyGems creates, 103 # so we'll just use a glob to find it and move it over. 104 gempkg="$src" 105 sourceRoot=source 106 gem unpack $gempkg --target=container 107 cp -r container/* $sourceRoot 108 rm -r container 109 110 # copy out the original gemspec, for convenience during patching / 111 # overrides. 112 gem specification $gempkg --ruby > original.gemspec 113 gemspec=$(readlink -f .)/original.gemspec 114 else 115 gempkg="$src" 116 fi 117 else 118 # Fall back to the original thing for everything else. 119 dontBuild="" 120 preUnpack="" postUnpack="" unpackPhase 121 fi 122 123 runHook postUnpack 124 ''; 125 126 buildPhase = attrs.buildPhase or '' 127 runHook preBuild 128 129 if [[ "$type" == "gem" ]]; then 130 if [[ -z "$gemspec" ]]; then 131 gemspec="$(find . -name '*.gemspec')" 132 echo "found the following gemspecs:" 133 echo "$gemspec" 134 gemspec="$(echo "$gemspec" | head -n1)" 135 fi 136 137 exec 3>&1 138 output="$(gem build $gemspec | tee >(cat - >&3))" 139 exec 3>&- 140 141 gempkg=$(echo "$output" | grep -oP 'File: \K(.*)') 142 143 echo "gem package built: $gempkg" 144 fi 145 146 runHook postBuild 147 ''; 148 149 # Note: 150 # We really do need to keep the $out/${ruby.gemPath}/cache. 151 # This is very important in order for many parts of RubyGems/Bundler to not blow up. 152 # See https://github.com/bundler/bundler/issues/3327 153 installPhase = attrs.installPhase or '' 154 runHook preInstall 155 156 export GEM_HOME=$out/${ruby.gemPath} 157 mkdir -p $GEM_HOME 158 159 echo "buildFlags: $buildFlags" 160 161 ${lib.optionalString (type == "url") '' 162 ruby ${./nix-bundle-install.rb} \ 163 "path" \ 164 '${gemName}' \ 165 '${version}' \ 166 '${lib.escapeShellArgs buildFlags}' 167 ''} 168 ${lib.optionalString (type == "git") '' 169 ruby ${./nix-bundle-install.rb} \ 170 "git" \ 171 '${gemName}' \ 172 '${version}' \ 173 '${lib.escapeShellArgs buildFlags}' \ 174 '${attrs.source.url}' \ 175 '${src}' \ 176 '${attrs.source.rev}' 177 ''} 178 179 ${lib.optionalString (type == "gem") '' 180 if [[ -z "$gempkg" ]]; then 181 echo "failure: \$gempkg path unspecified" 1>&2 182 exit 1 183 elif [[ ! -f "$gempkg" ]]; then 184 echo "failure: \$gempkg path invalid" 1>&2 185 exit 1 186 fi 187 188 gem install \ 189 --local \ 190 --force \ 191 --http-proxy 'http://nodtd.invalid' \ 192 --ignore-dependencies \ 193 --install-dir "$GEM_HOME" \ 194 --build-root '/' \ 195 --backtrace \ 196 --no-env-shebang \ 197 ${documentFlag} \ 198 $gempkg $gemFlags -- $buildFlags 199 200 # looks like useless files which break build repeatability and consume space 201 rm -fv $out/${ruby.gemPath}/doc/*/*/created.rid || true 202 rm -fv $out/${ruby.gemPath}/gems/*/ext/*/mkmf.log || true 203 204 # write out metadata and binstubs 205 spec=$(echo $out/${ruby.gemPath}/specifications/*.gemspec) 206 ruby ${./gem-post-build.rb} "$spec" 207 ''} 208 209 runHook postInstall 210 ''; 211 212 propagatedBuildInputs = gemPath ++ propagatedBuildInputs; 213 propagatedUserEnvPkgs = gemPath ++ propagatedUserEnvPkgs; 214 215 passthru = passthru // { isRubyGem = true; }; 216 inherit meta; 217}) 218 219)