about summary refs log tree commit diff
path: root/modules/user/openlab/speedtest.nix
blob: 6b6d72e3c90d945cb7db37ecc9633014cc00ea2c (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
{ config, lib, pkgs, ... }:

with lib;

let
  bin = drv: name: "${lib.getBin drv}/bin/${name}";
  cfg = config.vuizvui.user.openlab.speedtest;

  py = pkgs.runCommandLocal "speedtest.py" {} ''
    cat ${./speedtest.py} \
      | sed -e 's|^PING_BIN =.*$|PING_BIN = "${config.security.wrapperDir}/ping"|' \
      > $out
  '';

  speedtest = pkgs.writeScript "speedtest" ''
    #!${bin pkgs.bash "bash"}
    mkdir -p "$(dirname "${cfg.outputPath}")"
    ${bin pkgs.python3 "python3"} ${py} >> "${cfg.outputPath}"
  '';

in {
  options.vuizvui.user.openlab.speedtest = {
    enable = mkEnableOption "openlab speedtest";
    outputPath = mkOption {
      description = "File to which the results are appended.";
      type = types.path;
      default = "/dev/null";
    };
  };


  config = mkIf cfg.enable {
    systemd.services.speedtest = {
       description = "openlab network speedtest";
       path = with pkgs; [ curl bind.host ];
       environment = { "LC_ALL" = "C"; };
       wantedBy = [ "default.target" ];
       after = [ "network.target" ];
       script = "${speedtest}";
       startAt = [ "*-*-* *:00/15:00" ];
     };

     assertions = [ {
       assertion = cfg.outputPath != "/dev/null";
       message = "You should set `vuizvui.user.openlab.speedtest.outputPath`.";
     } ];
  };
}