1{ lib, config, pkgs, ... }:
2let
3 cfg = config.services.sitespeed-io;
4 format = pkgs.formats.json { };
5in
6{
7 options.services.sitespeed-io = {
8 enable = lib.mkEnableOption "Sitespeed.io";
9
10 user = lib.mkOption {
11 type = lib.types.str;
12 default = "sitespeed-io";
13 description = "User account under which sitespeed-io runs.";
14 };
15
16 package = lib.mkOption {
17 type = lib.types.package;
18 default = pkgs.sitespeed-io;
19 defaultText = "pkgs.sitespeed-io";
20 description = "Sitespeed.io package to use.";
21 };
22
23 dataDir = lib.mkOption {
24 default = "/var/lib/sitespeed-io";
25 type = lib.types.str;
26 description = "The base sitespeed-io data directory.";
27 };
28
29 period = lib.mkOption {
30 type = lib.types.str;
31 default = "hourly";
32 description = ''
33 Systemd calendar expression when to run. See {manpage}`systemd.time(7)`.
34 '';
35 };
36
37 runs = lib.mkOption {
38 default = [ ];
39 description = ''
40 A list of run configurations. The service will call sitespeed-io once
41 for every run listed here. This lets you examine different websites
42 with different sitespeed-io settings.
43 '';
44 type = lib.types.listOf (lib.types.submodule {
45 options = {
46 urls = lib.mkOption {
47 type = with lib.types; listOf str;
48 default = [];
49 description = ''
50 URLs the service should monitor.
51 '';
52 };
53
54 settings = lib.mkOption {
55 type = lib.types.submodule {
56 freeformType = format.type;
57 options = { };
58 };
59 default = { };
60 description = ''
61 Configuration for sitespeed-io, see
62 <https://www.sitespeed.io/documentation/sitespeed.io/configuration/>
63 for available options. The value here will be directly transformed to
64 JSON and passed as `--config` to the program.
65 '';
66 };
67
68 extraArgs = lib.mkOption {
69 type = with lib.types; listOf str;
70 default = [];
71 description = ''
72 Extra command line arguments to pass to the program.
73 '';
74 };
75 };
76 });
77 };
78 };
79
80 config = lib.mkIf cfg.enable {
81 assertions = [
82 {
83 assertion = cfg.runs != [];
84 message = "At least one run must be configured.";
85 }
86 {
87 assertion = lib.all (run: run.urls != []) cfg.runs;
88 message = "All runs must have at least one url configured.";
89 }
90 ];
91
92 systemd.services.sitespeed-io = {
93 description = "Check website status";
94 startAt = cfg.period;
95 serviceConfig = {
96 WorkingDirectory = cfg.dataDir;
97 User = cfg.user;
98 };
99 preStart = "chmod u+w -R ${cfg.dataDir}"; # Make sure things are writable
100 script = (lib.concatMapStrings (run: ''
101 ${lib.getExe cfg.package} \
102 --config ${format.generate "sitespeed.json" run.settings} \
103 ${lib.escapeShellArgs run.extraArgs} \
104 ${builtins.toFile "urls.txt" (lib.concatLines run.urls)} &
105 '') cfg.runs) +
106 ''
107 wait
108 '';
109 };
110
111 users = {
112 extraUsers.${cfg.user} = {
113 isSystemUser = true;
114 group = cfg.user;
115 home = cfg.dataDir;
116 createHome = true;
117 homeMode = "755";
118 };
119 extraGroups.${cfg.user} = { };
120 };
121 };
122}