Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix/show-config: allow getting the value of a specific setting #7595

Merged
merged 2 commits into from
Jan 23, 2023

Conversation

cole-h
Copy link
Member

@cole-h cole-h commented Jan 12, 2023

Instead of needing to run nix show-config --json | jq -r '."warn-dirty".value' to view the value of warn-dirty, you can now run nix show-config warn-dirty.

Fixes #7586.


I'm sure this isn't the most beautiful solution (and I couldn't figure out a good way to get JSON support for the single-option mode at this point in time), but it works!

Instead of needing to run `nix show-config --json | jq -r
'."warn-dirty".value'` to view the value of `warn-dirty`, you can now
run `nix show-config warn-dirty`.
@cole-h cole-h force-pushed the show-setting-value branch from ba6f8de to 1fc74af Compare January 12, 2023 21:56
Copy link
Member

@thufschmitt thufschmitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I actually had a talk just yesterday about the fact that it would be handy to have something like that :D

Can you add some tests for it? tests/config.sh seems the right place for that

}

Category category() override { return catUtility; }

void run() override
{
if (name) {
if (json) {
throw UsageError("'--json' is not supported when specifying a setting name");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we allow that? Both for consistency and because some settings are themselves a structured value (substituters for example) so it's worth having them already parsed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I apparently didn't see the second half of the PR description 🤦‍♂️

Wouldn't it be enough to just print setting->second.toJSON() in here ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, SettingInfo doesn't have toJSON()... Which is why I left it unsupported 😅

Also, SettingInfo is stringly-typed, so I'm not sure if we can easily get (useful) structured values out of it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, if configRegistrations was somehow exposed, maybe we could go through that, since that appears to have toJSON with proper JSON types (that's how globalConfig.toJSON() is implemented from what I can see).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp, it is exposed, just under globalConfig. Still, I did a bit of playing and it's real ugly, trying to get the proper toJSON stuff. Basically need to be able to do the following:

  1. Iterate through all of the Configs in configRegistrations
  2. Gain access to Config::_settings (which is private) so we can look up the setting by key there
  3. Gain access to AbstractSetting::toJSON() (which is protected)

If there was a general-purpose "C++ value to appropriate JSON value" function somewhere, we could just call that, but I don't know if that exists (all the json files appears to be under libexpr).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need JSON support here, because you can always get a specific flag by doing nix show-config --json | jq .<name>.value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might follow an issue. Whether or not this an important feature (and I readily believe the jq way is good enough), it would be nice if our configuration infrastructure didn't get in the way of this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to see json supported, but my attempt was a bit of a pain, and I was in over my head.

src/nix/show-config.cc Show resolved Hide resolved
@edolstra edolstra dismissed thufschmitt’s stale review January 23, 2023 16:56

Comments addressed.

@edolstra edolstra merged commit f503ba1 into NixOS:master Jan 23, 2023
@cole-h cole-h deleted the show-setting-value branch January 23, 2023 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nix show-config system should print the value of system
4 participants