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/types: make pattern of strMatching accessible #350467

Merged
merged 2 commits into from
Dec 9, 2024

Conversation

hsjobeki
Copy link
Contributor

Makes pattern of strMatching accessible in the option tree.

Thus this allows to generate for example json-schema validation
https://json-schema.org/understanding-json-schema/reference/regular_expressions

Context: I am building a Frontend that is partially derived from those options. Exposing the pattern is very helpfull in that case.
Otherwise my users get evalModules eval stacktraces. Which is after the field was submitted / sent to the server.
If i knew the pattern upfront then i can display feedback as the user types.

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.11 Release Notes (or backporting 23.11 and 24.05 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.

@hsjobeki hsjobeki requested a review from infinisil October 22, 2024 11:42
@github-actions github-actions bot added 6.topic: module system About "NixOS" module system internals 6.topic: lib The Nixpkgs function library labels Oct 22, 2024
@nix-owners nix-owners bot requested a review from roberth October 22, 2024 11:43
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.

I'm pretty sure this wasn't the original intent of the functor attribute.
We'll need to either encourage this or come up with an alternative.
@infinisil wdyt?

lib/types.nix Outdated Show resolved Hide resolved
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Oct 22, 2024
@hsjobeki
Copy link
Contributor Author

hsjobeki commented Oct 23, 2024

@infinisil. I thought that it could make sense to move away from using wrapped in defaultFunctor entirely, for all types.

Instead using only payload with payload.wrapped beeing the new convention for wrapped.
I hoped this could reduce technical complexity as shown:

# Example for 'types.attrsOf'
functor = (defaultFunctor name) // {
       # These could be defaults for composed types.
        payload.wrapped = elemType;
        type = payload: attrsOf payload.wrapped;
        binOp = lhs: rhs: { 
           wrapped = lhs.wrapped.typeMerge rhs.wrapped.functor; 
           # Place for other payload merging, no need to decide between .wrapped and .payload. (see attrsWith, which uses both)
         };
      };
      

Once all types use payload exclusively. This reduces complexity in the typeMerging functions.
Because payload can include all of wrapped functionallity as well, and is extensible for type specific additional attributes.
For example:

  • attrsWith has placeholder
  • strMatching has pattern
  • separatedString has sep
  • attrTag has tags
    ...
  defaultTypeMerge = f: f':
   let
       payload = f.binOp f.payload f'.payload;
   in
   # cannot merge different types
   if f.name != f'.name
      then null
   # simple types
   else if  (f.payload == null && f'.payload == null)
      then f.type
   # all other types
   else if (f.payload != null && f'.payload != null)
      then f.type payload
   else null;

Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

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

After #363565, we could make functor.payload stable (along with functor.{binOp,type}, and kind of functor.name), but that's a bit of a separate discussion.

While this doesn't fix a problem related to Nixpkgs (seemingly nobody has ever needed type merging for strMatching!), this doesn't break anything and unblocks @hsjobeki with getting access to the pattern for a UI, so I think this is fine to merge :)

@github-actions github-actions bot added 10.rebuild-darwin: 1-10 and removed 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin labels Dec 9, 2024
@infinisil infinisil merged commit 33fe023 into NixOS:master Dec 9, 2024
22 of 23 checks passed
@nixos-discourse
Copy link

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

https://discourse.nixos.org/t/use-cases-of-option-type-internals/57317/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: lib The Nixpkgs function library 6.topic: module system About "NixOS" module system internals 10.rebuild-darwin: 1-10 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants