1{ config, lib, pkgs, serverInfo, php, ... }: 2# http://codex.wordpress.org/Hardening_WordPress 3 4with lib; 5 6let 7 8 version = "4.2"; 9 fullversion = "${version}.2"; 10 11 # Our bare-bones wp-config.php file using the above settings 12 wordpressConfig = pkgs.writeText "wp-config.php" '' 13 <?php 14 define('DB_NAME', '${config.dbName}'); 15 define('DB_USER', '${config.dbUser}'); 16 define('DB_PASSWORD', '${config.dbPassword}'); 17 define('DB_HOST', '${config.dbHost}'); 18 define('DB_CHARSET', 'utf8'); 19 $table_prefix = '${config.tablePrefix}'; 20 if ( !defined('ABSPATH') ) 21 define('ABSPATH', dirname(__FILE__) . '/'); 22 require_once(ABSPATH . 'wp-settings.php'); 23 ${config.extraConfig} 24 ''; 25 26 # .htaccess to support pretty URLs 27 htaccess = pkgs.writeText "htaccess" '' 28 <IfModule mod_rewrite.c> 29 RewriteEngine On 30 RewriteBase / 31 RewriteRule ^index\.php$ - [L] 32 33 # add a trailing slash to /wp-admin 34 RewriteRule ^wp-admin$ wp-admin/ [R=301,L] 35 36 RewriteCond %{REQUEST_FILENAME} -f [OR] 37 RewriteCond %{REQUEST_FILENAME} -d 38 RewriteRule ^ - [L] 39 RewriteRule ^(wp-(content|admin|includes).*) $1 [L] 40 RewriteRule ^(.*\.php)$ $1 [L] 41 RewriteRule . index.php [L] 42 </IfModule> 43 ''; 44 45 # WP translation can be found here: 46 # https://github.com/nixcloud/wordpress-translations 47 supportedLanguages = { 48 en_GB = { revision="d6c005372a5318fd758b710b77a800c86518be13"; sha256="0qbbsi87k47q4rgczxx541xz4z4f4fr49hw4lnaxkdsf5maz8p9p"; }; 49 de_DE = { revision="3c62955c27baaae98fd99feb35593d46562f4736"; sha256="1shndgd11dk836dakrjlg2arwv08vqx6j4xjh4jshvwmjab6ng6p"; }; 50 zh_ZN = { revision="12b9f811e8cae4b6ee41de343d35deb0a8fdda6d"; sha256="1339ggsxh0g6lab37jmfxicsax4h702rc3fsvv5azs7mcznvwh47"; }; 51 fr_FR = { revision="688c8b1543e3d38d9e8f57e0a6f2a2c3c8b588bd"; sha256="1j41iak0i6k7a4wzyav0yrllkdjjskvs45w53db8vfm8phq1n014"; }; 52 }; 53 54 downloadLanguagePack = language: revision: sha256s: 55 pkgs.stdenv.mkDerivation rec { 56 name = "wp_${language}"; 57 src = pkgs.fetchFromGitHub { 58 owner = "nixcloud"; 59 repo = "wordpress-translations"; 60 rev = revision; 61 sha256 = sha256s; 62 }; 63 installPhase = "mkdir -p $out; cp -R * $out/"; 64 }; 65 66 selectedLanguages = map (lang: downloadLanguagePack lang supportedLanguages.${lang}.revision supportedLanguages.${lang}.sha256) (config.languages); 67 68 # The wordpress package itself 69 wordpressRoot = pkgs.stdenv.mkDerivation rec { 70 name = "wordpress"; 71 src = pkgs.fetchFromGitHub { 72 owner = "WordPress"; 73 repo = "WordPress"; 74 rev = "${fullversion}"; 75 sha256 = "0gq1j9b0d0rykql3jzdb2yn4adj0rrcsvqrmj3dzx11ir57ilsgc"; 76 }; 77 installPhase = '' 78 mkdir -p $out 79 # copy all the wordpress files we downloaded 80 cp -R * $out/ 81 82 # symlink the wordpress config 83 ln -s ${wordpressConfig} $out/wp-config.php 84 # symlink custom .htaccess 85 ln -s ${htaccess} $out/.htaccess 86 # symlink uploads directory 87 ln -s ${config.wordpressUploads} $out/wp-content/uploads 88 89 # remove bundled plugins(s) coming with wordpress 90 rm -Rf $out/wp-content/plugins/* 91 # remove bundled themes(s) coming with wordpress 92 rm -Rf $out/wp-content/themes/* 93 94 # symlink additional theme(s) 95 ${concatMapStrings (theme: "ln -s ${theme} $out/wp-content/themes/${theme.name}\n") config.themes} 96 # symlink additional plugin(s) 97 ${concatMapStrings (plugin: "ln -s ${plugin} $out/wp-content/plugins/${plugin.name}\n") (config.plugins) } 98 99 # symlink additional translation(s) 100 mkdir -p $out/wp-content/languages 101 ${concatMapStrings (language: "ln -s ${language}/*.mo ${language}/*.po $out/wp-content/languages/\n") (selectedLanguages) } 102 ''; 103 }; 104 105in 106 107{ 108 109 # And some httpd extraConfig to make things work nicely 110 extraConfig = '' 111 <Directory ${wordpressRoot}> 112 DirectoryIndex index.php 113 Allow from * 114 Options FollowSymLinks 115 AllowOverride All 116 </Directory> 117 ''; 118 119 enablePHP = true; 120 121 options = { 122 dbHost = mkOption { 123 default = "localhost"; 124 description = "The location of the database server."; 125 example = "localhost"; 126 }; 127 dbName = mkOption { 128 default = "wordpress"; 129 description = "Name of the database that holds the Wordpress data."; 130 example = "localhost"; 131 }; 132 dbUser = mkOption { 133 default = "wordpress"; 134 description = "The dbUser, read: the username, for the database."; 135 example = "wordpress"; 136 }; 137 dbPassword = mkOption { 138 default = "wordpress"; 139 description = "The mysql password to the respective dbUser."; 140 example = "wordpress"; 141 }; 142 tablePrefix = mkOption { 143 default = "wp_"; 144 description = '' 145 The $table_prefix is the value placed in the front of your database tables. Change the value if you want to use something other than wp_ for your database prefix. Typically this is changed if you are installing multiple WordPress blogs in the same database. See <link xlink:href='http://codex.wordpress.org/Editing_wp-config.php#table_prefix'/>. 146 ''; 147 }; 148 wordpressUploads = mkOption { 149 default = "/data/uploads"; 150 description = '' 151 This directory is used for uploads of pictures and must be accessible (read: owned) by the httpd running user. The directory passed here is automatically created and permissions are given to the httpd running user. 152 ''; 153 }; 154 plugins = mkOption { 155 default = []; 156 type = types.listOf types.path; 157 description = 158 '' 159 List of path(s) to respective plugin(s) which are symlinked from the 'plugins' directory. Note: These plugins need to be packaged before use, see example. 160 ''; 161 example = '' 162 # Wordpress plugin 'akismet' installation example 163 akismetPlugin = pkgs.stdenv.mkDerivation { 164 name = "akismet-plugin"; 165 # Download the theme from the wordpress site 166 src = pkgs.fetchurl { 167 url = https://downloads.wordpress.org/plugin/akismet.3.1.zip; 168 sha256 = "1i4k7qyzna08822ncaz5l00wwxkwcdg4j9h3z2g0ay23q640pclg"; 169 }; 170 # We need unzip to build this package 171 buildInputs = [ pkgs.unzip ]; 172 # Installing simply means copying all files to the output directory 173 installPhase = "mkdir -p $out; cp -R * $out/"; 174 }; 175 176 And then pass this theme to the themes list like this: 177 plugins = [ akismetPlugin ]; 178 ''; 179 }; 180 themes = mkOption { 181 default = []; 182 type = types.listOf types.path; 183 description = 184 '' 185 List of path(s) to respective theme(s) which are symlinked from the 'theme' directory. Note: These themes need to be packaged before use, see example. 186 ''; 187 example = '' 188 # For shits and giggles, let's package the responsive theme 189 responsiveTheme = pkgs.stdenv.mkDerivation { 190 name = "responsive-theme"; 191 # Download the theme from the wordpress site 192 src = pkgs.fetchurl { 193 url = http://wordpress.org/themes/download/responsive.1.9.7.6.zip; 194 sha256 = "06i26xlc5kdnx903b1gfvnysx49fb4kh4pixn89qii3a30fgd8r8"; 195 }; 196 # We need unzip to build this package 197 buildInputs = [ pkgs.unzip ]; 198 # Installing simply means copying all files to the output directory 199 installPhase = "mkdir -p $out; cp -R * $out/"; 200 }; 201 202 And then pass this theme to the themes list like this: 203 themes = [ responsiveTheme ]; 204 ''; 205 }; 206 languages = mkOption { 207 default = []; 208 description = "Installs wordpress language packs based on the list, see wordpress.nix for possible translations."; 209 example = "[ \"en_GB\" \"de_DE\" ];"; 210 }; 211 extraConfig = mkOption { 212 default = ""; 213 example = 214 '' 215 define( 'AUTOSAVE_INTERVAL', 60 ); // Seconds 216 ''; 217 description = '' 218 Any additional text to be appended to Wordpress's wp-config.php 219 configuration file. This is a PHP script. For configuration 220 settings, see <link xlink:href='http://codex.wordpress.org/Editing_wp-config.php'/>. 221 ''; 222 }; 223 }; 224 225 documentRoot = wordpressRoot; 226 227 # FIXME adding the user has to be done manually for the time being 228 startupScript = pkgs.writeScript "init-wordpress.sh" '' 229 #!/bin/sh 230 mkdir -p ${config.wordpressUploads} 231 chown ${serverInfo.serverConfig.user} ${config.wordpressUploads} 232 233 # we should use systemd dependencies here 234 #waitForUnit("network-interfaces.target"); 235 if [ ! -d ${serverInfo.fullConfig.services.mysql.dataDir}/${config.dbName} ]; then 236 echo "Need to create the database '${config.dbName}' and grant permissions to user named '${config.dbUser}'." 237 # Wait until MySQL is up 238 while [ ! -e /var/run/mysql/mysqld.pid ]; do 239 sleep 1 240 done 241 ${pkgs.mysql}/bin/mysql -e 'CREATE DATABASE ${config.dbName};' 242 ${pkgs.mysql}/bin/mysql -e 'GRANT ALL ON ${config.dbName}.* TO ${config.dbUser}@localhost IDENTIFIED BY "${config.dbPassword}";' 243 else 244 echo "Good, no need to do anything database related." 245 fi 246 ''; 247}