Skip to content

Instantly share code, notes, and snippets.

@GuillaumeDesforges
Last active October 6, 2022 12:55
Show Gist options
  • Save GuillaumeDesforges/39b44fdc72ff0c5b228cce1458c23c76 to your computer and use it in GitHub Desktop.
Save GuillaumeDesforges/39b44fdc72ff0c5b228cce1458c23c76 to your computer and use it in GitHub Desktop.
# usage:
# NIXPKGS_FLAKE_REF="github:nixos/nixpkgs/master" nix eval --json --file "./nixpkgs-graph.nix"
let
nixpkgsFlakeRef = builtins.getEnv "NIXPKGS_FLAKE_REF";
pkgs = import (builtins.getFlake nixpkgsFlakeRef) { };
in
with pkgs.lib;
let
recurse =
parentPath: name: value:
let
path = (if parentPath == "" then "" else parentPath + ".") + name;
valueEvalResult = builtins.tryEval value;
in
if valueEvalResult.success then
let okValue = valueEvalResult.value;
in
if isDerivation okValue then
{
inherit path;
# can't name it `outPath` because serialization would only output it instead of dict
# see Nix `toString` docs
outputPath =
let
pEvalResult = builtins.tryEval (toString okValue);
in
if pEvalResult.success then pEvalResult.value
else null;
buildInputs =
map
(p:
let
pEvalResult = builtins.tryEval (toString p);
in
if pEvalResult.success then pEvalResult.value
else null
)
(okValue.buildInputs or [ ]);
}
else
if isAttrs okValue && (okValue.recurseForDerivations or false) then
mapAttrs (recurse path) okValue
else null
else null;
in
(collect
(x: x ? outputPath)
(mapAttrs (recurse "") pkgs)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment