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

lib.options.filterOptions #295337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mightyiam
Copy link
Member

Co-authored-by: Samyak S Sarnayak samyak201@gmail.com

Description of changes

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)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • 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
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 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
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: module system About "NixOS" module system internals 6.topic: lib The Nixpkgs function library labels Mar 12, 2024
@mightyiam mightyiam marked this pull request as ready for review March 12, 2024 14:54
@mightyiam mightyiam changed the base branch from nixpkgs-unstable to master March 12, 2024 15:26
Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

What's the use case?

I feel like generally there should be a better solution than doing postprocessing on options.

lib/options.nix Outdated


```
filterOptions :: ([n] -> o -> bool) -> opts -> opts
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
filterOptions :: ([n] -> o -> bool) -> opts -> opts
filterOptions :: (List String -> Option -> Bool) -> Options -> Options

Copy link
Member Author

Choose a reason for hiding this comment

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

Applied.

lib/options.nix Outdated
@@ -466,6 +467,57 @@ rec {
${concatMapStringsSep "\n" (defFile: " - ${defFile}") opt.files}
'';

/**
Filters an options tree.
Copy link
Member

Choose a reason for hiding this comment

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

This is very minimal. It's good to have "tree", but what does it do? How is the callback used?

Something like:

Filters an option tree, such that the returned tree only contains the options for which the callback returns true.

Arguments passed to the callback are:

  • The option path, as a list of strings representing the attribute names.
  • The option declaration at that path.

It is not possible to prune entire branches of the option tree in one go, as the callback is only invoked for each option (leaf).


Question: doesn't this leave empty branches behind?

Copy link
Member Author

Choose a reason for hiding this comment

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

Applied.

Copy link
Member Author

Choose a reason for hiding this comment

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

There's a test for leaving no empty branches now.

lib/options.nix Outdated
=> {
services.foo = mkOption { description = "foo"; };
programs.a-prog = mkOption { description = "program"; };
}
Copy link
Member

Choose a reason for hiding this comment

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

This can not be the result. Please test 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.

Amended.

@mightyiam mightyiam marked this pull request as draft March 14, 2024 14:20
@mightyiam mightyiam force-pushed the filterOptions branch 3 times, most recently from ef204ea to 819202f Compare March 16, 2024 05:47
@mightyiam
Copy link
Member Author

The use case is filtering options prior to generating documentation for them.
The actual usage is here. Docs here.

Without that filtering, documentation for all of Nixpkgs' NixOS options would be generated.

@mightyiam mightyiam marked this pull request as ready for review March 16, 2024 05:57
Co-authored-by: Samyak S Sarnayak <samyak201@gmail.com>
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/3683

@infinisil
Copy link
Member

infinisil commented Apr 9, 2024

Here's a potentially cleaner alternative: If you create a submodule of all the options that you want to generate documentation for, you can use options.my.submodule.type.getSubOptions [ "my" "submodule" ], e.g. for services.maubot.settings:

pkgs.nixosOptionsDoc {
  options = options.services.maubot.settings.type.getSubOptions [ ];
  # ...;
}

You can change the [ ] to [ "my" "submodule" ] to generate the prefixed options instead if desired.

@k3yss k3yss requested a review from roberth July 4, 2024 11:17
@FliegendeWurst FliegendeWurst added the 2.status: merge conflict This PR has merge conflicts with the target branch label Dec 1, 2024
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: lib The Nixpkgs function library 6.topic: module system About "NixOS" module system internals 10.rebuild-darwin: 1-10 10.rebuild-darwin: 1 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants