├── LICENSE ├── README.md ├── deps.sh └── nix-dependency-treemap.html /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Laura 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dependency graph treemap visualization! :) 2 | 3 | Shows disk space used per dependency. 4 | 5 | ![image](https://github.com/user-attachments/assets/0d931c01-df08-405a-b7ef-5485df7d2901) 6 | 7 | ## Instructions 8 | 9 | 1. Clone repo 10 | 2. Run the bash script on a derivation, eg `./deps.sh /nix/store/z49g31nnd5hkf2di6gp693mlsp23xdgh-python3.10-remarks-0.3.10` 11 | 3. Open the html file 12 | 4. Open the generated JSON file 13 | 14 | ### Get derivation path for a flake build 15 | 16 | If you have a flake that builds a package, you can get the path with `nix build .#my-derivation --no-link --print-out-paths` 17 | 18 | ### What the heck is a treemap? 19 | 20 | > Introduced by Ben Shneiderman in 1991, a treemap recursively subdivides area into rectangles according to each node’s associated value. D3’s treemap implementation supports an extensible tiling method: the default squarified method seeks to generate rectangles with a golden aspect ratio; this offers better readability and size estimation than slice-and-dice, which simply alternates between horizontal and vertical subdivision by depth. 21 | 22 | [source](https://d3js.org/d3-hierarchy/treemap) 23 | -------------------------------------------------------------------------------- /deps.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Save as generate-deps-graph.sh 3 | 4 | # Get the package path 5 | PACKAGE_PATH="$1" 6 | OUTPUT_FILE="${2:-dependency-graph.json}" 7 | 8 | echo "Analyzing dependencies for $PACKAGE_PATH..." 9 | 10 | # Create a temporary directory for our work 11 | TEMP_DIR=$(mktemp -d) 12 | DEPS_LIST="$TEMP_DIR/deps-list.txt" 13 | SIZES_LIST="$TEMP_DIR/sizes-list.txt" 14 | REFS_DIR="$TEMP_DIR/refs" 15 | 16 | mkdir -p "$REFS_DIR" 17 | 18 | # Get all runtime dependencies 19 | echo "Collecting dependencies..." 20 | nix-store -q --requisites "$PACKAGE_PATH" > "$DEPS_LIST" 21 | 22 | # Get sizes for all dependencies 23 | echo "Collecting sizes..." 24 | cat "$DEPS_LIST" | xargs nix path-info -s > "$SIZES_LIST" 25 | 26 | # For each dependency, get its direct references 27 | echo "Building dependency tree..." 28 | while read -r dep; do 29 | nix-store -q --references "$dep" > "$REFS_DIR/$(basename "$dep")" 30 | done < "$DEPS_LIST" 31 | 32 | # Now build the JSON 33 | echo "Generating JSON..." 34 | echo "{" > "$OUTPUT_FILE" 35 | echo " \"name\": \"$(basename "$PACKAGE_PATH")\"," >> "$OUTPUT_FILE" 36 | echo " \"path\": \"$PACKAGE_PATH\"," >> "$OUTPUT_FILE" 37 | 38 | # Get the size of the root package 39 | ROOT_SIZE=$(grep "$PACKAGE_PATH" "$SIZES_LIST" | awk '{print $2}') 40 | echo " \"size\": $ROOT_SIZE," >> "$OUTPUT_FILE" 41 | echo " \"children\": [" >> "$OUTPUT_FILE" 42 | 43 | # Process all direct dependencies of the root 44 | DIRECT_DEPS=$(cat "$REFS_DIR/$(basename "$PACKAGE_PATH")") 45 | FIRST=true 46 | 47 | process_dependencies() { 48 | local parent="$1" 49 | local indent="$2" 50 | local direct_deps=$(cat "$REFS_DIR/$(basename "$parent")" 2>/dev/null || echo "") 51 | 52 | local first_child=true 53 | for dep in $direct_deps; do 54 | # Skip if this dep is not in our requisites list (it might be a runtime dep) 55 | if ! grep -q "$dep" "$DEPS_LIST"; then 56 | continue 57 | fi 58 | 59 | # Check if we've already processed this dep to avoid cycles 60 | if [[ "$processed_deps" == *"$dep"* ]]; then 61 | continue 62 | fi 63 | processed_deps="$processed_deps $dep" 64 | 65 | # Get size 66 | local size=$(grep "$dep" "$SIZES_LIST" | awk '{print $2}') 67 | 68 | if [[ "$first_child" != "true" ]]; then 69 | echo "," >> "$OUTPUT_FILE" 70 | fi 71 | first_child=false 72 | 73 | echo "$indent{" >> "$OUTPUT_FILE" 74 | echo "$indent \"name\": \"$(basename "$dep")\"," >> "$OUTPUT_FILE" 75 | echo "$indent \"path\": \"$dep\"," >> "$OUTPUT_FILE" 76 | echo "$indent \"size\": $size," >> "$OUTPUT_FILE" 77 | 78 | # Check if this dep has children 79 | if [[ -f "$REFS_DIR/$(basename "$dep")" ]] && [[ -s "$REFS_DIR/$(basename "$dep")" ]]; then 80 | echo "$indent \"children\": [" >> "$OUTPUT_FILE" 81 | process_dependencies "$dep" "$indent " 82 | echo "$indent ]" >> "$OUTPUT_FILE" 83 | else 84 | echo "$indent \"children\": []" >> "$OUTPUT_FILE" 85 | fi 86 | 87 | echo -n "$indent}" >> "$OUTPUT_FILE" 88 | done 89 | } 90 | 91 | # Track processed deps to avoid cycles 92 | processed_deps="" 93 | 94 | process_dependencies "$PACKAGE_PATH" " " 95 | 96 | echo "" >> "$OUTPUT_FILE" 97 | echo " ]" >> "$OUTPUT_FILE" 98 | echo "}" >> "$OUTPUT_FILE" 99 | 100 | echo "Dependency graph written to $OUTPUT_FILE" 101 | echo "Cleaning up temporary files..." 102 | rm -rf "$TEMP_DIR" 103 | -------------------------------------------------------------------------------- /nix-dependency-treemap.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Nix Dependency Treemap 7 | 8 | 159 | 160 | 161 |
162 |

Nix Dependency Treemap Visualizer

163 | 164 |
165 |

Drag & drop your Nix dependency JSON file here

166 |

or

167 | 168 | 169 |
170 | 171 | 192 |
193 | 194 | 529 | 530 | --------------------------------------------------------------------------------