diff options
Diffstat (limited to 'nixpkgs/pkgs/development/ruby-modules/bundled-common')
4 files changed, 349 insertions, 0 deletions
diff --git a/nixpkgs/pkgs/development/ruby-modules/bundled-common/default.nix b/nixpkgs/pkgs/development/ruby-modules/bundled-common/default.nix new file mode 100644 index 000000000000..c31dfb63b3cd --- /dev/null +++ b/nixpkgs/pkgs/development/ruby-modules/bundled-common/default.nix @@ -0,0 +1,158 @@ +{ stdenv, runCommand, ruby, lib +, defaultGemConfig, buildRubyGem, buildEnv +, makeWrapper +, bundler +}@defs: + +{ + name ? null +, pname ? null +, mainGemName ? null +, gemdir ? null +, gemfile ? null +, lockfile ? null +, gemset ? null +, ruby ? defs.ruby +, gemConfig ? defaultGemConfig +, postBuild ? null +, document ? [] +, meta ? {} +, groups ? null +, ignoreCollisions ? false +, buildInputs ? [] +, ... +}@args: + +assert name == null -> pname != null; + +with import ./functions.nix { inherit lib gemConfig; }; + +let + gemFiles = bundlerFiles args; + + importedGemset = if builtins.typeOf gemFiles.gemset != "set" + then import gemFiles.gemset + else gemFiles.gemset; + + filteredGemset = filterGemset { inherit ruby groups; } importedGemset; + + configuredGemset = lib.flip lib.mapAttrs filteredGemset (name: attrs: + applyGemConfigs (attrs // { inherit ruby; gemName = name; }) + ); + + hasBundler = builtins.hasAttr "bundler" filteredGemset; + + bundler = + if hasBundler then gems.bundler + else defs.bundler.override (attrs: { inherit ruby; }); + + gems = lib.flip lib.mapAttrs configuredGemset (name: attrs: buildGem name attrs); + + name' = if name != null then + name + else + let + gem = gems."${pname}"; + version = gem.version; + in + "${pname}-${version}"; + + pname' = if pname != null then + pname + else + name; + + copyIfBundledByPath = { bundledByPath ? false, ...}: + (if bundledByPath then + assert gemFiles.gemdir != null; "cp -a ${gemFiles.gemdir}/* $out/" #*/ + else "" + ); + + maybeCopyAll = pkgname: if pkgname == null then "" else + let + mainGem = gems."${pkgname}" or (throw "bundlerEnv: gem ${pkgname} not found"); + in + copyIfBundledByPath mainGem; + + # We have to normalize the Gemfile.lock, otherwise bundler tries to be + # helpful by doing so at run time, causing executables to immediately bail + # out. Yes, I'm serious. + confFiles = runCommand "gemfile-and-lockfile" {} '' + mkdir -p $out + ${maybeCopyAll mainGemName} + cp ${gemFiles.gemfile} $out/Gemfile || ls -l $out/Gemfile + cp ${gemFiles.lockfile} $out/Gemfile.lock || ls -l $out/Gemfile.lock + ''; + + buildGem = name: attrs: ( + let + gemAttrs = composeGemAttrs ruby gems name attrs; + in + if gemAttrs.type == "path" then + pathDerivation (gemAttrs.source // gemAttrs) + else + buildRubyGem gemAttrs + ); + + envPaths = lib.attrValues gems ++ lib.optional (!hasBundler) bundler; + + basicEnv = buildEnv { + inherit buildInputs ignoreCollisions; + + name = name'; + + paths = envPaths; + pathsToLink = [ "/lib" ]; + + postBuild = genStubsScript (defs // args // { + inherit confFiles bundler groups; + binPaths = envPaths; + }) + lib.optionalString (postBuild != null) postBuild; + + meta = { platforms = ruby.meta.platforms; } // meta; + + passthru = rec { + inherit ruby bundler gems confFiles envPaths; + + wrappedRuby = stdenv.mkDerivation { + name = "wrapped-ruby-${pname'}"; + nativeBuildInputs = [ makeWrapper ]; + buildCommand = '' + mkdir -p $out/bin + for i in ${ruby}/bin/*; do + makeWrapper "$i" $out/bin/$(basename "$i") \ + --set BUNDLE_GEMFILE ${confFiles}/Gemfile \ + --set BUNDLE_PATH ${basicEnv}/${ruby.gemPath} \ + --set BUNDLE_FROZEN 1 \ + --set GEM_HOME ${basicEnv}/${ruby.gemPath} \ + --set GEM_PATH ${basicEnv}/${ruby.gemPath} + done + ''; + }; + + env = let + irbrc = builtins.toFile "irbrc" '' + if !(ENV["OLD_IRBRC"].nil? || ENV["OLD_IRBRC"].empty?) + require ENV["OLD_IRBRC"] + end + require 'rubygems' + require 'bundler/setup' + ''; + in stdenv.mkDerivation { + name = "${pname'}-interactive-environment"; + nativeBuildInputs = [ wrappedRuby basicEnv ]; + shellHook = '' + 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 + ''; + }; + }; + }; +in + basicEnv diff --git a/nixpkgs/pkgs/development/ruby-modules/bundled-common/functions.nix b/nixpkgs/pkgs/development/ruby-modules/bundled-common/functions.nix new file mode 100644 index 000000000000..6324f27e9faf --- /dev/null +++ b/nixpkgs/pkgs/development/ruby-modules/bundled-common/functions.nix @@ -0,0 +1,93 @@ +{ lib, gemConfig, ... }: + +let + inherit (lib) attrValues concatMap converge filterAttrs getAttrs + intersectLists; + +in rec { + bundlerFiles = { + gemfile ? null + , lockfile ? null + , gemset ? null + , gemdir ? null + , ... + }: { + inherit gemdir; + + gemfile = + if gemfile == null then assert gemdir != null; gemdir + "/Gemfile" + else gemfile; + + lockfile = + if lockfile == null then assert gemdir != null; gemdir + "/Gemfile.lock" + else lockfile; + + gemset = + if gemset == null then assert gemdir != null; gemdir + "/gemset.nix" + else gemset; + }; + + filterGemset = { ruby, groups, ... }: gemset: + let + platformGems = filterAttrs (_: platformMatches ruby) gemset; + directlyMatchingGems = filterAttrs (_: groupMatches groups) platformGems; + + expandDependencies = gems: + let + depNames = concatMap (gem: gem.dependencies or []) (attrValues gems); + deps = getAttrs depNames platformGems; + in + gems // deps; + in + converge expandDependencies directlyMatchingGems; + + platformMatches = {rubyEngine, version, ...}: attrs: ( + !(attrs ? "platforms") || + builtins.length attrs.platforms == 0 || + builtins.any (platform: + platform.engine == rubyEngine && + (!(platform ? "version") || platform.version == version.majMin) + ) attrs.platforms + ); + + groupMatches = groups: attrs: + groups == null || !(attrs ? "groups") || + (intersectLists (groups ++ [ "default" ]) attrs.groups) != []; + + applyGemConfigs = attrs: + (if gemConfig ? "${attrs.gemName}" + then attrs // gemConfig."${attrs.gemName}" attrs + else attrs); + + genStubsScript = { lib, ruby, confFiles, bundler, groups, binPaths, ... }: '' + ${ruby}/bin/ruby ${./gen-bin-stubs.rb} \ + "${ruby}/bin/ruby" \ + "${confFiles}/Gemfile" \ + "$out/${ruby.gemPath}" \ + "${bundler}/${ruby.gemPath}" \ + ${lib.escapeShellArg binPaths} \ + ${lib.escapeShellArg groups} + ''; + + pathDerivation = { gemName, version, path, ... }: + let + res = { + type = "derivation"; + bundledByPath = true; + name = gemName; + version = version; + outPath = path; + outputs = [ "out" ]; + out = res; + outputName = "out"; + }; + in res; + + composeGemAttrs = ruby: gems: name: attrs: ((removeAttrs attrs ["platforms"]) // { + inherit ruby; + inherit (attrs.source) type; + source = removeAttrs attrs.source ["type"]; + gemName = name; + gemPath = map (gemName: gems."${gemName}") (attrs.dependencies or []); + }); +} diff --git a/nixpkgs/pkgs/development/ruby-modules/bundled-common/gen-bin-stubs.rb b/nixpkgs/pkgs/development/ruby-modules/bundled-common/gen-bin-stubs.rb new file mode 100644 index 000000000000..fe8c43f55ed1 --- /dev/null +++ b/nixpkgs/pkgs/development/ruby-modules/bundled-common/gen-bin-stubs.rb @@ -0,0 +1,48 @@ +require 'rbconfig' +require 'rubygems' +require 'rubygems/specification' +require 'fileutils' + +# args/settings +out = ENV["out"] +ruby = ARGV[0] +gemfile = ARGV[1] +bundle_path = ARGV[2] +bundler_gem_path = ARGV[3] +paths = ARGV[4].split +groups = ARGV[5].split + +# generate binstubs +FileUtils.mkdir_p("#{out}/bin") +paths.each do |path| + next unless File.directory?("#{path}/nix-support/gem-meta") + + name = File.read("#{path}/nix-support/gem-meta/name") + executables = File.read("#{path}/nix-support/gem-meta/executables") + .force_encoding('UTF-8').split + executables.each do |exe| + File.open("#{out}/bin/#{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. +# + +ENV["BUNDLE_GEMFILE"] = #{gemfile.dump} +ENV["BUNDLE_PATH"] = #{bundle_path.dump} +ENV['BUNDLE_FROZEN'] = '1' + +Gem.use_paths(#{bundler_gem_path.dump}, ENV["GEM_PATH"]) + +require 'bundler' +Bundler.setup(#{groups.map(&:dump).join(', ')}) + +load Gem.bin_path(#{name.dump}, #{exe.dump}) +EOF + FileUtils.chmod("+x", "#{out}/bin/#{exe}") + end + end +end diff --git a/nixpkgs/pkgs/development/ruby-modules/bundled-common/test.nix b/nixpkgs/pkgs/development/ruby-modules/bundled-common/test.nix new file mode 100644 index 000000000000..7c5932c4e761 --- /dev/null +++ b/nixpkgs/pkgs/development/ruby-modules/bundled-common/test.nix @@ -0,0 +1,50 @@ +{ stdenv, writeText, lib, ruby, defaultGemConfig, callPackage, test, stubs, should }: +let + testConfigs = { + inherit lib; + gemConfig = defaultGemConfig; + }; + functions = (import ./functions.nix testConfigs); +in + builtins.concatLists [ + ( test.run "All set, no gemdir" (functions.bundlerFiles { + gemfile = test/Gemfile; + lockfile = test/Gemfile.lock; + gemset = test/gemset.nix; + }) { + gemfile = should.equal test/Gemfile; + lockfile = should.equal test/Gemfile.lock; + gemset = should.equal test/gemset.nix; + }) + + ( test.run "Just gemdir" (functions.bundlerFiles { + gemdir = test/.; + }) { + gemfile = should.equal test/Gemfile; + lockfile = should.equal test/Gemfile.lock; + gemset = should.equal test/gemset.nix; + }) + + ( test.run "Gemset and dir" (functions.bundlerFiles { + gemdir = test/.; + gemset = test/extraGemset.nix; + }) { + gemfile = should.equal test/Gemfile; + lockfile = should.equal test/Gemfile.lock; + gemset = should.equal test/extraGemset.nix; + }) + + ( test.run "Filter empty gemset" {} (set: functions.filterGemset {inherit ruby; groups = ["default"]; } set == {})) + ( let gemSet = { test = { groups = ["x" "y"]; }; }; + in + test.run "Filter matches a group" gemSet (set: functions.filterGemset {inherit ruby; groups = ["y" "z"];} set == gemSet)) + ( let gemSet = { test = { platforms = []; }; }; + in + test.run "Filter matches empty platforms list" gemSet (set: functions.filterGemset {inherit ruby; groups = [];} set == gemSet)) + ( let gemSet = { test = { platforms = [{engine = ruby.rubyEngine; version = ruby.version.majMin;}]; }; }; + in + test.run "Filter matches on platform" gemSet (set: functions.filterGemset {inherit ruby; groups = [];} set == gemSet)) + ( let gemSet = { test = { groups = ["x" "y"]; }; }; + in + test.run "Filter excludes based on groups" gemSet (set: functions.filterGemset {inherit ruby; groups = ["a" "b"];} set == {})) + ] |