-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.nix
More file actions
114 lines (102 loc) · 3.11 KB
/
module.nix
File metadata and controls
114 lines (102 loc) · 3.11 KB
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
{
config,
lib,
pkgs,
...
}: let
cfg = config.services.phpuzb.telegram;
in {
options.services.phpuzb.telegram = {
enable = lib.mkEnableOption "PHP Community Telegram bot service";
user = lib.mkOption {
type = lib.types.str;
default = "phpuzb-telegram";
description = "User under which the bot runs.";
};
group = lib.mkOption {
type = lib.types.str;
default = "phpuzb-telegram";
description = "Group under which the bot runs.";
};
package = lib.mkOption {
type = lib.types.package;
description = "The PHPUZB Telegram bot package (should contain src/index.php).";
};
phpPackage = lib.mkOption {
type = lib.types.package;
default = pkgs.php84;
description = "PHP package used to run the bot and PHP-FPM pool.";
};
environment = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
default = {};
example = {
TELEGRAM_BOT_TOKEN = "xxxx";
APP_ENV = "production";
};
description = "Environment variables passed to the Telegram bot process and PHP-FPM.";
};
nginx = {
enable = lib.mkEnableOption "Enable nginx site for the bot";
serverName = lib.mkOption {
type = lib.types.str;
default = "localhost";
description = "Server name for nginx configuration.";
};
port = lib.mkOption {
type = lib.types.port;
default = 8080;
description = "Port on which nginx listens.";
};
};
config = lib.mkIf cfg.enable {
users.users.${cfg.user} = {
isSystemUser = true;
group = cfg.group;
};
users.groups.${cfg.group} = {};
systemd.services.phpuzb-telegram = {
description = "PHPUZB Telegram Bot Service";
after = ["network.target"];
wantedBy = ["multi-user.target"];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Restart = "always";
WorkingDirectory = "${cfg.package}/share/php";
ExecStart = "${cfg.phpPackage}/bin/php ${cfg.package}/share/php/src/index.php";
Environment = lib.mapAttrsToList (n: v: "${n}=${v}") cfg.environment;
};
};
services.nginx = lib.mkIf cfg.nginx.enable {
enable = true;
virtualHosts.${cfg.nginx.serverName} = {
listen = [
{
addr = "0.0.0.0";
port = cfg.nginx.port;
}
];
root = "${cfg.package}/share/php";
index = "index.php";
locations."/".extraConfig = ''
try_files $uri /index.php$is_args$args;
'';
locations."~ \.php$".extraConfig = ''
fastcgi_pass unix:${config.services.phpfpm.pools.phpuzb.socket};
include ${pkgs.nginx}/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
'';
};
};
services.phpfpm.pools.phpuzb = {
user = cfg.user;
group = cfg.group;
settings = {
"listen.owner" = cfg.user;
"listen.group" = cfg.group;
};
};
};
};
}