Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at release-19.03 228 lines 6.5 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, dontInstallManpages ? false 48, propagatedBuildInputs ? [] 49, propagatedUserEnvPkgs ? [] 50, buildFlags ? [] 51, passthru ? {} 52, ...} @ attrs: 53 54let 55 src = attrs.src or ( 56 if type == "gem" then 57 fetchurl { 58 urls = map ( 59 remote: "${remote}/gems/${gemName}-${version}.gem" 60 ) (attrs.source.remotes or [ "https://rubygems.org" ]); 61 inherit (attrs.source) sha256; 62 } 63 else if type == "git" then 64 fetchgit { 65 inherit (attrs.source) url rev sha256 fetchSubmodules; 66 leaveDotGit = true; 67 } 68 else if type == "url" then 69 fetchurl attrs.source 70 else 71 throw "buildRubyGem: don't know how to build a gem of type \"${type}\"" 72 ); 73 documentFlag = 74 if document == [] 75 then "-N" 76 else "--document ${lib.concatStringsSep "," document}"; 77 78in 79 80stdenv.mkDerivation ((builtins.removeAttrs attrs ["source"]) // { 81 inherit ruby; 82 inherit dontBuild; 83 inherit dontStrip; 84 inherit type; 85 86 buildInputs = [ 87 ruby makeWrapper 88 ] ++ lib.optionals (type == "git") [ git ] 89 ++ lib.optionals (type != "gem") [ bundler ] 90 ++ lib.optional stdenv.isDarwin darwin.libobjc 91 ++ buildInputs; 92 93 #name = builtins.trace (attrs.name or "no attr.name" ) "${namePrefix}${gemName}-${version}"; 94 name = attrs.name or "${namePrefix}${gemName}-${version}"; 95 96 inherit src; 97 98 unpackPhase = attrs.unpackPhase or '' 99 runHook preUnpack 100 101 if [[ -f $src && $src == *.gem ]]; then 102 if [[ -z "$dontBuild" ]]; then 103 # we won't know the name of the directory that RubyGems creates, 104 # so we'll just use a glob to find it and move it over. 105 gempkg="$src" 106 sourceRoot=source 107 gem unpack $gempkg --target=container 108 cp -r container/* $sourceRoot 109 rm -r container 110 111 # copy out the original gemspec, for convenience during patching / 112 # overrides. 113 gem specification $gempkg --ruby > original.gemspec 114 gemspec=$(readlink -f .)/original.gemspec 115 else 116 gempkg="$src" 117 fi 118 else 119 # Fall back to the original thing for everything else. 120 dontBuild="" 121 preUnpack="" postUnpack="" unpackPhase 122 fi 123 124 runHook postUnpack 125 ''; 126 127 buildPhase = attrs.buildPhase or '' 128 runHook preBuild 129 130 if [[ "$type" == "gem" ]]; then 131 if [[ -z "$gemspec" ]]; then 132 gemspec="$(find . -name '*.gemspec')" 133 echo "found the following gemspecs:" 134 echo "$gemspec" 135 gemspec="$(echo "$gemspec" | head -n1)" 136 fi 137 138 exec 3>&1 139 output="$(gem build $gemspec | tee >(cat - >&3))" 140 exec 3>&- 141 142 gempkg=$(echo "$output" | grep -oP 'File: \K(.*)') 143 144 echo "gem package built: $gempkg" 145 fi 146 147 runHook postBuild 148 ''; 149 150 # Note: 151 # We really do need to keep the $out/${ruby.gemPath}/cache. 152 # This is very important in order for many parts of RubyGems/Bundler to not blow up. 153 # See https://github.com/bundler/bundler/issues/3327 154 installPhase = attrs.installPhase or '' 155 runHook preInstall 156 157 export GEM_HOME=$out/${ruby.gemPath} 158 mkdir -p $GEM_HOME 159 160 echo "buildFlags: $buildFlags" 161 162 ${lib.optionalString (type == "url") '' 163 ruby ${./nix-bundle-install.rb} \ 164 "path" \ 165 '${gemName}' \ 166 '${version}' \ 167 '${lib.escapeShellArgs buildFlags}' 168 ''} 169 ${lib.optionalString (type == "git") '' 170 ruby ${./nix-bundle-install.rb} \ 171 "git" \ 172 '${gemName}' \ 173 '${version}' \ 174 '${lib.escapeShellArgs buildFlags}' \ 175 '${attrs.source.url}' \ 176 '${src}' \ 177 '${attrs.source.rev}' 178 ''} 179 180 ${lib.optionalString (type == "gem") '' 181 if [[ -z "$gempkg" ]]; then 182 echo "failure: \$gempkg path unspecified" 1>&2 183 exit 1 184 elif [[ ! -f "$gempkg" ]]; then 185 echo "failure: \$gempkg path invalid" 1>&2 186 exit 1 187 fi 188 189 gem install \ 190 --local \ 191 --force \ 192 --http-proxy 'http://nodtd.invalid' \ 193 --ignore-dependencies \ 194 --install-dir "$GEM_HOME" \ 195 --build-root '/' \ 196 --backtrace \ 197 --no-env-shebang \ 198 ${documentFlag} \ 199 $gempkg $gemFlags -- $buildFlags 200 201 # looks like useless files which break build repeatability and consume space 202 rm -fv $out/${ruby.gemPath}/doc/*/*/created.rid || true 203 rm -fv $out/${ruby.gemPath}/gems/*/ext/*/mkmf.log || true 204 205 # write out metadata and binstubs 206 spec=$(echo $out/${ruby.gemPath}/specifications/*.gemspec) 207 ruby ${./gem-post-build.rb} "$spec" 208 ''} 209 210 ${lib.optionalString (!dontInstallManpages) '' 211 for section in {1..9}; do 212 mandir="$out/share/man/man$section" 213 find $out/lib \( -wholename "*/man/*.$section" -o -wholename "*/man/man$section/*.$section" \) \ 214 -execdir mkdir -p $mandir \; -execdir cp '{}' $mandir \; 215 done 216 ''} 217 218 runHook postInstall 219 ''; 220 221 propagatedBuildInputs = gemPath ++ propagatedBuildInputs; 222 propagatedUserEnvPkgs = gemPath ++ propagatedUserEnvPkgs; 223 224 passthru = passthru // { isRubyGem = true; }; 225 inherit meta; 226}) 227 228)