about summary refs log tree commit diff
path: root/pkgs/development/ruby-modules/bundled-common/functions.nix
blob: 57a10dadc40082305c663a150c7da0280403bafd (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{ 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}/gems/bundler-${bundler.version}" \
        ${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";
          suffix = version;
          gemType = "path";
        };
    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 []);
  });
}