Skip to content
Snippets Groups Projects
Commit dd3f968a authored by John Locke's avatar John Locke
Browse files

feat(settings): Make nix include more compatible with non-flake environments

Include the settings.nix.php file only if the Drupal Flake's database server is running.

Issue #3517263 by freelock: Make configuration compatible with ddev
parent 92afd5ae
Branches
Tags
No related merge requests found
......@@ -5,7 +5,8 @@ set -e
# Use params or env vars, fallback to defaults
PROJECT_NAME=${1:-${PROJECT_NAME:-$(basename $(pwd))}}
DOCROOT=${2:-${DOCROOT:-""}}
DOCROOT=${2:-${DOCROOT:-"web"}}
DB_SOCKET=${3:-${DB_SOCKET:-"${PWD}/data/${PROJECT_NAME}-db/mysql.sock"}}
SITE_PATH="${DOCROOT%/}${DOCROOT:+/}sites/default"
SETTINGS_FILE="${SITE_PATH}/settings.php"
NIX_SETTINGS_FILE="${SITE_PATH}/settings.nix.php"
......@@ -24,7 +25,7 @@ cat > "${NIX_SETTINGS_FILE}" << EOL
'username' => "drupal",
'password' => "",
'host' => 'localhost',
'unix_socket' => "${PWD}/data/${PROJECT_NAME}-db/mysql.sock",
'unix_socket' => "${PWD}/${DB_SOCKET}",
'driver' => 'mysql',
'prefix' => "",
];
......@@ -41,10 +42,22 @@ EOL
# Create files if not present
mkdir -p ${SITE_PATH}/files
# Add include to settings.php if not already present
if ! grep -q "settings.nix.php" "$SETTINGS_FILE"; then
echo 'if (file_exists($app_root . "/" . $site_path . "/settings.nix.php")) {' >> "$SETTINGS_FILE"
echo ' include $app_root . "/" . $site_path . "/settings.nix.php";' >> "$SETTINGS_FILE"
echo "}" >> "$SETTINGS_FILE"
# Add or replace include block in settings.php
if grep -q "settings\.nix\.php" "$SETTINGS_FILE"; then
# Remove block only if DB_SOCKET check is not present
if ! grep -q "file_exists(.*\.sock.*)" "$SETTINGS_FILE"; then
sed -i '/if (file_exists($app_root \. "\/" \. $site_path \. "\/settings\.nix\.php")) {/,/^}/d' "$SETTINGS_FILE"
fi
fi
# Add new include block if not already present
if ! grep -q "settings\.nix\.php" "$SETTINGS_FILE"; then
cat >> "$SETTINGS_FILE" << EOL
if (file_exists(\$app_root . "/" . \$site_path . "/settings.nix.php") &&
file_exists("${PWD}/${DB_SOCKET}") &&
filetype("${PWD}/${DB_SOCKET}") === "socket") {
include \$app_root . "/" . \$site_path . "/settings.nix.php";
}
EOL
fi
......@@ -17,9 +17,10 @@ pushd "$PROJECT_ROOT" || exit 1
echo "Removing old files..."
rm -rf flake.nix flake.lock .envrc .env.example .services
echo "Updating to the latest version of the drupal-flake..."
# Initialize a new flake using the drupal-flake template
nix flake init -t git+https://git.drupalcode.org/project/drupal_flake --refresh
FLAKE_SOURCE=${1:-"git+https://git.drupalcode.org/project/drupal_flake"}
echo "Updating to the latest version of the drupal-flake from $FLAKE_SOURCE..."
nix flake init -t "$FLAKE_SOURCE" --refresh
popd || exit 1
echo "Done!"
......@@ -8,6 +8,11 @@
default = "";
description = "The project name for MySQL socket path";
};
dbSocket = lib.mkOption {
type = lib.types.str;
default = "data/db/mysql.sock";
description = "The MySQL socket path";
};
domain = lib.mkOption {
type = lib.types.str;
default = "";
......@@ -30,7 +35,7 @@
outputs.settings = {
processes.${name} = {
command = ''
${pkgs.writeScript "nix-settings" (builtins.readFile ./bin/nix-settings)} ${config.projectName} ${config.docroot}
${pkgs.writeScript "nix-settings" (builtins.readFile ./bin/nix-settings)} ${config.projectName} ${config.docroot} ${config.dbSocket}
'';
};
};
......
......@@ -26,6 +26,7 @@ let
xdebug.discover_client_host = yes
xdebug.max_nesting_level = 512
xdebug.log = /tmp/xdebug.log
mysqli.default_socket = ${config.dbSocket}
'';
});
......@@ -69,6 +70,11 @@ in
default = "php83";
description = "PHP version to use (php74, php80, php81, php82, php83, php84)";
};
dbSocket = lib.mkOption {
type = lib.types.str;
default = "data/db/mysql.sock";
description = "The MySQL socket path";
};
};
config = {
......@@ -79,7 +85,7 @@ in
mkdir -p ${config.dataDir}
${phpEnv}/bin/php-fpm --nodaemonize -p ${config.dataDir} --fpm-config ${phpfpmConfig}
'';
# To support project browser/auto updates, need path to composer and rsync.
# To support project browser/auto updates, need path to composer and rsync.
environment = [
"PATH=${phpEnv}/bin:${pkgs.coreutils}/bin:${pkgs.rsync}/bin:${pkgs.bash}/bin"
];
......
......@@ -54,6 +54,7 @@
phpVersion = getEnvWithDefault "PHP_VERSION" "php83";
drupalPackage = getEnvWithDefault "DRUPAL_PACKAGE" "drupal/cms";
docroot = getEnvWithDefault "DOCROOT" "web";
dbSocket = getEnvWithDefault "DB_SOCKET" "data/${projectName}-db/mysql.sock";
inherit (inputs.services-flake.lib) multiService;
......@@ -87,6 +88,7 @@
enable = true;
# Override PHP version:
phpVersion = phpVersion;
dbSocket = dbSocket;
};
# Create log dir
settings.processes.setupNginx = {
......@@ -97,8 +99,8 @@
services.nginx."${projectName}-nginx" = {
enable = true;
# Without this Nginx always claims port 8080, and can't be started elsewhere.
port = lib.strings.toInt port;
# Without this Nginx always claims port 8080, and can't be started elsewhere.
port = lib.strings.toInt port;
# Override domain:
httpConfig = ''
server {
......@@ -125,9 +127,9 @@
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
# Set read timeout to an hour, for debugging
# Set read timeout to an hour, for debugging
fastcgi_read_timeout 3600;
# Drupal sends big headers in dev mode, need to increase buffer size
# Drupal sends big headers in dev mode, need to increase buffer size
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
......@@ -165,6 +167,8 @@
port = port;
# Set the docroot
docroot = docroot;
# Set the MySQL socket path
dbSocket = dbSocket;
};
......@@ -235,13 +239,27 @@
-d xdebug.client_port=9003 \
$PROJECT_ROOT/vendor/bin/drush.php "$@"
'')
(writeScriptBin "?" ''
#!${pkgs.bash}/bin/bash
echo -e "\n\033[1;34m${projectName} Development Commands:\033[0m"
echo -e "\033[1;32mnix run\033[0m Start the development environment"
echo -e "\033[1;32mnix run .#demo\033[0m Set up a new Drupal site, or start servers"
echo -e "\033[1;32mdemo\033[0m Set up a new Drupal site, or start servers"
echo -e "\033[1;32mxdrush\033[0m Run Drush with Xdebug enabled"
echo -e "\033[1;32mnix-settings\033[0m Add/include settings.nix.php"
echo -e "\033[1;32mrefresh-flake [path]\033[0m Refresh the flake from Drupal.org or [path]"
echo -e "\033[1;32m?\033[0m Show this help message"
echo ""
echo -e "Site URL: \033[1;33mhttp://${domain}:${port}\033[0m"
'')
];
DRUSH_OPTIONS_URI = "http://${domain}:${port}";
shellHook = ''
export PROJECT_ROOT="$PWD"
export DB_SOCKET="$PWD/${dbSocket}"
echo "Entering development environment for ${projectName}"
echo "Use nix run to start everything up, and then use a different shell for management. You can import a database using drush sqlc < db.sql"
echo "Use '?' to see the commands provided in this flake."
'';
};
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment