about summary refs log tree commit diff
path: root/modules/user/aszlig/services/i3/conky.nix
blob: d9d4440967847f783573e1e0581c1eaaae68df61 (plain) (blame)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{ pkgs ? import (import ../../../../../nixpkgs-path.nix) {}
, lib ? import "${import ../../../../../nixpkgs-path.nix}/lib"
, timeout ? 300
}:

with lib;

let
  baseConfig = pkgs.writeText "conkyrc" ''
    cpu_avg_samples 2
    net_avg_samples 2
    no_buffers yes
    out_to_console yes
    out_to_ncurses no
    out_to_stderr no
    out_to_x no
    extra_newline no
    update_interval 1.0
    uppercase no
    use_spacer none
    pad_percents 3
    use_spacer left
    TEXT
  '';

  optexpr = name: expr: "\${${name}_disabled:-\\\${${name} ${expr}\\}}";
  cexpr = name: args: "${optexpr name (concatStringsSep " " args)}";

  mkNetInfo = iface: let
    upspeed = cexpr "upspeed" [ iface ];
    downspeed = cexpr "downspeed" [ iface ];
  in "${upspeed} ${downspeed}";

  mkDiskFree = path: let
    used = cexpr "fs_used" [ path ];
    size = cexpr "fs_size" [ path ];
  in "${used}/${size}";

  gpuTemp = "${cexpr "hwmon" [ "0" "temp" "1" ]}C";

  weather = (cexpr "weather" [
    "http://weather.noaa.gov/pub/data/observations/metar/stations/"
    "EDMA"
    "temperature"
  ]) + "C";

  mkConky = args: let
    time = cexpr "time" [ "%a %b %d %T %Z %Y" ];
    text = concatStringsSep " | " (args ++ singleton time);
    conky = pkgs.conky.override {
      weatherMetarSupport = true;
    };
  in pkgs.writeScript "conky-run.sh" ''
    #!${pkgs.stdenv.shell}
    PATH="${pkgs.coreutils}/bin"

    cpuload() {
      for i in $(seq 1 $(nproc))
      do
        [ $i -eq 1 ] || echo -n ' '
        echo -n "\''${cpu cpu$i}%"
      done
    }

    cputemp_collect() {
      for i in /sys/bus/platform/devices/coretemp.?/hwmon/hwmon?/temp?_input
      do
        [ -e "$i" ] || continue
        echo "$i" | ${pkgs.gnused}/bin/sed -re \
          's/^.*hwmon([0-9]+)[^0-9]*([0-9]+).*$/''${hwmon \1 temp \2}/'
      done
    }

    cputemp() {
      echo $(cputemp_collect)
    }

    tries=0
    while ! raw_netinfo="$(${
      "${pkgs.iproute}/sbin/ip route get 8.8.8.8 2> /dev/null"
    })"; do
      if [ $tries -ge ${toString timeout} ]; then
        upspeed_disabled=N/A
        downspeed_disabled=N/A
        break
      fi
      echo "Waiting for primary network interface to become available..."
      tries=$(($tries + 1))
      sleep 1
    done

    primary_netdev="$(echo "$raw_netinfo" | \
      ${pkgs.gnused}/bin/sed -nre 's/^.*dev *([^ ]+).*$/\1/p')"

    ${conky}/bin/conky -c "${baseConfig}" -t "${text}"
  '';

in {
  left = mkConky [
    "CPU: $(cpuload) - ${cexpr "cpu" [ "cpu0" ]}%"
    "MEM: \\$mem/\\$memmax - \\$memperc%"
    "SWAP: \\$swap/\\$swapmax \\$swapperc%"
  ];

  right = mkConky [
    "NET: ${mkNetInfo "$primary_netdev"}"
    "DF: ${mkDiskFree "/"}"
    "LAVG: \\$loadavg"
    "TEMP - CPU: $(cputemp) - GPU: ${gpuTemp} - OUTSIDE: ${weather}"
  ];

  single = mkConky [
    "CPU: $(cpuload) - ${cexpr "cpu" [ "cpu0" ]}%"
    "MEM: \\$mem/\\$memmax - \\$memperc%"
    "NET: ${mkNetInfo "$primary_netdev"}"
    "TEMP - CPU: $(cputemp) - OUTSIDE: ${weather}"
  ];
}