-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Tilde (~) in path literal is illegal #7742
Comments
This diff appears to allow this to work just fine without expanding the diff --git a/src/libexpr/lexer.l b/src/libexpr/lexer.l
index 462b3b602..1d2421f8a 100644
--- a/src/libexpr/lexer.l
+++ b/src/libexpr/lexer.l
@@ -114,7 +114,7 @@ ANY .|\n
ID [a-zA-Z\_][a-zA-Z0-9\_\'\-]*
INT [0-9]+
FLOAT (([1-9][0-9]*\.[0-9]*)|(0?\.[0-9]+))([Ee][+-]?[0-9]+)?
-PATH_CHAR [a-zA-Z0-9\.\_\-\+]
+PATH_CHAR [a-zA-Z0-9\.\_\-\+~]
PATH {PATH_CHAR}*(\/{PATH_CHAR}+)+\/?
PATH_SEG {PATH_CHAR}*\/
HPATH \~(\/{PATH_CHAR}+)+\/?
But that list still looks fairly restrictive (e.g. disallows |
One thing we have to be careful of when adding |
There are many characters, that are valid in actual paths but not in nix’ path-literals because it might collide with other syntax. We might want to allow the escape character |
@aherrmann How are these path values passed to |
@infinisil On the command-line, e.g. as an "external_file",
- "$(location @nixpkgs_location_expansion_test_file//:test_file)"
+ './$${"$(location @nixpkgs_location_expansion_test_file//:test_file)"}',
], What's unfortunate is that this is not hidden in some implementation detail of rules_nixpkgs but exposed to its users. |
@aherrmann I see, then I think an There are two differences that might need to be fixed then:
|
@infinisil Thanks for looking into this!
Bazel usually deals in relative paths and we try to avoid absolute paths to Bazel managed files because they are generally not reproducible: They'll typically have a prefix of the form |
Oh then that's great, because the current code using |
@infinisil I'm not sure I understand. When I try to use the stringly relative path then I get errors of the form
So, IIUC I have to convert it to an absolute path first somehow. With a path literal that happens automatically and turns it into an absolute path in the Nix store, which is reproducible. But, if I want to do the same with |
Ohh I think you just need this actually (using cp ${./. + ("/" + local_file)} $out/out/local_file
cp ${./. + ("/" + external_file)} $out/out/external_file That's also what Nix would do underneath if Note however that
This new function prevents appending absolute paths. |
@infinisil Thank you for the pointer! Sorry to be going back and forth on this so much. I gave this a shot, One issue I encountered when passing I keep having the impression that |
@aherrmann No problem! This is interesting detail about
Note that
But since these arguments are essentially user-facing, this does make it more annoying to call. So here's an alternative that doesn't need passing an extra base directory argument, at the expense of being an impure expression (which doesn't matter much since the base directory is impure in any case and you're not using pure evaluation which would forbid it):
Yeah I agree that it seems that way, but extending the Nix grammar just to allow
Passing a dynamic Nix expression to
|
This is not a bug, because Nix intentionally restricts what characters are allowed in store paths. However, it is a valid feature request --- we could relax those rules. |
Fair enough. Thank you for the discussion and suggestions on workarounds! |
Triaged in the Nix team meeting:
|
This issue has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/2023-02-24-nix-team-meeting-minutes-35/25757/1 |
Discussed in the Nix team meeting 2022-02-27:
|
This issue has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/2023-02-27-nix-team-meeting-minutes-36/25890/1 |
Is there any workaround for prefetching URLs that contain tilde? Like https://mirror.msys2.org/msys/x86_64/msys2-keyring-1~20240410-1-any.pkg.tar.zst |
@thomasfire this seems to be a different issue, because URLs are not file system paths, and should always be passed as a string. The tilde is merely not accepted as the symbolic part of the store path, which can be worked around by setting
|
Needs documentation at https://nixos.org/manual/nix/unstable/language/builtins#builtins-fetchurl |
This issue has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/illegal-character-error-trying-to-use-fetchurl/47287/2 |
#10941 has improved the error message to become: $ nix run nix -- repl
Nix 2.24.0pre20240625_ccb679e
Type :? for help.
nix-repl> builtins.fetchurl "https://mirror.msys2.org/msys/x86_64/msys2-keyring-1~20240410-1-any.pkg.tar.zst"
error:
… while calling the 'fetchurl' builtin
at «string»:1:1:
1| builtins.fetchurl "https://mirror.msys2.org/msys/x86_64/msys2-keyring-1~20240410-1-any.pkg.tar.zst"
| ^
error: invalid store path name when fetching URL 'https://mirror.msys2.org/msys/x86_64/msys2-keyring-1~20240410-1-any.pkg.tar.zst': name 'msys2-keyring-1~20240410-1-any.pkg.tar.zst' contains illegal character '~'. Please pass an attribute set with 'url' and 'name' attributes to 'fetchurl', so that it can create a valid store path. However, the documentation still needs to be fixed. |
Describe the bug
Path literals in the Nix language do not support
~
characters within the path (apart from the first character). However, such characters may occur in valid paths.Steps To Reproduce
nix repl
./foo~
error: syntax error, unexpected invalid token, expecting end of file
.Expected behavior
Nix accepts path literals containing
~
characters in other positions than the beginning as legal paths.nix-env --version
outputnix (Nix) 2.11.1
Additional context
This is about
~
in locations other than the very beginning of the literal, where in stands for$HOME
,~/foo
is accepted by Nix 2.11.1 and resolves to the same path as$HOME/foo
.Possible workarounds thanks to @thufschmitt and @shlevy:
Possibly relevant part of the code thanks to @layus.
The issue occurs in the Bazel extension rules_nixpkgs in the context of
nixopts
location expansion when expanding labels to external workspaces in the new bzlmod dependency system. In that case the workspace directory is defined by the mangled name of the external workspace which includes~
characters, e.g.external/nixpkgs_location_expansion_test_file~override/test_file
for@nixpkgs_location_expansion_test_file//:test_file
. The above workaround would have to be performed by users of rules_nixpkgs, meaning the issue is user-facing for rules_nixpkgs.Priorities
Add 👍 to issues you find important.
The text was updated successfully, but these errors were encountered: