Proper indentation of string interpolation
I have recently looked into https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/x11/hardware/synaptics.nix whose interesting content is essentially :
let tapConfig = ''
Option "MaxTapTime" "180"
Option "MaxTapMove" "220"
'';
in {
services.xserver.config =
''
...
# Automatically enable the synaptics driver for all touchpads.
Section "InputClass"
Identifier "synaptics touchpad catchall"
MatchIsTouchpad "on"
Driver "synaptics"
${tapConfig}
Option "ClickFinger1" "1"
EndSection
'';
}
This piece of code results into
...
# Automatically enable the synaptics driver for all touchpads.
Section "InputClass"
Identifier "synaptics touchpad catchall"
MatchIsTouchpad "on"
Driver "synaptics"
Option "MaxTapTime" "180"
Option "MaxTapMove" "220"
Option "ClickFinger1" "1"
EndSection
instead of the expected
...
# Automatically enable the synaptics driver for all touchpads.
Section "InputClass"
Identifier "synaptics touchpad catchall"
MatchIsTouchpad "on"
Driver "synaptics"
Option "MaxTapTime" "180"
Option "MaxTapMove" "220"
Option "ClickFinger1" "1"
EndSection
Is my interpretation a desirable behaviour ? Stated differently : Would a patch fixing indentation of embedded indented strings be accepted ?
I marked this as stale due to inactivity. → More info
I closed this issue due to inactivity. → More info
Possible workaround (requires with lib;):
let tapConfig = ''
Option "MaxTapTime" "180"
Option "MaxTapMove" "220"
'';
indent = n: let sep = "\n" + concatStrings (replicate n " "); in s:
stringAsChars (c: if c == "\n" then sep else c) (removeSuffix "\n" s);
in {
services.xserver.config = ''
...
# Automatically enable the synaptics driver for all touchpads.
Section "InputClass"
Identifier "synaptics touchpad catchall"
MatchIsTouchpad "on"
Driver "synaptics"
${indent 2 tapConfig}
Option "ClickFinger1" "1"
EndSection
'';
}
As an addendum to the above:
indent =
n: s:
let
indentString = concatStrings (replicate n " ");
sep = "\n" + indentString;
in
indentString + stringAsChars (c: if c == "\n" then sep else c) (removeSuffix "\n" s);
The first iteration would not indent the first line of the interpolated string. This fixes that.