1 { config, options, pkgs, lib, ... }:
3 inherit (lib) literalExpression mkOption mkEnableOption mkIf mkMerge types optional;
5 cfg = config.services.uptime;
6 opt = options.services.uptime;
8 configDir = pkgs.runCommand "config" { preferLocalBuild = true; }
9 (if cfg.configFile != null then ''
11 ext=`echo ${cfg.configFile} | grep -o \\..*`
12 ln -sv ${cfg.configFile} $out/default$ext
13 ln -sv /var/lib/uptime/runtime.json $out/runtime.json
16 cat ${pkgs.nodePackages.node-uptime}/lib/node_modules/node-uptime/config/default.yaml > $out/default.yaml
17 cat >> $out/default.yaml <<EOF
19 autoStartMonitor: false
22 connectionString: 'mongodb://localhost/uptime'
24 ln -sv /var/lib/uptime/runtime.json $out/runtime.json
27 options.services.uptime = {
28 configFile = mkOption {
29 description = lib.mdDoc ''
30 The uptime configuration file
32 If mongodb: server != localhost, please set usesRemoteMongo = true
34 If you only want to run the monitor, please set enableWebService = false
35 and enableSeparateMonitoringService = true
37 If autoStartMonitor: false (recommended) and you want to run both
38 services, please set enableSeparateMonitoringService = true
41 type = types.nullOr types.path;
46 usesRemoteMongo = mkOption {
47 description = lib.mdDoc "Whether the configuration file specifies a remote mongo instance";
54 enableWebService = mkEnableOption (lib.mdDoc "the uptime monitoring program web service");
56 enableSeparateMonitoringService = mkEnableOption (lib.mdDoc "the uptime monitoring service") // {
57 default = cfg.enableWebService;
58 defaultText = literalExpression "config.${opt.enableWebService}";
62 description = lib.mdDoc "The node environment to run in (development, production, etc.)";
66 default = "production";
70 config = mkMerge [ (mkIf cfg.enableWebService {
71 systemd.services.uptime = {
72 description = "uptime web service";
73 wantedBy = [ "multi-user.target" ];
75 NODE_CONFIG_DIR = configDir;
76 NODE_ENV = cfg.nodeEnv;
77 NODE_PATH = "${pkgs.nodePackages.node-uptime}/lib/node_modules/node-uptime/node_modules";
79 preStart = "mkdir -p /var/lib/uptime";
80 serviceConfig.ExecStart = "${pkgs.nodejs}/bin/node ${pkgs.nodePackages.node-uptime}/lib/node_modules/node-uptime/app.js";
83 services.mongodb.enable = mkIf (!cfg.usesRemoteMongo) true;
84 }) (mkIf cfg.enableSeparateMonitoringService {
85 systemd.services.uptime-monitor = {
86 description = "uptime monitoring service";
87 wantedBy = [ "multi-user.target" ];
88 requires = optional cfg.enableWebService "uptime.service";
89 after = optional cfg.enableWebService "uptime.service";
91 NODE_CONFIG_DIR = configDir;
92 NODE_ENV = cfg.nodeEnv;
93 NODE_PATH = "${pkgs.nodePackages.node-uptime}/lib/node_modules/node-uptime/node_modules";
95 # Ugh, need to wait for web service to be up
96 preStart = if cfg.enableWebService then "sleep 1s" else "mkdir -p /var/lib/uptime";
97 serviceConfig.ExecStart = "${pkgs.nodejs}/bin/node ${pkgs.nodePackages.node-uptime}/lib/node_modules/node-uptime/monitor.js";