Annoying bug in nix builtin readFile

Hello,

This is week 2 of learning Nix.

I am trying to start with organizing my home, using home-manager. So far things are going great, but I have come across this annoying issue: bug: unexpected '@' · Issue #5279 · nix-community/home-manager · GitHub and after investigating a bit I realized that the problem is in fact with the built in “readFile” function as I explained here.

I have also managed to reproduce it:

nix-repl> builtins.readFile /path/with/at@char/in/it                                   
error: syntax error, unexpected '@', expecting end of file
       at «string»:1:32:
            1| builtins.readFile /path/with/at@char/in/it
             |                                ^

Where is the proper place to report bugs with the Nix language itself? I am looking at Nix/Nixpkgs/NixOS · GitHub where there are areas for the package manager, docs, etc. Is there some name for the language nix uses, and a separate project for that? Or shall I just file under NixOS/nix (i.e. the package manager also has the language parser + standard libraries).

Thanks!

First of all, the homeDirectory setting for home-manager should not be a path literal syntax like /home/foo. It should be a string like "/home/foo" Apparently it applies toString so that actually shouldn’t matter. But, in case you do need an @ symbol in a path value, you can append strings to paths, like ./foo + "@bar", which returns the path value for ./foo@bar, even if you’re not allowed to represent that with path literal syntax like ./foo@bar.

1 Like

The bug occurs in home manager even if the homeDirectory is a string, as mine is and always has been.

It has nothing to do with readFile. Tilde (~) in path literal is illegal · Issue #7742 · NixOS/nix · GitHub discusses allowed characters in path literals.

But as said, paths and path-strings are different, if the latter is still causing issues, I’d call it an issue with home-manager, not nix.

Thank you for pointing to the correct bug report. I guess nothing to do other than follow #7742

I wouldn’t call it a nix bug, I linked it for the discussion. It’s an HM bug.

1 Like

Indeed the discussion there has lead to a simple fix that I have verified on my system. Either way, best close this thread and let the experienced people sort things out on the issue thread.

It is most certainly a Nix bug. If it isn’t handling valid filesystem paths then something is wrong.

It is not reasonable for Nix’s syntax to support all valid file system paths. For instance, file system paths permit spaces in file names. It’s not reasonable for Nix to support that in path literal syntax, because spaces are semantically relevant in Nix syntax (function application). I’m sure there’s countless other examples, since file system paths are basically allowed to include any bytes except for / (and I think \0?). As I said before, the solution is to use concatenation with a string literal. ./. + "@foo"./@foo, ./foo + " bar"./foo bar

2 Likes

Except that you can escape those things. There is no excuse for not being able to handle a file path with characters like “@”, it needs to just work. I use “@” to group my projects by organization if one exists and Nix explodes if I use it in any of those repositories in a way that references the path. That isn’t normal.

That’s a bug in something, but not Nix. User-provided data—including paths—shouldn’t be sent to Nix via --arg, because --arg doesn’t escape stuff. It should come via --argstr instead, and if it needs to be consumed as a path instead of as a string, converted by the consumer via path concatenation just as @ElvishJerricco has said.

My best guess is that flake-utils-plus/lib/fup-repl.nix at afcb15b845e74ac5e998358709b2b5fe42a948d1 · gytis-ivaskevicius/flake-utils-plus · GitHub needs to change from --arg to --argstr, but I don’t use this thing so I don’t know enough to test it. But that’s where the bug is.

1 Like