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