# Obtaining a Nixpkgs module system configuration modules graph

**URL:** https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286
**Category:** Development
**Created:** [April 20, 2025, 10:49am UTC](https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286 "2025-04-20T10:49:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mightyiam](https://discourse.nixosstag.fcio.net/user_avatar/discourse.nixosstag.fcio.net/mightyiam/32/9844_2.png) [@mightyiam](https://discourse.nixosstag.fcio.net/u/mightyiam)
#### Post date: [April 20, 2025, 10:49am UTC](https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286/1 "2025-04-20T10:49:18Z")

</div>

I would like to be able to obtain a graph data structure representing the modules that were part of a Nixpkgs module system configuration evaluation.

I couldn’t find a way to achieve this. Did I miss something? Otherwise, would this be a desirable addition?

---

<div class="post-metadata">

### Author: ![roberth](https://discourse.nixosstag.fcio.net/user_avatar/discourse.nixosstag.fcio.net/roberth/32/5488_2.png) [@roberth](https://discourse.nixosstag.fcio.net/u/roberth)
#### Post date: [April 20, 2025, 12:12pm UTC](https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286/2 "2025-04-20T12:12:11Z")

</div>

This isn’t part of the module system interface yet. I suppose it could be?

`collectModules` is a private function which is responsible for finding all the modules, so it could be changed to return them not as a list of a module but something richer that can be preserved and returned by `evalModules`.  
Note that the first thing it does (or “last” in terms of data) is `map (attrs: attrs.module)`. Moving that responsibility to `evalModules` would make it possible to access some of those internals.

I’d want to make sure that memory usage doesn’t regress, but the implementation of lexical closures in the evaluator is rather naive, so I wouldn’t be surprised if it doesn’t make a difference anyway. Probably more important is that we don’t expose more internals publicly on a wholesale basis because that will make it harder to evolve the system. That should be a matter of doing a bit of `map`/`mapAttrs` `(v: { inherit foo v; })` in this new `evalModules` output attribute.

---

<div class="post-metadata">

### Author: ![mightyiam](https://discourse.nixosstag.fcio.net/user_avatar/discourse.nixosstag.fcio.net/mightyiam/32/9844_2.png) [@mightyiam](https://discourse.nixosstag.fcio.net/u/mightyiam)
#### Post date: [May 10, 2025, 8:56am UTC](https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286/3 "2025-05-10T08:56:45Z")

</div>

Been working on this on a schedule of a few hours per week with @A-jay98.

---

<div class="post-metadata">

### Author: ![A-jay98](https://discourse.nixosstag.fcio.net/user_avatar/discourse.nixosstag.fcio.net/a-jay98/32/30791_2.png) [@A-jay98](https://discourse.nixosstag.fcio.net/u/A-jay98)
#### Post date: [May 31, 2025, 8:16am UTC](https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286/4 "2025-05-31T08:16:39Z")

</div>

Hi, we have made some progress on this and could use some input before spending the time to write more extensive tests and documentations. Here is the [Draft PR](https://github.com/NixOS/nixpkgs/pull/403839) for it that adds a `graph` attribute to the output of `evalModules`.

We would appreciate any feedback.  
Thanks!

---

<div class="post-metadata">

### Author: ![mightyiam](https://discourse.nixosstag.fcio.net/user_avatar/discourse.nixosstag.fcio.net/mightyiam/32/9844_2.png) [@mightyiam](https://discourse.nixosstag.fcio.net/u/mightyiam)
#### Post date: [June 7, 2025, 7:55am UTC](https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286/5 "2025-06-07T07:55:44Z")

</div>

Ready for review:

> <https://github.com/NixOS/nixpkgs/pull/403839>
>
> For various module evaluation design/refactor analysis purposes one may want to …obtain a data structure that represents all the modules that took part of a configuration evaluation.
> 
> This PR adds an attribute to the return value of \`lib.evalModules\` with such a data structure.
> 
> Initially discussed \[here\](https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286?u=mightyiam).
> 
> 
> 
> \## Things done
> 
> 
> 
> \- Built on platform(s)
> - \[\] x86\_64-linux
> - \[\] aarch64-linux
> - \[\] x86\_64-darwin
> - \[\] aarch64-darwin
> \- For non-Linux: Is sandboxing enabled in \`nix.conf\`? (See \[Nix manual\](https://nixos.org/manual/nix/stable/command-ref/conf-file.html))
> - \[\] \`sandbox = relaxed\`
> - \[\] \`sandbox = true\`
> \- \[x\] Tested, as applicable:
> - \[NixOS test(s)\](https://nixos.org/manual/nixos/unstable/index.html#sec-nixos-tests) (look inside \[nixos/tests\](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests))
> - and/or \[package tests\](https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md#package-tests)
> - or, for functions and "core" functionality, tests in \[lib/tests\](https://github.com/NixOS/nixpkgs/blob/master/lib/tests) or \[pkgs/test\](https://github.com/NixOS/nixpkgs/blob/master/pkgs/test)
> - made sure NixOS tests are \[linked\](https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md#linking-nixos-module-tests-to-a-package) to the relevant packages
> \- \[\] Tested compilation of all packages that depend on this change using \`nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"\`. Note: all changes have to be committed, also see \[nixpkgs-review usage\](https://github.com/Mic92/nixpkgs-review#usage)
> \- \[\] Tested basic functionality of all binary files (usually in \`./result/bin/\`)
> \- \[25.05 Release Notes\](https://github.com/NixOS/nixpkgs/blob/master/nixos/doc/manual/release-notes/rl-2505.section.md) (or backporting \[24.11\](https://github.com/NixOS/nixpkgs/blob/master/nixos/doc/manual/release-notes/rl-2411.section.md) and \[25.05\](https://github.com/NixOS/nixpkgs/blob/master/nixos/doc/manual/release-notes/rl-2505.section.md) Release notes)
> - \[\] (Package updates) Added a release notes entry if the change is major or breaking
> - \[\] (Module updates) Added a release notes entry if the change is significant
> - \[\] (Module addition) Added a release notes entry if adding a new NixOS module
> \- \[x\] Fits \[CONTRIBUTING.md\](https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md).
> 
> 
> 
> Co-authored-by: @A-jay98
> 
> \---
> 
> Add a :+1: \[reaction\] to \[pull requests you find important\].
> 
> \[reaction\]: https://github.blog/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/
> \[pull requests you find important\]: https://github.com/NixOS/nixpkgs/pulls?q=is%3Aopen+sort%3Areactions-%2B1-desc

---

<div class="post-metadata">

### Author: ![mightyiam](https://discourse.nixosstag.fcio.net/user_avatar/discourse.nixosstag.fcio.net/mightyiam/32/9844_2.png) [@mightyiam](https://discourse.nixosstag.fcio.net/u/mightyiam)
#### Post date: [August 7, 2025, 4:36pm UTC](https://discourse.nixosstag.fcio.net/t/obtaining-a-nixpkgs-module-system-configuration-modules-graph/63286/6 "2025-08-07T16:36:05Z")

</div>

It has been merged!

> [@Nixpkgs module system config modules graph](https://discourse.nixosstag.fcio.net/t/nixpkgs-module-system-config-modules-graph/67722):
>
> We can now trivially obtain a Nix data structure that is a tree of the modules that took part in the evaluation of a configuration evergreen_tree Try it yourself! dancer First, make sure of course that your Nixpkgs checkout includes [the merged PR](https://github.com/NixOS/nixpkgs/pull/403839) ([which branches it landed in](https://nixpkgs-tracker.ocfox.me/?pr=403839)). Now, simply do something like $ nix eval --json '.#nixosConfigurations.termitomyces.graph' \> graph.json Or pipe it to a fancy TUI json explorer: $ nix eval --json '.#nixosConfigurations.termitomyces.graph' | nix ru…
