summary refs log tree commit diff
path: root/pkgs/development/interpreters/ruby/load-ruby-env.nix
blob: c4356ed5f50f67de636f232a2543b06e19530d70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{ ruby, lib, callPackage, gemFixes, fetchurl, fetchgit, buildRubyGem }@defs:

# This function builds a set of gems. You first convert your Gemfile to an attrset
# called a "gemset", and then use this function to build the gemset.
#
# A gemset looks like the following:
#
#   {
#     libv8 = {
#       version = "3.16.14.7";
#       src = {
#         type = "gem";
#         sha256 = "...";
#       };
#     };
#     therubyracer = {
#       version = "0.12.1";
#       dependencies = [ "libv8" ];
#       src = {
#         type = "gem";
#         sha256 = "...";
#       };
#     };
#   }
#
# If you use these gems as build inputs, the GEM_PATH will be updated
# appropriately, and command like `bundle exec` should work out of the box.

{ gemset, ruby ? defs.ruby, fixes ? gemFixes }@args:

let
  const = x: y: x;

  fetchers.path = attrs: attrs.src.path;
  fetchers.gem = attrs: fetchurl {
    url = "${attrs.src.source or "https://rubygems.org"}/downloads/${attrs.name}-${attrs.version}.gem";
    inherit (attrs.src) sha256;
  };
  fetchers.git = attrs: fetchgit {
    inherit (attrs.src) url rev sha256 fetchSubmodules;
    leaveDotGit = true;
  };

  instantiate = (attrs:
    let
      defaultAttrs = {
        name = "${attrs.name}-${attrs.version}";
        inherit ruby gemPath;
      };
      gemPath = map (name: gemset''."${name}") (attrs.dependencies or []);
      fixedAttrs = attrs // (fixes."${attrs.name}" or (const {})) attrs;
      withSource = fixedAttrs //
        (if (lib.isDerivation fixedAttrs.src || builtins.isString fixedAttrs.src)
           then {}
           else { src = (fetchers."${fixedAttrs.src.type}" fixedAttrs); });

    in
      buildRubyGem (withSource // defaultAttrs)
  );

  gemset' = if builtins.isAttrs gemset then gemset else import gemset;

  gemset'' = lib.flip lib.mapAttrs gemset' (name: attrs:
    if (lib.isDerivation attrs)
      then attrs
      else instantiate (attrs // { inherit name; })
  );

in gemset''