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

Add NIX_FORCE_COLOR env variable to skip removal of ANSI escapes #3116

Closed
wants to merge 1 commit into from

Conversation

Ma27
Copy link
Member

@Ma27 Ma27 commented Oct 3, 2019

Currently the build log from a remote builder isn't colorful as usually
no TTY is allocated when accessing the remote machine with SSH. However
using a TTY (e.g. by setting RequestTTY force) doesn't work either:

cannot build on 'ssh://builder': serialised integer 6791531838262124011 is too large for type 'j'

This patch allows to skip the removal of escape sequences by adding the env var
NIX_FORCE_COLOR=1 (e.g. in nix.envVars in the NixOS configuration).

Currently the build log from a remote builder isn't colorful as usually
no TTY is allocated when accessing the remote machine with SSH. However
using a TTY (e.g. by setting `RequestTTY force`) doesn't work either:

```
cannot build on 'ssh://builder': serialised integer 6791531838262124011 is too large for type 'j'
```

This patch allows to skip the removal of escape sequences by adding the env var
`NIX_FORCE_COLOR=1` (e.g. in `nix.envVars` in the NixOS configuration).
@zimbatm
Copy link
Member

zimbatm commented Oct 6, 2019

env vars are easy to add but also suffer from discoverability. It would be best to introduce a new command-line option for --colors auto|yes|no like on most GNU tools.

@edolstra
Copy link
Member

edolstra commented Oct 7, 2019

I don't think this is the right solution. Could you try ssh-ng://? It tunnels log messages in the same way as the Nix daemon, so it should work better.

@Ma27
Copy link
Member Author

Ma27 commented Oct 7, 2019

@zimbatm agreed! I mainly wanted to propose an approach how this could be fixedbas I'm not even sure if that's the best approach.

@edolstra thanks for the hint, didn't know about this and will try this out :)

@edolstra
Copy link
Member

edolstra commented Oct 7, 2019

But.. nix-daemon strips ANSI-sequences too.

AFAIK the stripping is done in the UI. I think the line

act->result(resBuildLogLine, currentLogLine);

in DerivationGoal::flushLine() passes the original log line verbatim. I'm not sure whether build-remote properly tunnels those messages to the caller though. (That's part of the reason for getting rid of the separate build hook, #1221.)

@Ma27
Copy link
Member Author

Ma27 commented Oct 13, 2019

@edolstra sorry for asking, but could you give me an example how to use a remote-machine with ssh-ng? As far as I understand this, the feature is used for remote stores, but I'm building remotely by default.

@zimbatm
Copy link
Member

zimbatm commented Oct 14, 2019

@Ma27 Eg: nix-build --builders ssh-ng://foobar if your user is a trusted user.

@Ma27
Copy link
Member Author

Ma27 commented Oct 14, 2019

This works indeed perfectly fine. Closing then 👍

@Ma27 Ma27 closed this Oct 14, 2019
@Ma27 Ma27 deleted the remote-builder-color branch October 14, 2019 12:25
@ruuda
Copy link

ruuda commented Sep 7, 2022

It would still be useful to have a way to force colors. For example, want to build something on CI with nix build --print-build-logs. The build logs do contain colors, but they are not visible on CI, because there is no TTY on CI, so Nix strips the escape codes, even though the CI system is capable of interpreting ANSI escape sequences.

If I understand correctly,

nix/src/libutil/util.cc

Lines 1409 to 1414 in 73fde9e

bool shouldANSI()
{
return isatty(STDERR_FILENO)
&& getEnv("TERM").value_or("dumb") != "dumb"
&& !getEnv("NO_COLOR").has_value();
}
is what prevents colors from showing up without a TTY. It would be nice to have a way to override this.

@7c6f434c
Copy link
Member

7c6f434c commented Sep 7, 2022

Presumably if you run inside expect and set TERM to what you prefer, things should work without changes to Nix?

@ruuda
Copy link

ruuda commented Sep 7, 2022

Presumably if you run inside expect and set TERM to what you prefer, things should work without changes to Nix?

It’s, not as easy as setting an environment variable, Nix checks isatty(3). One workaround is to wrap the entire thing in script --command.

@7c6f434c
Copy link
Member

7c6f434c commented Sep 7, 2022 via email

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

Successfully merging this pull request may close these issues.

None yet

5 participants