about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/networking/sitespeed-io.nix
blob: f7eab0bb19d7748a0cd26292bd443ca3800e83da (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
119
120
121
122
{ lib, config, pkgs, ... }:
let
  cfg = config.services.sitespeed-io;
  format = pkgs.formats.json { };
in
{
  options.services.sitespeed-io = {
    enable = lib.mkEnableOption (lib.mdDoc "Sitespeed.io");

    user = lib.mkOption {
      type = lib.types.str;
      default = "sitespeed-io";
      description = lib.mdDoc "User account under which sitespeed-io runs.";
    };

    package = lib.mkOption {
      type = lib.types.package;
      default = pkgs.sitespeed-io;
      defaultText = "pkgs.sitespeed-io";
      description = lib.mdDoc "Sitespeed.io package to use.";
    };

    dataDir = lib.mkOption {
      default = "/var/lib/sitespeed-io";
      type = lib.types.str;
      description = lib.mdDoc "The base sitespeed-io data directory.";
    };

    period = lib.mkOption {
      type = lib.types.str;
      default = "hourly";
      description = lib.mdDoc ''
        Systemd calendar expression when to run. See {manpage}`systemd.time(7)`.
      '';
    };

    runs = lib.mkOption {
      default = [ ];
      description = lib.mdDoc ''
        A list of run configurations. The service will call sitespeed-io once
        for every run listed here. This lets you examine different websites
        with different sitespeed-io settings.
      '';
      type = lib.types.listOf (lib.types.submodule {
        options = {
          urls = lib.mkOption {
            type = with lib.types; listOf str;
            default = [];
            description = lib.mdDoc ''
              URLs the service should monitor.
            '';
          };

          settings = lib.mkOption {
            type = lib.types.submodule {
              freeformType = format.type;
              options = { };
            };
            default = { };
            description = lib.mdDoc ''
              Configuration for sitespeed-io, see
              <https://www.sitespeed.io/documentation/sitespeed.io/configuration/>
              for available options. The value here will be directly transformed to
              JSON and passed as `--config` to the program.
            '';
          };

          extraArgs = lib.mkOption {
            type = with lib.types; listOf str;
            default = [];
            description = lib.mdDoc ''
              Extra command line arguments to pass to the program.
            '';
          };
        };
      });
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
    {
      assertion = cfg.runs != [];
      message = "At least one run must be configured.";
    }
    {
      assertion = lib.all (run: run.urls != []) cfg.runs;
      message = "All runs must have at least one url configured.";
    }
  ];

    systemd.services.sitespeed-io = {
      description = "Check website status";
      startAt = cfg.period;
      serviceConfig = {
        WorkingDirectory = cfg.dataDir;
        User = cfg.user;
      };
      preStart = "chmod u+w -R ${cfg.dataDir}"; # Make sure things are writable
      script = (lib.concatMapStrings (run: ''
        ${lib.getExe cfg.package} \
          --config ${format.generate "sitespeed.json" run.settings} \
          ${lib.escapeShellArgs run.extraArgs} \
          ${builtins.toFile "urls.txt" (lib.concatLines run.urls)} &
      '') cfg.runs) +
      ''
        wait
      '';
    };

    users = {
      extraUsers.${cfg.user} = {
        isSystemUser = true;
        group = cfg.user;
        home = cfg.dataDir;
        createHome = true;
        homeMode = "755";
      };
      extraGroups.${cfg.user} = { };
    };
  };
}