blob: 5ddbd9c8ba8766b6f225ebba10904f61fc0bd50b (
plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.feishin;
in
{
options.services.feishin = {
enable = lib.mkEnableOption "the web version of Feishin";
package = lib.mkPackageOption pkgs "feishin-web" { };
domain = lib.mkOption {
description = "Domain to use for the virtualhost.";
type = lib.types.str;
};
pathbase = lib.mkOption {
description = "URL path base to use for the virtualhost.";
type = lib.types.str;
default = "";
example = "/music";
};
settings = lib.mkOption {
description = ''
Configuration for the web version of Feishin, specified as key-value pairs.
Refer to <https://github.com/jeffvli/feishin#configuration>, and
<https://github.com/jeffvli/feishin/blob/development/settings.js.template>
for the template.
'';
type = lib.types.attrsOf lib.types.str;
default = {
ANALYTICS_DISABLED = "true";
};
example = {
ANALYTICS_DISABLED = "true";
SERVER_NAME = "My Server";
SERVER_LOCK = "true";
};
};
nginx = {
enable = lib.mkEnableOption "a virtualhost to serve Feishin through nginx";
virtualHost = lib.mkOption {
description = "Extra configuration for the nginx virtualhost for Feishin.";
type = lib.types.submodule (import ../web-servers/nginx/vhost-options.nix { inherit config lib; });
default = { };
example = lib.literalExpression ''
{
serverAliases = [ "feishin.''${config.networking.domain}" ];
}
'';
};
};
caddy = {
enable = lib.mkEnableOption "a virtualhost to serve Feishin through Caddy";
virtualHost = lib.mkOption {
description = "Extra configuration for the Caddy virtualhost for Feishin.";
type = lib.types.submodule (
import ../web-servers/caddy/vhost-options.nix { cfg = config.services.caddy; }
);
default = { };
example = lib.literalExpression ''
{
serverAliases = [ "feishin.''${config.networking.domain}" ];
}
'';
};
};
};
config =
let
resolvedSettings =
cfg.settings // lib.optionalAttrs (cfg.pathbase != "") { PUBLIC_PATH = cfg.pathbase; };
settingsJs = ''
"use strict";
${lib.concatMapAttrsStringSep "\n" (name: value: ''window.${name} = "${value}";'') resolvedSettings}
'';
settingsJsDir = pkgs.writeTextDir "settings.js" settingsJs;
in
lib.mkIf cfg.enable {
services.nginx = lib.mkIf cfg.nginx.enable {
enable = lib.mkDefault true;
virtualHosts."${cfg.domain}" = lib.mkMerge [
cfg.nginx.virtualHost
{
root = lib.mkForce "${cfg.package}";
extraConfig = ''
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
'';
locations."${cfg.pathbase}/" = {
tryFiles = "$uri $uri/ /index.html =404";
};
locations."=${cfg.pathbase}/settings.js" = {
alias = settingsJsDir + "/settings.js";
extraConfig = ''
add_header Cache-Control "no-store";
'';
};
}
];
};
services.caddy = lib.mkIf cfg.caddy.enable {
enable = lib.mkDefault true;
virtualHosts."${cfg.domain}" = lib.mkMerge [
cfg.caddy.virtualHost
{
hostName = lib.mkForce cfg.domain;
extraConfig =
let
baseConfig = ''
encode
handle /settings.js {
header Cache-Control no-store
root ${settingsJsDir}
file_server
}
handle {
root ${cfg.package}
try_files {path} /index.html
file_server
}
'';
in
if (cfg.pathbase == "") then
baseConfig
else
''
handle_path ${cfg.pathbase}/* {
${baseConfig}
}
'';
}
];
};
};
meta.maintainers = with lib.maintainers; [
rharish
BatteredBunny
];
}
|