[Nix-dev] How to escape $'\t' in Nix multi-line string?

Layus layus.on at gmail.com
Sun Apr 30 23:55:07 CEST 2017


On 30/04/17 20:04, Bjørn Forsman wrote:
> On 30 April 2017 at 19:53, Profpatsch <mail at profpatsch.de> wrote:
>> On 17-04-30 03:07pm, Bjørn Forsman wrote:
>>> On 30 April 2017 at 15:01, Bjørn Forsman <bjorn.forsman at gmail.com> wrote:
>>>> Hi all,
>>>>
>>>> I'm stuck trying to pass $'\t' to bash via Nix multi-line strings:
>>>>
>>>> nix-repl> ''cut -d $'\t' -f 1''
>>>> error: syntax error, unexpected $undefined, expecting IND_STR or
>>>> DOLLAR_CURLY or IND_STRING_CLOSE, at (string):1:10
>>> Oh, it was only missing double single-quotes before the dollar sign.
>>>
>>> What tripped me was that the documentation says ${ is special in
>>> strings, not $'.
>> How should we improve the manual in this regard?
> Sure. But I'm also quite interested in knowing why $' needs escaping,
> especially when $ ' (space between) does not.
I guess the answer resides at 
https://github.com/NixOS/nix/blob/2f21d522c28b1e902bd7f0b5b9e7523975102d81/src/libexpr/lexer.l#L159-L162

To be efficient, we accept any $ followed by something that's not a 
opening curly brace ('{') as normal string content. But we cannot accept 
"$'", as the last single quote could be part of a double single-quote, 
thus ending the multiline-string.

The corner case of a single single-quote following a dollar requires a 
lookahead and has been left out (forgotten ? :-)) in the lexer.

For advanced usages of multiline-strings, I refer you to the tests in 
nix source code : 
https://github.com/NixOS/nix/blob/master/tests/lang/eval-okay-ind-string.nix

-- Layus.


More information about the nix-dev mailing list