~sumner/home-manager-config

home-manager-config/modules/window-manager/writeping.nix -rw-r--r-- 1.3 KiB
54885923Sumner Evans display: use autorandr 2 hours ago
                                                                                
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
{ config, pkgs, ... }: let
  rollingPingFile = "${config.home.homeDirectory}/tmp/rolling_ping";
  writepingScript = pkgs.writeShellScript "writeping" ''
    ${pkgs.coreutils}/bin/touch ${rollingPingFile}

    # Append the new ping time.
    ping=$(/run/wrappers/bin/ping -c 1 -W 1 8.8.8.8)
    if [[ $? != 0 ]]; then
        echo "fail" > ${rollingPingFile}
    else
        ${pkgs.coreutils}/bin/cat ${rollingPingFile} | ${pkgs.gnugrep}/bin/grep "fail"
        [[ $? == 0 ]] && rm ${rollingPingFile}
        ping=$(echo $ping | \
          ${pkgs.gnugrep}/bin/grep 'rtt' | \
          ${pkgs.coreutils}/bin/cut -d '/' -f 5)
        echo $ping >> ${rollingPingFile}
    fi

    # Only keep the last 10 values.
    echo "$(${pkgs.coreutils}/bin/tail ${rollingPingFile})" > ${rollingPingFile}
  '';
in
{
  systemd.user.services.writeping = {
    Unit.Description = "Write the new ping value for rolling ping average calculation";

    Service = {
      Type = "oneshot";
      ExecStart = "${writepingScript}";
    };
  };

  systemd.user.timers.writeping = {
    Unit.Description = "Write the new ping value for rolling ping average calculation";

    Timer = {
      OnCalendar = "*:*:0/10"; # Every 10 seconds
      Unit = "writeping.service";
    };

    Install.WantedBy = [ "timers.target" ];
  };
}