-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.nix
115 lines (93 loc) · 3.13 KB
/
service.nix
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
# SPDX-FileCopyrightText: 2020-2023 Serokell <https://serokell.io/>
#
# SPDX-License-Identifier: MPL-2.0
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.hackage-search;
in {
options.services.hackage-search = {
enable = mkEnableOption "Hackage search server";
port = mkOption {
description = "A port for running hackage search server on";
type = with types; nullOr port;
default = null;
};
socket = mkOption {
description = "Unix domain socket to serve the server on";
type = with types; nullOr str;
default = "/run/hackage-search/server.sock";
};
package = mkOption {
description = "A hackage-search package";
type = with types; str;
# Updated from https://github.com/serokell/hackage-search/tree/release by buildkite pipeline
default = "/nix/var/nix/profiles/per-user/deploy/hackage-search";
};
};
config = mkIf (cfg.enable) {
assertions = [
{ assertion = isNull cfg.socket != isNull cfg.port;
message = "Only one of socket and port may be defined at once."; }
];
systemd.services = {
hackage-download = rec {
requires = [ "network-online.target" ];
after = requires;
path = [ cfg.package ];
startAt = "daily";
script = ''hackage-download --hackage "$CACHE_DIRECTORY"'';
startLimitBurst = mkDefault 5;
startLimitIntervalSec = mkDefault 300;
serviceConfig = {
DynamicUser = true;
User = "hackage-search";
Restart = mkDefault "on-failure";
RestartSec = mkDefault 10;
CacheDirectory = "hackage-search";
WorkingDirectory = "/var/cache/hackage-search";
Type = "oneshot";
};
};
hackage-search = rec {
wantedBy = [ "multi-user.target" ];
requires = [ "hackage-download.service" ];
after = requires;
path = with pkgs; [ ripgrep cfg.package ];
script =
let serve =
if ! isNull cfg.socket
then "--unix ${cfg.socket}"
else "--port ${toString cfg.port}";
in ''
hackage-search ${serve} --frontend "${cfg.package}/html" --hackage "$CACHE_DIRECTORY"
'';
startLimitBurst = mkDefault 5;
startLimitIntervalSec = mkDefault 300;
serviceConfig = {
DynamicUser = true;
User = "hackage-search";
Restart = mkDefault "on-failure";
RestartSec = mkDefault 10;
ExecStartPost =
if isNull cfg.socket
then null
else pkgs.writeShellScript "chmod-socket"
"sleep 5; chmod 777 ${cfg.socket}";
CacheDirectory = "hackage-search";
RuntimeDirectory =
if isNull cfg.socket
then null
else "hackage-search";
};
};
};
services.nginx.virtualHosts.hackage-search = {
locations."/".proxyPass =
if ! isNull cfg.socket
then "http://unix:${cfg.socket}:"
else "http://localhost:${toString cfg.port}";
locations."/static/fonts/".alias = "${cfg.package}/fonts/";
};
};
}