!tDnwWRNkmmYtMXfaZl:nixos.org

Nix Language

1922 Members
Nix programming language357 Servers

Load older messages


SenderMessageTime
17 May 2026
@trudwin:matrix.orgtrudwin Yes, but the two quotes ('') of the escape sequence merge with the single quote ' from before the escape sequence, which then results in a different string. That is the root cause of my question. :D 13:58:09
@hexa:lossy.networkhexaoh, I did not see the original question before the example, my bad.13:58:38
@trudwin:matrix.orgtrudwin I don't think so. That would be \${foo}. $$ still evaluates to two dollar signs, even in "normal" strings. 14:01:01
@hexa:lossy.networkhexaI misunderstood what you wanted, sorry14:04:34
@trudwin:matrix.orgtrudwin

So basically with naive escaping applied:

'${"a"} → encodes to → '''${"a"} → decodes to → ''a (faulty, as the value changed)

And my question is basically if there is a more idiomatic why than the following:

'${"a"} → encodes to → '$''\{"a"} → decodes to → '${"a"}

I stripped the opening and closing quotes of the indented string for better readability.

14:07:09
@trudwin:matrix.orgtrudwin *

So basically with naive escaping applied:

'${"a"} → encodes to → '''${"a"} → decodes to → ''a (faulty, as the value changed)

And my question is basically if there is a more idiomatic why than the following:

'${"a"} → encodes to → '$''\{"a"} → decodes to → '${"a"}

I skipped the opening and closing quotes of the indented string (in the middle) for better readability.

14:08:10
@piegames:flausch.socialpiegamesNo, I don't think so14:34:18
@piegames:flausch.socialpiegamesI remember running across this during the deprecations14:34:28
@piegames:flausch.socialpiegames This is the reason why ''\{ could not be deprecated 14:34:54
@trudwin:matrix.orgtrudwin

Another question I was also stumbling into while working on this topic. Is there an idiomatic why to keep some trailing whitespace in indented strings? Let's consider the following code:

let
  markdown = ''
    **Headline:**

        code snippet
  '';
in
...

If I remove the first line of the markdown snipped (i.e. the "headline"), the additional indent of the code snippet gets trimmed. So instead of

    code snippet

the resulting string value would be:

code snippet

Is there some why to represent indented code block inside an indented string?

18:19:42
@trudwin:matrix.orgtrudwin

I guess I could do the following:

let
  markdown = ''
        code snippet
    ${""}'';
in
...

This would basically use the ${""} just in front on the closing quotes to mark the indent of the string. Not sure if there is a better way to do that in the Nix language. However, I guess this case might be rarely relevant. I currently consider to just kind of ignore this edge case for now.

18:25:28
@lumi:the-apothecary.clubNuwa changed their profile picture.22:24:07
@lumi:the-apothecary.clubNuwa changed their profile picture.23:19:37
@lumi:the-apothecary.clubNuwa changed their profile picture.23:41:13
18 May 2026
@piegames:flausch.socialpiegames
In reply to @trudwin:matrix.org

I guess I could do the following:

let
  markdown = ''
        code snippet
    ${""}'';
in
...

This would basically use the ${""} just in front on the closing quotes to mark the indent of the string. Not sure if there is a better way to do that in the Nix language. However, I guess this case might be rarely relevant. I currently consider to just kind of ignore this edge case for now.

No, this is indeed the best currently available workaround
07:30:41
@cesare:matrix.uao.mecesare joined the room.10:03:14
@afarawaystar:matrix.org@afarawaystar:matrix.org left the room.14:16:59
@toonn:matrix.orgtoonn I find myself doing `// optionalAttrs (attrs ? a) { inherit (attrs) a; }` a lot. Is there a nice way to shorten that? I naively tried `attrs: attrname: optionalAttrs (attrs ? "${attrname}") { inherit (attrs) "${attrname}"; }` but dynamic attributes aren't allowed in inherit, I assume `{ ${attrname} = attrs.${attrname}; }` would work but "nice" was left behind somewhere along the road... 15:47:25
@piegames:flausch.socialpiegames Uh, is this not just a fancy filterAttrs? 15:48:52
@piegames:flausch.socialpiegames // filterAttrs (n: _: n == "attrname") attrs 15:49:30
@llakala:matrix.orgllakalafilterAttrs is heavier, requiring a whole removeAttrs15:53:23
@llakala:matrix.orgllakala

I personally choose not to use optionalAttrs in that way, instead choosing

  ${if cond then "foo" else null} = value;

xovidium has said that empty merges are free on new nix versions though

15:54:44
@llakala:matrix.orgllakala(I've been working close to the metal on mkDerivation, which makes me performance minded)15:55:17
@llakala:matrix.orgllakala * 15:57:12
@toonn:matrix.orgtoonn I have the same reservation about filterAttrs. The lambda also kinda obscures what's going on, it's too powerful in a way. 17:03:03
@toonn:matrix.orgtoonn Hadn't considered a dynamic attribute with if-then-else. Useful for multiple attributes at a time but I do feel like it's more obscure. 17:05:00
@chaotic.smol:matrix.orgArcadia (Cady) Rose joined the room.17:09:05
19 May 2026
@lumi:the-apothecary.clubNuwa changed their profile picture.14:56:39
@todoqki:matrix.orgtodo joined the room.15:41:02
@lumi:the-apothecary.clubNuwaRedacted or Malformed Event20:24:00

Show newer messages


Back to Room ListRoom Version: 6