summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/whatsupnix/whatsupnix.bash
blob: 7089e8897990ecd104ce5aabffdd67d401026bce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash

# Prints build logs for failed derivations in quiet build mode (-Q).
# See https://github.com/NixOS/nix/issues/443
#
# Usage:
#
#    set -o pipefail
#    nix-build ... -Q ... | whatsupnix [user@target[:port]]
#


GAWK=${GAWK:-gawk}
NIX_STORE=${NIX_STORE:-nix-store}

broken=$(mktemp)
trap 'rm -f -- "$broken"' EXIT

exec >&2

$GAWK -v broken="$broken" '
  match($0, /^builder for ‘(\/nix\/store\/[^’]+\.drv)’ failed/, m) {
    print m[1] >> broken
  }
  { print $0 }
'

case $# in
  0)
    print_log() {
      NIX_PAGER= $NIX_STORE -l "$1"
    }
    ;;
  1)
    remote_user=${1%%@*}
    if test "$remote_user" = "$1"; then
      remote_user=root
    else
      set -- "${1#$remote_user@}"
    fi
    remote_port=${1##*:}
    if test "$remote_port" = "$1"; then
      remote_port=22
    else
      set -- "${1%:$remote_port}"
    fi
    remote_host=$1
    print_log() {
      ssh "$remote_user@$remote_host" -p "$remote_port" \
          env NIX_PAGER= nix-store -l "$1"
    }
    ;;
  *)
    echo "usage: whatsupnix [[USER@]HOST[:PORT]]" >&2
    exit 1
esac

while read -r drv; do
  title="** FAILED $drv LOG **"
  frame=${title//?/*}

  echo "$frame"
  echo "$title"
  echo "$frame"
  echo

  print_log "$drv"

  echo
done < "$broken"

exit 0