Skip to content

Commit

Permalink
bundlerEnv: ensure dependencies always included
Browse files Browse the repository at this point in the history
Suppose I have a Gemfile like this:

    source "https://rubygems.org"
    gem "actioncable"
    gem "websocket-driver", group: :test

The gemset.nix generated by Bundix 2.4.1 will set ActionCable's groups
to [ "default" ], and websocket-driver's to [ "test" ]. This means that
the generated bundlerEnv wouldn't include websocket-driver unless the
test group was included, even though it's required by the default group.

This is arguably a bug in Bundix (websocket-driver's groups should
probably be [ "default" "test" ] or just [ "default" ]), but there's no
reason bundlerEnv should omit dependencies even given such an input --
it won't necessarily come from Bundix, and it would be good for
bundlerEnv to do the right thing.

To fix this, filterGemset is now a recursive function, that adds
dependencies of gems in the group to the filtered gemset until it
stabilises on the gems that match the required groups, and all of their
recursive dependencies.
  • Loading branch information
alyssais committed Dec 11, 2018
1 parent ac19d5e commit 67b1265
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/attrsets.nix
Expand Up @@ -94,6 +94,15 @@ rec {
attrValues = builtins.attrValues or (attrs: attrVals (attrNames attrs) attrs);


/* Given a set of attribute names, return the set of the corresponding
attributes from the given set.
Example:
getAttrs [ "a" "b" ] { a = 1; b = 2; c = 3; }
=> { a = 1; b = 2; }
*/
getAttrs = names: attrs: genAttrs names (name: attrs.${name});

/* Collect each attribute named `attr' from a list of attribute
sets. Sets that don't contain the named attribute are ignored.
Expand Down
4 changes: 2 additions & 2 deletions lib/default.nix
Expand Up @@ -61,10 +61,10 @@ let
boolToString mergeAttrs flip mapNullable inNixShell min max
importJSON warn info nixpkgsVersion version mod compare
splitByAndCompare functionArgs setFunctionArgs isFunction;
inherit (fixedPoints) fix fix' extends composeExtensions
inherit (fixedPoints) fix fix' converge extends composeExtensions
makeExtensible makeExtensibleWithCustomName;
inherit (attrsets) attrByPath hasAttrByPath setAttrByPath
getAttrFromPath attrVals attrValues catAttrs filterAttrs
getAttrFromPath attrVals attrValues getAttrs catAttrs filterAttrs
filterAttrsRecursive foldAttrs collect nameValuePair mapAttrs
mapAttrs' mapAttrsToList mapAttrsRecursive mapAttrsRecursiveCond
genAttrs isDerivation toDerivation optionalAttrs
Expand Down
10 changes: 10 additions & 0 deletions lib/fixed-points.nix
Expand Up @@ -24,6 +24,16 @@ rec {
# for a concrete example.
fix' = f: let x = f x // { __unfix__ = f; }; in x;

# Return the fixpoint that `f` converges to when called recursively, starting
# with the input `x`.
#
# nix-repl> converge (x: x / 2) 16
# 0
converge = f: x:
if (f x) == x
then x
else converge f (f x);

# Modify the contents of an explicitly recursive attribute set in a way that
# honors `self`-references. This is accomplished with a function
#
Expand Down
20 changes: 18 additions & 2 deletions pkgs/development/ruby-modules/bundled-common/functions.nix
@@ -1,5 +1,9 @@
{ lib, gemConfig, ... }:
rec {

let
inherit (lib) attrValues concatMap converge filterAttrs getAttrs;

in rec {
bundlerFiles = {
gemfile ? null
, lockfile ? null
Expand All @@ -22,7 +26,19 @@ rec {
else gemset;
};

filterGemset = {ruby, groups,...}: gemset: lib.filterAttrs (name: attrs: platformMatches ruby attrs && groupMatches groups attrs) 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") ||
Expand Down

0 comments on commit 67b1265

Please sign in to comment.