How to list attributes of a Nix expression

I’m trying to integrate a Nix based project into a CI tool. For that it would be useful to take a release.nix and get a list of all buildable attributes out of it. This should include recursing into everything that has recurseForDerivations set.

So considering this Nix expression in release.nix:

  foo = someDerivation;

  bar = recurseIntoAttrs {
    zoo = someOtherDerivation;
    zop = yetAnotherDerivation;

I want to be able to say:

% list-derivations release.nix

Is there already a tool like this?


nix search is capable of doing this:

nix search --no-cache -f release.nix
1 Like

This is exactly what I was looking for. With --json it even produces machine readable output. \o/

@khumba Is there a way to avoid the ugly error: no results for the given search term(s)! warning when using the --json parameter?

Hey nice find on --json, I wasn’t aware of that. I’m going to have to integrate that into some of my scripts to replace some hand-built listing logic.

It looks to me like you can remove that message by hiding stderr:

nix search ... 2>/dev/null

Mmh. This doesn’t seem to work anymore. The current nix search seems to do something different. There is not even --no-cache anymore.

This changed about 18+ months ago. with the release of nix 2.4.

You either have to stick to 2.3 or adopt to the changes in the experimental interface (which in this case means to also adopt flakes) or get what you need using the non-experimental CLI.