# GitHub Actions, nix-shell and coloured output

**URL:** https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479
**Category:** Help
**Created:** [February 10, 2021, 6:04pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479 "2021-02-10T18:04:37Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![jacg](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jacg/32/2653_2.png) [@jacg](https://discourse.nixos.org/u/jacg)
#### Post date: [February 10, 2021, 6:04pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/1 "2021-02-10T18:04:37Z")

</div>

GitHub Action logs show pretty coloured output.

When executing a project’s tests in a GHA with `nix-shell --run <our test suite>`, the output is monochromatic. Doing exactly the same locally, I get pretty colours in the test output.

The monochromatic test output on the GHA makes me really appreciate the colours: I want them back!

How can I persuade `nix-shell` to pick up whatever it is in the GHA environment that makes the colours appear?

---

<div class="post-metadata">

### Author: ![abathur](https://discourse.nixos.org/user_avatar/discourse.nixos.org/abathur/32/2719_2.png) [@abathur](https://discourse.nixos.org/u/abathur)
#### Post date: [February 12, 2021, 4:08pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/2 "2021-02-12T16:08:35Z")

</div>

Curious about this as well. I have some CI jobs that run color-using nix-shell demos, and one of the downsides I’ve found about moving to GA from travis-ci is losing that colored output.

---

<div class="post-metadata">

### Author: ![ryantm](https://discourse.nixos.org/user_avatar/discourse.nixos.org/ryantm/32/900_2.png) [@ryantm](https://discourse.nixos.org/u/ryantm)
#### Post date: [February 12, 2021, 6:49pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/3 "2021-02-12T18:49:47Z")

</div>

Typically I see test suites look at TERM to decide if the can output color. I’d print that out, outside and inside your shell to see what’s going on.

---

<div class="post-metadata">

### Author: ![abathur](https://discourse.nixos.org/user_avatar/discourse.nixos.org/abathur/32/2719_2.png) [@abathur](https://discourse.nixos.org/u/abathur)
#### Post date: [February 13, 2021, 7:21am UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/4 "2021-02-13T07:21:30Z")

</div>

This may not be a perfect proxy for the OP’s case since I just stuck it in a nix-build.

Outside of the execution (i.e., as an entry in the .yml), it is `dumb`, but inside my test.sh (running inside the nix-build) it is xterm-256color as I’d expect.

---

<div class="post-metadata">

### Author: ![jacg](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jacg/32/2653_2.png) [@jacg](https://discourse.nixos.org/u/jacg)
#### Post date: [February 13, 2021, 2:07pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/5 "2021-02-13T14:07:42Z")

</div>

Hmm, I seem to get `dumb` both inside and outside:

```plaintext
- name: Check $TERM outside nix-shell
  run: echo $TERM
- name: Check $TERM inside nix-shell
  run: nix-shell --run "echo $TERM"

```

gives the outputs

```plaintext
Run echo $TERM
  echo $TERM
  shell: /bin/bash -e {0}
  env:
    NIX_PATH: nixpkgs=channel:nixos-unstable
dumb

```

and

```plaintext
Run nix-shell --run "echo $TERM"
  nix-shell --run "echo $TERM"
  shell: /bin/bash -e {0}
  env:
    NIX_PATH: nixpkgs=channel:nixos-unstable
[... irrelevant noise ...]
dumb

```

FWIW, my test runner is `pytest`. I guess I should try running it outside of the `nix-shell` and see if it produces any colour.

---

<div class="post-metadata">

### Author: ![jacg](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jacg/32/2653_2.png) [@jacg](https://discourse.nixos.org/u/jacg)
#### Post date: [February 13, 2021, 3:03pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/6 "2021-02-13T15:03:07Z")

</div>

`pytest` showed no colours in the GitHub action, both outside and inside the `nix-shell`.

In the case of `pytest` I have managed to get around the problem with

```plaintext
nix-shell --run "PYTEST_ADDOPTS=--color=yes <run my tests>"

```

---

<div class="post-metadata">

### Author: ![domenkozar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/domenkozar/32/38357_2.png) [@domenkozar](https://discourse.nixos.org/u/domenkozar)
#### Post date: [February 14, 2021, 12:18pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/7 "2021-02-14T12:18:27Z")

</div>

As hinted, I’d try:

```plaintext
...
runs-on: ubuntu-latest
env:
  TERM: xterm-256color

```

---

<div class="post-metadata">

### Author: ![jacg](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jacg/32/2653_2.png) [@jacg](https://discourse.nixos.org/u/jacg)
#### Post date: [February 17, 2021, 5:16pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/8 "2021-02-17T17:16:52Z")

</div>

That seems to result in monochrome `pytest` output.

---

<div class="post-metadata">

### Author: ![cdepillabout](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cdepillabout/32/410_2.png) [@cdepillabout](https://discourse.nixos.org/u/cdepillabout)
#### Post date: [February 18, 2021, 4:46am UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/9 "2021-02-18T04:46:12Z")

</div>

I don’t know if this would be helpful, but I recently reviewed a change to a library that tries to smartly determine whether to print in color or not.

Basically, the fix was to look at the output file descriptor for whether it is a terminal device. If it is, then print in color. If it is not, then don’t print in color.

Here’s the line in question:

> <https://github.com/cdepillabout/pretty-simple/blob/d1025af063ee0fcd70a1126a510faabbf2fc370c/src/Text/Pretty/Simple/Internal/Printer.hs#L192>

Here’s the library function:

[https://hackage.haskell.org/package/base-4.14.1.0/docs/System-IO.html#v:hIsTerminalDevice](https://hackage.haskell.org/package/base-4.14.1.0/docs/System-IO.html#v:hIsTerminalDevice)

I’m not actually sure what function is being called under the hood, but my guess is something like `isatty()`:

> **[Check output device is a terminal](https://rosettacode.org/wiki/Check_output_device_is_a_terminal#C)**
>
> Task Demonstrate how to check whether the output device is a terminal or not.

* * *

It could be that GitHub Actions is redirecting output in some way that `isatty()` doesn’t return the same thing that you were seeing on TravisCI. Although it seems like you’ve figured out that forcing color output works.

---

<div class="post-metadata">

### Author: ![abathur](https://discourse.nixos.org/user_avatar/discourse.nixos.org/abathur/32/2719_2.png) [@abathur](https://discourse.nixos.org/u/abathur)
#### Post date: [February 20, 2021, 6:02am UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/10 "2021-02-20T06:02:34Z")

</div>

I poked at this a little bit, and it seems like:

- GA isn’t running tests with a tty, while travis-ci is
- Nix commands [seem to filter ansi escapes if !tty](https://github.com/NixOS/nix/blob/548437c2347159c4c79352283dd12ce58324f1d6/src/libutil/logging.cc#L74) (not certain I’m interpreting this correctly; I’m not really familiar with the Nix source)
- I found [a suggestion for hacking this with the script command](https://github.com/gfx/example-github-actions-with-tty/blob/4c5f457c65dfe61e273e0470414699420be5e134/.github/workflows/test.yml#L18), but it only works for the linux builds.

It [doesn’t look like GA has any short run plan to change this](https://github.com/actions/runner/issues/241), so it seems like the “solution” is for Nix commands to have a flag or env that explicitly enables passing through escapes. (see also: [https://no-color.org](https://no-color.org))

With the caveat that I don’t know exactly why travis-ci and GA disagree here and realize they may have some compelling reasons, it strikes me as incredibly sad/stupid that every person/team/project who encounters this has to waste time understanding the issue, hunting down which tool in their toolchain is eating the sequences, and fiddling flags/envs (or filing issues…)

---

<div class="post-metadata">

### Author: ![brendanzab](https://discourse.nixos.org/user_avatar/discourse.nixos.org/brendanzab/32/5495_2.png) [@brendanzab](https://discourse.nixos.org/u/brendanzab)
#### Post date: [December 30, 2021, 11:05am UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/11 "2021-12-30T11:05:02Z")

</div>

Heh, thanks to the above, I managed to get the colours on my `run` commands back with:

```plaintext
defaults:
  run:
    shell: 'script -q -e -c "export TERM=xterm-256color; bash {0}"'

```

Only problem was that I then ended up [getting a bunch of spammy output](https://github.com/brendanzab/ocaml-flake-example/runs/4666076150) regarding as the packages downloaded:

 ![progress screenshot](https://discourse.nixos.org/uploads/default/original/2X/e/e7caeffaa6269a6855c014881ca4c3df9e9c007f.jpeg)

I’m guessing github doesn’t allow for the rewriting of previous lines? 😅

---

<div class="post-metadata">

### Author: ![abathur](https://discourse.nixos.org/user_avatar/discourse.nixos.org/abathur/32/2719_2.png) [@abathur](https://discourse.nixos.org/u/abathur)
#### Post date: [December 30, 2021, 6:21pm UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/12 "2021-12-30T18:21:09Z")

</div>

For others reading later: the above will only work on linux (bsd script doesn’t have the -e or -c args), so it won’t be a suitable default if you have macos in your build matrix.

Also, I noticed a new comment since I last read that thread suggesting the above may mark some failing builds as successful?

[https://github.com/actions/runner/issues/241#issuecomment-842566950](https://github.com/actions/runner/issues/241#issuecomment-842566950)

---

<div class="post-metadata">

### Author: ![blaggacao](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@blaggacao](https://discourse.nixos.org/u/blaggacao)
#### Post date: [July 28, 2023, 1:36am UTC](https://discourse.nixos.org/t/github-actions-nix-shell-and-coloured-output/11479/13 "2023-07-28T01:36:46Z")

</div>

This approach claims to be cross platform: [https://github.com/Yuri6037/Action-FakeTTY/tree/master/script](https://github.com/Yuri6037/Action-FakeTTY/tree/master/script)
