Make `nix log` respect NO_COLOR=1
Some parts of nix already respect NO_COLOR=1.
It seems however this functionality is desired, yet missing from nix log.
See: https://discourse.nixos.org/t/disabling-colors-for-nix-log-output/17480
This pull request has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/disabling-colors-for-nix-log-output/17480/5
I wonder if there's a way to make the logger do this instead of having to litter around shouldANSI checks... I haven't looked at the Nix code in a while, but I imagine that would be better than the current situation.
I wonder if there's a way to make the logger do this instead of having to litter around
shouldANSIchecks... I haven't looked at the Nix code in a while, but I imagine that would be better than the current situation.
How about wrapping std::cout in nix::cout?
I wonder if there's a way to make the logger do this instead of having to litter around
shouldANSIchecks... I haven't looked at the Nix code in a while, but I imagine that would be better than the current situation.How about wrapping
std::coutinnix::cout?
Hm. There is also logger->cout.
Hm. There is also logger->cout.
Right, probably the cleanest solution is to make logger->cout strip the ANSI colors if needed.
I suggest to add a --colour=<never|auto|always> option, that defaults to auto which is the status quo. With never, shouldANSI must always return false, while with always, it must always return true ignoring NO_COLOR and whether the file descriptor is an actual terminal.
There should be an alias --color for this option (or the other way around --colour aliasing to --color). Perhaps an --colour/--color without an argument could be equal to --colour=always, but this might complicate argument parsing.
The motivation behind --colour=always is to pipe the output e.q. to a pager like more or less. At the moment, because isatty returns false in this case, shouldANSI evaluates to false and any ANSI-colour-sequences are stripped.
This would be really nice. For example, it's convenient to pipe nix log output to gh gist create, but as is (even with NO_COLOR=1) this creates a bunch of escape sequence mess.
Seems that one can pipe to a sed -e $'s/\x1b\[[0-9;]*m//g' intermediary for now (source).
Any chance this gets some attention?
This seems like a really small PR for a decent QoL win, particularly as we need to post logs for build errors that are currently littered with ansi escape codes unless people do manual workarounds.
This seems like a really small PR for a decent QoL win, particularly as we need to post logs for build errors that are currently littered with ansi escape codes unless people do manual workarounds.
@n8henrie I'm sorry I currently don't have the time to work in the suggested changes ( https://github.com/NixOS/nix/pull/6911#issuecomment-1222278160 ). But I agree that it might not be much work to do this properly. Maybe somebody else can give it a shot. :) I'll close this for now.
@knedlsepp I stumbled upon this issue myself today. Is there an issue open to keep track of the issue properly?
@knedlsepp I stumbled upon this issue myself today. Is there an issue open to keep track of the issue properly?
Looks like https://github.com/NixOS/nix/issues/5214 would do.