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: Add sanitizeDerivationName function #83241

Merged
merged 1 commit into from Apr 2, 2020

Conversation

infinisil
Copy link
Member

@infinisil infinisil commented Mar 23, 2020

Motivation for this change

Was part of #78640, but put into its own PR after the discussion in #83223

Ping @Profpatsch @rpearce @Mic92 @roberth

Things done
  • Manually verified that it does what it advertises
  • Added tests

@infinisil
Copy link
Member Author

@rpearce I think the safePrefix thing in your PR can be achieved just by doing validDerivationName "${safePrefix}-${string}"

@rpearce
Copy link
Contributor

rpearce commented Mar 23, 2020

Would adding tests in something like https://github.com/NixOS/nixpkgs/blob/master/lib/tests/misc.nix be useful? I must confess that I don't know how to run them, though

@rpearce
Copy link
Contributor

rpearce commented Mar 23, 2020

👍 works perfectly for me

@roberth
Copy link
Member

roberth commented Mar 23, 2020

From the title of this PR I foolishly expected a string -> bool. May I suggest sanitizeDerivationName? It's accurate and it verbs.

@infinisil
Copy link
Member Author

Changed the name to the suggested one and added some tests. Ideally the tests would generate random strings, put them through the function, then try to create a derivation with it. But our test infrastructure isn't really set up for that (yet?). So for now there's just a bunch of hardcoded ones.

@infinisil infinisil changed the title lib/strings: Add validDerivationName function lib/strings: Add sanitizeDerivationName function Mar 24, 2020
@roberth
Copy link
Member

roberth commented Mar 24, 2020

Some test improvements:

  • test the allowed characters by invoking derivation
  • test a string that is too long
  • test a string that is too long by itself, but not after bad chars are filtered out

lib/strings.nix Outdated
sanitizeDerivationName pkgs.hello
=> "-nix-store-2g75chlbpxlrqn15zlby2dfh8hr9qwbk-hello-2.10"
*/
sanitizeDerivationName = lib.flip lib.pipe [
Copy link
Member

Choose a reason for hiding this comment

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

How much overhead do these compose function actually add?

Copy link
Member Author

Choose a reason for hiding this comment

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

Shouldn't be too much. Though I have a change to it here which would improve performance a bit: 8022565

lib/strings.nix Outdated
sanitizeDerivationName pkgs.hello
=> "-nix-store-2g75chlbpxlrqn15zlby2dfh8hr9qwbk-hello-2.10"
*/
sanitizeDerivationName = lib.flip lib.pipe [
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
sanitizeDerivationName = lib.flip lib.pipe [
sanitizeDerivationName = string: lib.pipe string [

Copy link
Contributor

Choose a reason for hiding this comment

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

(noob question about how this works!)

Does this remove the dependency on lib.flip by explicitly accepting the string as an argument before passing it on to pipe's first argument? In essence, this sacrifices the point-free solution for 1 less dependency?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's less about reducing dependencies, but rather reducing mental overhead. By not using flip one doesn't need to know what flip does to understand this function.

@infinisil infinisil force-pushed the valid-drv-name branch 2 times, most recently from 57fb955 to c30a9f6 Compare March 29, 2020 16:31
@infinisil
Copy link
Member Author

infinisil commented Mar 29, 2020

@roberth Now did that, and found 2 bugs in the function while doing so :D

  • One bug was that I used a regex like "[^+-.]", thinking it would mean exclude any of "+", "-" and ".", but really it meant exclude the ascii range from "+" to ".". So now I moved the "-" to the end of the brackets, so it's interpreted not as a range.
  • The other bug was regarding the derivation length: Nix also counts the ".drv" as being part of the name, so the name itself needs to be trimmed to 207 instead of 211 characters

@Profpatsch Also applied your suggestion

@infinisil
Copy link
Member Author

One question to discuss: If the name is too long, should it be trimmed from the left or the right? Currently the function trims from the left, which would be better when names represent file names, where the left-most chars are the least specific. But trimming from the right would make more sense when it's a "${name}-${version}" like string, where the ${name} shouldn't be removed.

lib/tests/misc.nix Outdated Show resolved Hide resolved
lib/tests/misc.nix Show resolved Hide resolved
@infinisil infinisil force-pushed the valid-drv-name branch 2 times, most recently from 51f27dc to d9e992a Compare March 29, 2020 23:14
@infinisil infinisil merged commit f75c11c into NixOS:master Apr 2, 2020
@infinisil infinisil deleted the valid-drv-name branch April 2, 2020 03:58
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.

None yet

6 participants