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.strings: deprecate lib.strings.{head,tail} #313144

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

Conversation

FireyFly
Copy link
Member

These operate on lists and error if you try to call them on strings... and they're already exposed under lib and lib.lists

Description of changes

  • lib.strings.head is no longer an alias for builtins.head, ditto for tail

Things done

I checked (somewhat sloppily) for references with rg lib.strings.head, rg '\(lib.strings\) .*head, rg lib.strings.tail, rg \(lib.strings\) .*tail and didn't notice any. I checked that <nixpkgs> and <nixpkgs/nixos> evaluate (on my system/with my config) as well

  • 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.

@FireyFly FireyFly requested a review from infinisil as a code owner May 20, 2024 14:30
@github-actions github-actions bot added the 6.topic: lib The Nixpkgs function library label May 20, 2024
@FireyFly FireyFly requested review from infinisil and removed request for infinisil May 20, 2024 14:30
@eclairevoyant
Copy link
Contributor

eclairevoyant commented May 20, 2024

We should probably throw an actual error message (or possibly even simply warn for now) to explain why it would be removed

@FireyFly
Copy link
Member Author

Yep, that's very fair, you're probably right... I think after a period it'd be nice to fully remove them though since they still show up in e.g. tabcompletion in the REPL otherwise..

I figured in this case it might make sense to be a bit more bold than usual for deprecation since well... the existing functions don't even work on strings (so chances are pretty slim anyone depends on importing these from lib.strings and using them on lists, honestly)

@eclairevoyant
Copy link
Contributor

chances are pretty slim anyone depends on importing these from lib.strings and using them on lists, honestly

You'd be surprised what people do, especially accidentally, since nix lacks robust static analysis

@FireyFly FireyFly force-pushed the remove-lib-strings-head-tail branch from 92895c9 to 4a3df5b Compare May 21, 2024 21:09
@FireyFly
Copy link
Member Author

I updated the PR to warn instead of fully remove the functions, to offer a grace period for people to correct any eventual use of lib.strings.{head,tail}

@FireyFly FireyFly changed the title lib/strings: remove 'head' and 'tail' lib.strings: deprecate lib.strings.{head,tail} (which don't work on strings) May 21, 2024
@FireyFly FireyFly changed the title lib.strings: deprecate lib.strings.{head,tail} (which don't work on strings) lib.strings: deprecate lib.strings.{head,tail} May 21, 2024
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.

Just some relatively minor comments, but otherwise this looks good!

lib/strings.nix Outdated Show resolved Hide resolved
lib/strings.nix Outdated
Comment on lines 1341 to 1345
head_tail_warn_msg = name:
''
lib.strings.${name} only supports lists; use lib.lists.${name} instead.
The lib.strings.${name} alias is deprecated and will be removed after the NixOS 2024.11 release.
'';
Copy link
Member

Choose a reason for hiding this comment

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

Imo it's a bit overkill to abstract such an error message, and as implemented this actually slows down lib evaluation a little bit, because of the extra // used. I think it would be fine to just inline the error twice

Copy link
Member Author

@FireyFly FireyFly May 22, 2024

Choose a reason for hiding this comment

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

Ah, I pulled it out because the main attrset is rec and already uses head/tail a few times but we can change the couple uses to explicit builtins.{head,tail} in the file

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 moved it into the main rec {} attrset with exports and changed occurrences of head/tail in the file to use builtins.{head,tail} explicitly

@FireyFly FireyFly force-pushed the remove-lib-strings-head-tail branch from 4a3df5b to 2733fca Compare May 22, 2024 09:26
@FireyFly FireyFly requested a review from infinisil May 22, 2024 09:33
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.

Just a minor comment, looking good after that!

lib/strings.nix Outdated
@@ -85,7 +94,7 @@ rec {
list:
if list == [] || length list == 1
then list
else tail (lib.concatMap (x: [separator x]) list);
else builtins.tail (lib.concatMap (x: [separator x]) list);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of doing this, you can add

  inherit (lib) head tail;

to the top let in

(the preference should be to get symbols from lib, because it's more flexible)

Copy link
Member Author

@FireyFly FireyFly May 24, 2024

Choose a reason for hiding this comment

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

The problem with that is the top-level let-in is above the rec { } attrset where we define the deprecated head and tail with warnings... so any unqualified references will refer to those and throw warnings.

(That's why at first I had pulled those deprecated attributes out into a separate attrset that was merged in with // before...)

I can access the functions from lib instead of builtins though if that's preferred

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to using lib.{head,tail} instead of builtins.{head,tail} as requested. As mentioned I don't see how a let-inherit would help here since the head/tail with warnings in the returned recursive attrset would shadow them. Please let me know if I'm missing something, or if there's a different approach you would prefer...

@FireyFly FireyFly requested a review from infinisil May 27, 2024 20:11
These are aliases for builtins.{head,tail}, which only operate on lists and
error if you try to feed them strings.  They're already exposed under `lib`
and `lib.lists`, which are less misleading names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants