Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at 20.03 6.8 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 ? false 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 } 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 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 elif [[ "$type" == "git" ]]; then 146 git init 147 # remove variations to improve the likelihood of a bit-reproducible output 148 rm -rf .git/logs/ .git/hooks/ .git/index .git/FETCH_HEAD .git/ORIG_HEAD .git/refs/remotes/origin/HEAD .git/config 149 # support `git ls-files` 150 git add . 151 fi 152 153 runHook postBuild 154 ''; 155 156 # Note: 157 # We really do need to keep the $out/${ruby.gemPath}/cache. 158 # This is very important in order for many parts of RubyGems/Bundler to not blow up. 159 # See https://github.com/bundler/bundler/issues/3327 160 installPhase = attrs.installPhase or '' 161 runHook preInstall 162 163 export GEM_HOME=$out/${ruby.gemPath} 164 mkdir -p $GEM_HOME 165 166 echo "buildFlags: $buildFlags" 167 168 ${lib.optionalString (type == "url") '' 169 ruby ${./nix-bundle-install.rb} \ 170 "path" \ 171 '${gemName}' \ 172 '${version}' \ 173 '${lib.escapeShellArgs buildFlags}' 174 ''} 175 ${lib.optionalString (type == "git") '' 176 ruby ${./nix-bundle-install.rb} \ 177 "git" \ 178 '${gemName}' \ 179 '${version}' \ 180 '${lib.escapeShellArgs buildFlags}' \ 181 '${attrs.source.url}' \ 182 '.' \ 183 '${attrs.source.rev}' 184 ''} 185 186 ${lib.optionalString (type == "gem") '' 187 if [[ -z "$gempkg" ]]; then 188 echo "failure: \$gempkg path unspecified" 1>&2 189 exit 1 190 elif [[ ! -f "$gempkg" ]]; then 191 echo "failure: \$gempkg path invalid" 1>&2 192 exit 1 193 fi 194 195 gem install \ 196 --local \ 197 --force \ 198 --http-proxy 'http://nodtd.invalid' \ 199 --ignore-dependencies \ 200 --install-dir "$GEM_HOME" \ 201 --build-root '/' \ 202 --backtrace \ 203 --no-env-shebang \ 204 ${documentFlag} \ 205 $gempkg $gemFlags -- $buildFlags 206 207 # looks like useless files which break build repeatability and consume space 208 pushd $out/${ruby.gemPath} 209 rm -fv doc/*/*/created.rid || true 210 rm -fv {gems/*/ext/*,extensions/*/*/*}/{mkmf.log,gem_make.out} || true 211 rm -fvr cache 212 popd 213 214 # write out metadata and binstubs 215 spec=$(echo $out/${ruby.gemPath}/specifications/*.gemspec) 216 ruby ${./gem-post-build.rb} "$spec" 217 ''} 218 219 ${lib.optionalString (!dontInstallManpages) '' 220 for section in {1..9}; do 221 mandir="$out/share/man/man$section" 222 find $out/lib \( -wholename "*/man/*.$section" -o -wholename "*/man/man$section/*.$section" \) \ 223 -execdir mkdir -p $mandir \; -execdir cp '{}' $mandir \; 224 done 225 ''} 226 227 runHook postInstall 228 ''; 229 230 propagatedBuildInputs = gemPath ++ propagatedBuildInputs; 231 propagatedUserEnvPkgs = gemPath ++ propagatedUserEnvPkgs; 232 233 passthru = passthru // { isRubyGem = true; }; 234 inherit meta; 235}) 236 237)