Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indented strings do not work with tabs #3759

Closed
piegamesde opened this issue Jun 28, 2020 · 8 comments
Closed

Indented strings do not work with tabs #3759

piegamesde opened this issue Jun 28, 2020 · 8 comments

Comments

@piegamesde
Copy link
Member

Is your feature request related to a problem? Please describe.

The manual says about indented strings:

This kind of string literal intelligently strips indentation from the start of each line. To be precise, it strips from each line a number of spaces equal to the minimal indentation of the string as a whole (disregarding the indentation of empty lines).

This is exclusive to spaces and won't work on files indented using tabs.

Describe the solution you'd like

I'd like to have it work with tabs as well.

Describe alternatives you've considered

  • Create some helper functionality to convert between tabs and spaces
  • Create some helper functionality to strip tabbed indented strings
  • Resign and use soft tabs

Additional context

I use tabs for indentation. Programming languages should not force me to use spaces for some features to work.

@edolstra
Copy link
Member

We can't change this because it would be incompatible (it could cause the result of evaluating a derivation to change).

@piegamesde
Copy link
Member Author

If anyone else except me still wants to use tabs in their Nix files – this may help you:

stripTabs = text: let
	# Whether all lines start with a tab (or is empty)
	shouldStripTab = lines: builtins.all (line: (line == "") || (pkgs.lib.strings.hasPrefix "	" line)) lines;
	# Strip a leading tab from all lines
	stripTab = lines: builtins.map (line: pkgs.lib.strings.removePrefix "	" line) lines;
	# Strip tabs recursively until there are none
	stripTabs = lines: if (shouldStripTab lines) then (stripTabs (stripTab lines)) else lines;
in
	# Split into lines. Strip leading tabs. Concat back to string.
	builtins.concatStringsSep "\n" (stripTabs (pkgs.lib.strings.splitString "\n" text));

Usage:

myConfigExtraString = (stripTabs ''
	Some
		multiline
		text
'');

@JojOatXGME
Copy link

Other line terminators (i.e. CRLF) also prevent the removal of leading whitespace.

@piegamesde
Copy link
Member Author

I think that warrants a reopen.

@ajs124
Copy link
Member

ajs124 commented Jul 12, 2021

Not really. For once I'll have to agree with @edolstra, this behavior cannot really be adjusted, as long as nix does not have any kind of language versioning or other process for breaking changes, because as he said "it could cause the result of evaluating a derivation to change".

@piegamesde
Copy link
Member Author

@ajkovar Yes and no. Just because of this doesn't mean the issue cannot be resolved at all. Some possible alternative solutions off the hat:

  • Add a helper function, preferably built-in and always in scope
  • Emit a warning in Nix when encountering tabs or windows-style newlines in indented multi-line strings
  • Add new Syntax to Nix that fixes the problem (triple quote strings?).

@piegamesde
Copy link
Member Author

piegamesde commented Jul 12, 2021

Actually, this has another, more severe implication: Some tools like git automatically adjust line endings to the preferred flavor of the host platform. With this behavior, this may silently change derivations.

@JojOatXGME
Copy link

I think adding new syntax or built-in functions just for this issue is over the top. However, I would like to see a warning in such cases, since such issues with whitespace are very subtle. It took a lot of time until I discovered that the line terminators were causing the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants