|
| 1 | +# A cycle is when packages depend on each other |
| 2 | +# The Nix store can't contain direct cycles, so cycles need special handling |
| 3 | +# They can be avoided by referencing by name, so the consumer sets up the cycle |
| 4 | +# internally, or by co-locating cycling packages in a single store path. |
| 5 | +# Both approaches are valid, it depends on the situation what fits better. |
| 6 | +# |
| 7 | +# The below code detects cycles by visiting all edges of the dependency graph |
| 8 | +# and keeping track of parents and already-visited nodes. Then it picks a head |
| 9 | +# for each cycle, and the other members are referred to as cyclees. |
| 10 | +# The head is the member with the shortest name, since that often results in a |
| 11 | +# head that "feels right". |
| 12 | +# |
| 13 | +# The visits are tracked by maintaining state in the accumulator during folding. |
| 14 | +{ |
| 15 | + lib, |
| 16 | + dependencyGraph, |
| 17 | +}: let |
| 18 | + b = builtins; |
| 19 | + |
| 20 | + # The separator char should never be in version |
| 21 | + mkTag = pkg: "${pkg.name}#${pkg.version}"; |
| 22 | + trueAttr = tag: lib.listToAttrs [(lib.nameValuePair tag true)]; |
| 23 | + |
| 24 | + # discover cycles as sets with their members=true |
| 25 | + # a member is pkgname#pkgversion (# should not be in version string) |
| 26 | + # this walks dependencies depth-first |
| 27 | + # It will eventually see parents as children => cycle |
| 28 | + # |
| 29 | + # To visit only new nodes, we pass around state in parentAcc: |
| 30 | + # - visited: a set of already-visited packages |
| 31 | + # - cycles: a list of cycle sets |
| 32 | + getCycles = pkg: parents: parentAcc: let |
| 33 | + deps = dependencyGraph."${pkg.name}"."${pkg.version}"; |
| 34 | + pkgTag = mkTag pkg; |
| 35 | + pkgTrue = trueAttr pkgTag; |
| 36 | + |
| 37 | + visitOne = acc: dep: let |
| 38 | + depTag = mkTag dep; |
| 39 | + depTrue = trueAttr depTag; |
| 40 | + in |
| 41 | + if acc.visited ? "${depTag}" |
| 42 | + then |
| 43 | + # We will already have found all cycles it has, skip |
| 44 | + acc |
| 45 | + else if parents ? "${depTag}" |
| 46 | + then |
| 47 | + # We found a cycle |
| 48 | + { |
| 49 | + visited = acc.visited; |
| 50 | + cycles = acc.cycles ++ [(pkgTrue // depTrue)]; |
| 51 | + } |
| 52 | + else |
| 53 | + # We need to check this dep |
| 54 | + # Don't add pkg to visited until all deps are processed |
| 55 | + getCycles dep (parents // pkgTrue) acc; |
| 56 | + initialAcc = { |
| 57 | + visited = parentAcc.visited; |
| 58 | + cycles = []; |
| 59 | + }; |
| 60 | + |
| 61 | + allVisited = b.foldl' visitOne initialAcc deps; |
| 62 | + in |
| 63 | + if parentAcc.visited ? "${pkgTag}" |
| 64 | + then |
| 65 | + # this can happen while walking the root nodes |
| 66 | + parentAcc |
| 67 | + else { |
| 68 | + visited = allVisited.visited // pkgTrue; |
| 69 | + cycles = |
| 70 | + if b.length allVisited.cycles != 0 |
| 71 | + then mergeCycles parentAcc.cycles allVisited.cycles |
| 72 | + else parentAcc.cycles; |
| 73 | + }; |
| 74 | + |
| 75 | + # merge cycles: We want a set of disjoined cycles |
| 76 | + # meaning, for each cycle of the set e.g. {a=true; b=true; c=true;...}, |
| 77 | + # there is no other cycle that has any member (a,b,c,...) of this set |
| 78 | + # We maintain a set of already disjoint cycles and add a new cycle |
| 79 | + # by merging all cycles of the set that have members in common with |
| 80 | + # the cycle. The rest stays disjoint. |
| 81 | + mergeCycles = b.foldl' mergeOneCycle; |
| 82 | + mergeOneCycle = djCycles: cycle: let |
| 83 | + cycleDeps = b.attrNames cycle; |
| 84 | + includesDep = s: lib.any (n: s ? "${n}") cycleDeps; |
| 85 | + partitions = lib.partition includesDep djCycles; |
| 86 | + mergedCycle = |
| 87 | + if b.length partitions.right != 0 |
| 88 | + then b.zipAttrsWith (n: v: true) ([cycle] ++ partitions.right) |
| 89 | + else cycle; |
| 90 | + disjoined = [mergedCycle] ++ partitions.wrong; |
| 91 | + in |
| 92 | + disjoined; |
| 93 | + |
| 94 | + # Walk all root nodes of the dependency graph |
| 95 | + allCycles = let |
| 96 | + mkHandleVersion = name: acc: version: |
| 97 | + getCycles {inherit name version;} {} acc; |
| 98 | + handleName = acc: name: let |
| 99 | + pkgVersions = b.attrNames dependencyGraph.${name}; |
| 100 | + handleVersion = mkHandleVersion name; |
| 101 | + in |
| 102 | + b.foldl' handleVersion acc pkgVersions; |
| 103 | + |
| 104 | + initalAcc = { |
| 105 | + visited = {}; |
| 106 | + cycles = []; |
| 107 | + }; |
| 108 | + rootNames = b.attrNames dependencyGraph; |
| 109 | + |
| 110 | + allDone = b.foldl' handleName initalAcc rootNames; |
| 111 | + in |
| 112 | + allDone.cycles; |
| 113 | + |
| 114 | + # Convert list of cycle sets to set of cycle lists |
| 115 | + getCycleSets = cycles: b.foldl' lib.recursiveUpdate {} (b.map getCycleSetEntry cycles); |
| 116 | + getCycleSetEntry = cycle: let |
| 117 | + split = b.map toNameVersion (b.attrNames cycle); |
| 118 | + toNameVersion = d: let |
| 119 | + matches = b.match "^(.*)#([^#]*)$" d; |
| 120 | + name = b.elemAt matches 0; |
| 121 | + version = b.elemAt matches 1; |
| 122 | + in {inherit name version;}; |
| 123 | + sorted = |
| 124 | + b.sort |
| 125 | + (x: y: let |
| 126 | + lenX = b.stringLength x.name; |
| 127 | + lenY = b.stringLength y.name; |
| 128 | + in |
| 129 | + if lenX < lenY |
| 130 | + then true |
| 131 | + else if lenX == lenY |
| 132 | + then |
| 133 | + if x.name < y.name |
| 134 | + then true |
| 135 | + else if x.name == y.name |
| 136 | + then x.version > y.version |
| 137 | + else false |
| 138 | + else false) |
| 139 | + split; |
| 140 | + head = b.elemAt sorted 0; |
| 141 | + cyclees = lib.drop 1 sorted; |
| 142 | + in {${head.name}.${head.version} = cyclees;}; |
| 143 | + |
| 144 | + cyclicDependencies = getCycleSets allCycles; |
| 145 | +in |
| 146 | + cyclicDependencies |
0 commit comments