Stream: nix

Topic: Recursively search through flake inputs


view this post on Zulip Shivaraj B H (Jan 03 2024 at 09:49):

I recall @Srid mentioning this before, but I don’t seem to recollect. For example, if I have a flake in project-a and this has project-b in its inputs, project-b in turn depends on project-c in its flake. My question is, how can I find if project-a depends on project-c without having to go through each of the flake. I want something like nix why-depends but for flake inputs.

view this post on Zulip Srid (Jan 03 2024 at 13:23):

Unsure, but there is nix flake metadata which dumps the tree of inputs. It takes --json to produce a JSON output, so something can be built off it. Also nix-browser can (be made to) do this too, but a CLI would be nice.

view this post on Zulip Tim DeHerrera (Jan 07 2024 at 23:34):

fwiw, for quickly traversing inputs in the flake you can simply load the flake in a repl, and a top-level inputs attribute exists for quickly traversing the inputs of a flake

view this post on Zulip Shivaraj B H (Jan 08 2024 at 13:42):

Hey Tim, thanks for that, I am aware that I can traverse through the entire graph of the inputs, i.e if I know the path to whatever input I am looking for. I was looking for a solution, that given an input, gives me the pinned version for that input in the dependency graph and the path to reach to that node in the graph.

This is what worked for me:
To find the pinned revision:

$ nix flake metadata --json | jq '.. | ."hedis"? | select( . != null )'

Which gives me the following output:

{
  "flake": false,
  "locked": {
    "lastModified": 1689234100,
    "narHash": "sha256-J4mqtraMzLJguL0BwMUETeBpNJXRf86E2ZaP9xx+4tY=",
    "owner": "juspay",
    "repo": "hedis",
    "rev": "77bf501da57639a60728b577e1c3780f70ddb418",
    "type": "github"
  },
  "original": {
    "owner": "juspay",
    "repo": "hedis",
    "rev": "77bf501da57639a60728b577e1c3780f70ddb418",
    "type": "github"
  }
}
"hedis"

Now, what if I want to get the path to reach this node, I can still use jq to do this, thanks to this solution from stackoverflow:

nix flake metadata --json | jq -c 'paths | select(.[-1] == "hedis")'

This gives me the output:

["locks","nodes","euler-hs","inputs","hedis"]
["locks","nodes","hedis"]

Now I know hedis comes from euler-hs’s flake.

view this post on Zulip Srid (Jan 08 2024 at 17:12):

Nice one; here's another example (shared-kernel is transitively used by two other flakes, in addition to direct reference by the root flake; though the output doesn't say whether they all are pinned at same rev):

 nix flake metadata --json github:nammayatri/nammayatri | jq -c 'paths | select(.[-1] == "shared-kernel")'
["locks","nodes","beckn-gateway","inputs","shared-kernel"]
["locks","nodes","namma-dsl","inputs","shared-kernel"]
["locks","nodes","root","inputs","shared-kernel"]
["locks","nodes","shared-kernel"]

view this post on Zulip Shivaraj B H (Jan 08 2024 at 17:16):

That shouldn’t be hard to solve, I will have a look tomorrow

view this post on Zulip Srid (Jan 08 2024 at 17:18):

You could wrap all of this in a pkgs.writeShellApplication and make it a flake-app runnable as, for instance, nix run github:shivaraj-bh/search-flake-inputs github:nammayatri/nammayatri shared-kernel

view this post on Zulip Srid (Jan 08 2024 at 17:19):

Probably an overkill, unless it is going to do more / act general.

view this post on Zulip Shivaraj B H (Jan 08 2024 at 17:32):

I will probably come up with a more prettified output for this along with displaying a correspond short rev for the node in question, then making a flake-app around it would make sense

view this post on Zulip Srid (Jan 08 2024 at 17:33):

Stretch goal: can it also search flake output tree?

view this post on Zulip Shivaraj B H (Jan 15 2024 at 14:29):

https://github.com/shivaraj-bh/search-flake-inputs

Screenshot-2024-01-15-at-7.58.02PM.png

view this post on Zulip Srid (Jan 16 2024 at 05:32):

announce it!

view this post on Zulip tgunnoe (Feb 05 2024 at 21:04):

this is really useful. thanks

view this post on Zulip Shivaraj B H (Feb 06 2024 at 03:45):

I was planning to announce it but was wondering if I could make a general purpose search application in rust. It will be able to search outputs, inputs and can be made extensible to search much more.

view this post on Zulip Shivaraj B H (Feb 06 2024 at 03:46):

tgunnoe said:

this is really useful. thanks

Maybe I should announce it at its current state and then later launch a successor that will deprecate this.


Last updated: Nov 15 2024 at 12:33 UTC