This commit is contained in:
kalmenn 2025-07-07 13:52:45 +02:00
parent 6fbd9b2a6c
commit 4df3ab28af
Signed by: kalmenn
GPG key ID: F500055C44BC3834
6 changed files with 182 additions and 73 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/result

View file

@ -11,9 +11,13 @@
); );
packages = nixpkgs.lib.genAttrs [ "x86_64-linux" ] ( packages = nixpkgs.lib.genAttrs [ "x86_64-linux" ] (
system: nixpkgs.lib.genAttrs [ "pterodactyl" "php" "wings" ] ( system: let
package: import ./packages/${package}.nix { pkgs = import nixpkgs { inherit system; }; }; pkgs = import nixpkgs { inherit system; };
) in rec {
}); php = import ./packages/php.nix { inherit pkgs; };
pterodactyl = import ./packages/pterodactyl.nix { inherit pkgs php; };
wings = import ./packages/wings.nix { inherit pkgs; };
}
);
}; };
} }

View file

@ -2,20 +2,36 @@
{ lib, config, pkgs, ... }: { lib, config, pkgs, ... }:
let let
cfg = config.services.pterodactyl; thisConfig = config.services.pterodactyl;
flakePkgs = flake.outputs.packages.${pkgs.system};
defaultUser = "pterodactyl"; defaultUser = "pterodactyl";
in { in {
options.services.pterodactyl = { options.services.pterodactyl = {
enable = lib.mkEnableOption "Enable the pterodacytl game server panel"; enable = lib.mkEnableOption "Enable the pterodacytl game server panel";
proxy = { pkg = lib.mkOption {
enable = lib.mkEnableOption "Automatically configure Nginx to serve the panel"; type = lib.types.package;
serverName = lib.mkOption { description = "The package in which to source the php files used by pterodactyl";
default = flake.outputs.packages.${pkgs.system}.pterodactyl;
};
dataDir = lib.mkOption {
type = lib.types.str; type = lib.types.str;
description = "The canonical domain on which the panel will be hosted"; description = "The directory in which to store stateful data";
default = "/data/services/pterodactyl";
};
server = {
enable = lib.mkEnableOption ''
Automatically configure Nginx to serve the panel
If you need more control over the nginx proxy, for now you must leave this option disabled
and write your own nginx configuration. Feel free to copy ours as a starting point.
'';
name = lib.mkOption {
type = lib.types.str;
description = "The canonical domain name on which the panel will be hosted";
default = "localhost"; default = "localhost";
example = "pterodactyl.example.com"; example = "pterodactyl.example.com";
}; };
@ -27,27 +43,46 @@ in {
}; };
}; };
php = {
socket = lib.mkOption {
type = lib.types.path;
description = "The location of the unix socket used by php-fpm";
default = config.services.phpfpm.pools.pterodactyl.socket;
};
user = lib.mkOption { user = lib.mkOption {
type = lib.types.str; type = lib.types.str;
description = '' description = ''
The user that owns the files managed by the panel. The user that the php-fpm processes will run under. See also the `group` option.
If you change this, make sure their files are accessible by your www user
If you change this, make sure the unix socket is accessible by your www user
(probably "nginx"). (probably "nginx").
''; '';
default = defaultUser; default = defaultUser;
example = defaultUser; };
group = lib.mkOption {
type = lib.types.str;
description = ''
The group that the php-fpm processes will run under. See also the `user` option.
If you change this, make sure the unix socket is accessible by your www user
(probably "nginx").
'';
default = defaultUser;
};
root = lib.mkOption {
type = lib.types.path;
description = "The main application directory";
default = "/srv/pterodactyl";
}; };
pkg = lib.mkOption { pkg = lib.mkOption {
type = lib.types.package; type = lib.types.package;
description = "The package in which to source the php files used by pterodactyl"; description = "The php package to use";
default = flakePkgs.pterodactyl; default = flake.outputs.packages.${pkgs.system}.php;
}; };
dataDir = lib.mkOption {
type = lib.types.str;
description = "The directory in which to store stateful data";
default = "/var/lib/pterodactyl";
}; };
redis = { redis = {
@ -67,29 +102,37 @@ in {
}; };
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf thisConfig.enable {
users = lib.mkIf (cfg.user == defaultUser) { users = {
users.${cfg.user} = { users = {
${thisConfig.php.user} = lib.mkIf (thisConfig.php.user == defaultUser) {
isSystemUser = true; isSystemUser = true;
createHome = true; createHome = true;
home = cfg.dataDir; home = "/var/lib/pterodactyl";
group = cfg.user; group = thisConfig.php.group;
}; };
groups.${cfg.user} = { }; ${config.services.nginx.user} = lib.mkIf (thisConfig.php.group == defaultUser) {
extraGroups = [ thisConfig.php.group ];
users.${config.services.nginx.user}.extraGroups = [ cfg.user ]; };
}; };
services.redis.servers.${cfg.redis.name} = lib.mkIf cfg.redis.configureLocally { groups = {
${thisConfig.php.group} = lib.mkIf (thisConfig.php.group == defaultUser) {};
};
};
services.redis.servers.${thisConfig.redis.name} = lib.mkIf thisConfig.redis.configureLocally {
enable = true; enable = true;
port = cfg.redis.port; port = thisConfig.redis.port;
}; };
services.nginx = lib.mkIf cfg.proxy.enable { services.nginx = lib.mkIf thisConfig.server.enable {
enable = true; enable = true;
virtualHosts."${cfg.serverName}" = { virtualHosts."pterodactyl" = {
serverName = thisConfig.server.name;
root = "${config.services.pterodactyl.pkg}/public"; root = "${config.services.pterodactyl.pkg}/public";
extraConfig = '' extraConfig = ''
@ -101,7 +144,7 @@ in {
include ${pkgs.nginx}/conf/fastcgi_params; include ${pkgs.nginx}/conf/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:${config.services.phpfpm.pools.pterodactyl.socket}; fastcgi_pass unix:${thisConfig.php.socket};
fastcgi_index index.php; fastcgi_index index.php;
@ -127,7 +170,7 @@ in {
}; };
services.phpfpm.pools.pterodactyl = { services.phpfpm.pools.pterodactyl = {
user = cfg.user; user = thisConfig.php.user;
settings = { settings = {
"listen.owner" = config.services.nginx.user; "listen.owner" = config.services.nginx.user;
"pm" = "dynamic"; "pm" = "dynamic";
@ -145,21 +188,75 @@ in {
}; };
}; };
systemd.services.pteroq = { systemd = {
tmpfiles.settings."10-pterodactyl" = let
user = thisConfig.php.user;
group = thisConfig.php.group;
in {
"${thisConfig.dataDir}/cache" = { "d" = { inherit group user; mode = "750"; }; };
"${thisConfig.dataDir}/storage" = { "d" = { inherit group user; mode = "750"; }; };
"${thisConfig.php.root}" = { "d" = { inherit group user; mode = "750"; }; };
};
services = {
pterodactyl-setup = {
description = "Setup the necessary files for the pterodactyl panel to work";
before = [ "pteroq.service" ]
++ (if thisConfig.server.enable then [ "nginx.service" ] else []);
wantedBy = [ "multi-user.target" "pteroq.service" ]
++ (if thisConfig.server.enable then [ "nginx.service" ] else []);
serviceConfig = {
Type = "oneshot";
User = thisConfig.php.user;
Group = thisConfig.php.group;
ExecStart = pkgs.writeShellScript "pterodactyl-setup.sh" ''
SRC="${thisConfig.pkg}/share/php/pterodactyl-panel/";
DEST="${thisConfig.php.root}";
USER="${thisConfig.php.user}";
GROUP="${thisConfig.php.group}";
rm $DEST/* -r;
for file in app config database public resources routes vendor artisan; do
cp -r "$SRC/$file" "$DEST"
chmod -R 0550 "$DEST/$file"
done;
mkdir -p -m 0750 "$DEST/bootstrap"
cp "$SRC/bootstrap/app.php" "$DEST/bootstrap/app.php";
chmod 0550 "$DEST/bootstrap/app.php";
# ln -s "${thisConfig.dataDir}/cache" "$DEST/bootstrap/cache";
ln -s "${thisConfig.dataDir}/storage" "$DEST/storage";
'';
};
};
pteroq = {
enable = true; enable = true;
description = "Pterodactyl Queue Worker"; description = "Pterodactyl Queue Worker";
after = [ "redis-${cfg.redis.name}.service" ]; after = [ "redis-${thisConfig.redis.name}.service" ];
unitConfig = { StartLimitInterval = 180; }; unitConfig = { StartLimitInterval = 180; };
serviceConfig = { serviceConfig = {
User = cfg.user; User = thisConfig.php.user;
Group = cfg.user; Group = thisConfig.php.group;
WorkingDirectory = thisConfig.php.root;
ExecStart = "${thisConfig.php.pkg}/bin/php ${thisConfig.php.root}/artisan queue:work --queue=high,standard,low --sleep=3 --tries=3";
Restart = "always"; Restart = "always";
ExecStart =
"${flakePkgs.php}/bin/php ${cfg.pkg}/artisan queue:work --queue=high,standard,low --sleep=3 --tries=3";
StartLimitBurst = 30;
RestartSec = "5s"; RestartSec = "5s";
StartLimitBurst = 30;
}; };
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
}; };
}; };
};
};
} }

View file

@ -1,12 +1,12 @@
{ pkgs }: { pkgs ? import <nixpkgs> }:
pkgs.php83.buildEnv { pkgs.php83.buildEnv {
extensions = { enabled, all, }: enabled ++ (with all; [ extensions = { enabled, all, }: enabled ++ (with all; [
redis redis
# xdebug xdebug
]); ]);
# extraConfig = '' extraConfig = ''
# xdebug.mode=debug xdebug.mode=debug
# ''; '';
} }

View file

@ -1,9 +1,16 @@
{ pkgs }: { pkgs ? import <nixpkgs>, php ? pkgs.php }:
let php.buildComposerProject2 (finalAttrs: {
pname = "pterodactyl-panel";
version = "1.11.11"; version = "1.11.11";
in pkgs.fetchzip {
url = "https://github.com/pterodactyl/panel/releases/download/v${ version }/panel.tar.gz"; src = pkgs.fetchzip {
url = "https://github.com/pterodactyl/panel/releases/download/v${finalAttrs.version}/panel.tar.gz";
hash = "sha256-0nOHtReVUVXYQY/glS4x0gkbhetoXSWg4rRwOJlkcM8="; hash = "sha256-0nOHtReVUVXYQY/glS4x0gkbhetoXSWg4rRwOJlkcM8=";
stripRoot = false; stripRoot = false;
} };
vendorHash = "sha256-WBzGGWCWNqvU4Ws7wBh8Ads7RBawBaxN1QNIUI4ivOQ=";
inherit php;
})

View file

@ -1,4 +1,4 @@
{ pkgs }: { pkgs ? import <nixpkgs> }:
let let
version = "1.11.13"; version = "1.11.13";