nix icon indicating copy to clipboard operation
nix copied to clipboard

Proper indentation of string interpolation

Open layus opened this issue 10 years ago • 4 comments

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 ?

layus avatar May 24 '15 11:05 layus

I marked this as stale due to inactivity. → More info

stale[bot] avatar Feb 15 '21 22:02 stale[bot]

I closed this issue due to inactivity. → More info

stale[bot] avatar May 01 '22 03:05 stale[bot]

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
    '';
}

federkamm avatar Feb 25 '25 06:02 federkamm

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.

rrvsh avatar Jun 17 '25 05:06 rrvsh