@@ -1,22 +1,12 @@
11{ config, lib, pkgs, serverInfo, php, ... }:
2+# http://codex.wordpress.org/Hardening_WordPress
23
34with lib;
45
56let
6− # https://wordpress.org/plugins/postgresql-for-wordpress/
7− # Wordpress plugin 'postgresql-for-wordpress' installation example
8− postgresqlForWordpressPlugin = pkgs.stdenv.mkDerivation {
9− name = "postgresql-for-wordpress-plugin";
10− # Download the theme from the wordpress site
11− src = pkgs.fetchurl {
12− url = https://downloads.wordpress.org/plugin/postgresql-for-wordpress.1.3.1.zip;
13− sha256 = "f11a5d76af884c7bec2bc653ed5bd29d3ede9a8657bd67ab7824e329e5d809e8";
14− };
15− # We need unzip to build this package
16− buildInputs = [ pkgs.unzip ];
17− # Installing simply means copying all files to the output directory
18− installPhase = "mkdir -p $out; cp -R * $out/";
19− };
7+
8+ version = "4.2";
9+ fullversion = "${version}.2";
2010
2111 # Our bare-bones wp-config.php file using the above settings
2212 wordpressConfig = pkgs.writeText "wp-config.php" ''
@@ -38,38 +28,88 @@ let
3828 <IfModule mod_rewrite.c>
3929 RewriteEngine On
4030 RewriteBase /
41− RewriteCond %{REQUEST_FILENAME} !-f
42− RewriteCond %{REQUEST_FILENAME} !-d
43− RewriteRule . /index.php [L]
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]
4442 </IfModule>
4543 '';
4644
45+ # WP translation can be found here:
46+ # https://make.wordpress.org/polyglots/teams/
47+ # FIXME:
48+ # - add all these languages:
49+ # sq ar az eu bs bg ca zh-cn zh-tw hr da nl en-au
50+ # en-ca en-gb eo fi fr gl de el he hu is id it ja
51+ # ko lt nb nn oci pl pt-br pt ro ru sr sk es-mx es
52+ # sv th tr uk cy
53+ # - cache the files on github.com/qknight/WordpressLanguages and use fetchFromGithub instead
54+ # note: this implementation of supportedLanguages will only work for me (qknight) as i'm using nix-prefetch-url
55+ # as the sha256 changes like every download.
56+ # note: this is also true for plugins and themes but these are controlled not from withing wordpress.nix
57+ supportedLanguages = {
58+ en_GB = "1yf1sb6ji3l4lg8nkkjhckbwl81jly8z93jf06pvk6a1p6bsr6l6";
59+ de_DE = "3881221f337799b88f9562df8b3f1560f2c49a8f662297561a5b25ce77f22e17";
60+ };
61+
62+ downloadLanguagePack = language: sha256:
63+ pkgs.stdenv.mkDerivation rec {
64+ name = "wp_${language}-${version}";
65+ src = pkgs.fetchurl {
66+ url = "https://downloads.wordpress.org/translation/core/${version}/${language}.zip";
67+ sha256 = "${sha256}";
68+ };
69+ buildInputs = [ pkgs.unzip ];
70+ unpackPhase = ''
71+ unzip $src
72+ export sourceRoot=.
73+ '';
74+ installPhase = "mkdir -p $out; cp -R * $out/";
75+ };
76+
77+ selectedLanguages = map (lang: downloadLanguagePack lang supportedLanguages.${lang}) (config.languages);
78+
4779 # The wordpress package itself
4880 wordpressRoot = pkgs.stdenv.mkDerivation rec {
4981 name = "wordpress";
50− # Fetch directly from the wordpress site, want to upgrade?
51− # Just change the version URL and update the hash
52− src = pkgs.fetchurl {
53− url = http://wordpress.org/wordpress-4.1.1.tar.gz;
54− sha256 = "1s9y0i9ms3m6dswb9gqrr95plnx6imahc07fyhvrp5g35f6c12k1";
82+ src = pkgs.fetchFromGitHub {
83+ owner = "WordPress";
84+ repo = "WordPress";
85+ rev = "${fullversion}";
86+ sha256 = "0gq1j9b0d0rykql3jzdb2yn4adj0rrcsvqrmj3dzx11ir57ilsgc";
5587 };
5688 installPhase = ''
5789 mkdir -p $out
58− # Copy all the wordpress files we downloaded
90+ # copy all the wordpress files we downloaded
5991 cp -R * $out/
60− # We'll symlink the wordpress config
92+
93+ # symlink the wordpress config
6194 ln -s ${wordpressConfig} $out/wp-config.php
62− # As well as our custom .htaccess
95+ # symlink custom .htaccess
6396 ln -s ${htaccess} $out/.htaccess
64− # And the uploads directory
97+ # symlink uploads directory
6598 ln -s ${config.wordpressUploads} $out/wp-content/uploads
66− # And the theme(s)
99+
100+ # remove bundled plugins(s) coming with wordpress
101+ rm -Rf $out/wp-content/plugins/*
102+ # remove bundled themes(s) coming with wordpress
103+ rm -Rf $out/wp-content/themes/*
104+
105+ # symlink additional theme(s)
67106 ${concatMapStrings (theme: "ln -s ${theme} $out/wp-content/themes/${theme.name}\n") config.themes}
68− # And the plugin(s)
69− # remove bundled plugin(s) coming with wordpress
70− rm -Rf $out/wp-content/plugins/akismet
71− # install plugins
72− ${concatMapStrings (plugin: "ln -s ${plugin} $out/wp-content/plugins/${plugin.name}\n") (config.plugins ++ [ postgresqlForWordpressPlugin]) }
107+ # symlink additional plugin(s)
108+ ${concatMapStrings (plugin: "ln -s ${plugin} $out/wp-content/plugins/${plugin.name}\n") (config.plugins) }
109+
110+ # symlink additional translation(s)
111+ mkdir -p $out/wp-content/languages
112+ ${concatMapStrings (language: "ln -s ${language}/*.mo ${language}/*.po $out/wp-content/languages/\n") (selectedLanguages) }
73113 '';
74114 };
75115
@@ -102,12 +142,12 @@ in
102142 };
103143 dbUser = mkOption {
104144 default = "wordpress";
105− description = "The dbUser, read the username, for the database.";
145+ description = "The dbUser, read: the username, for the database.";
106146 example = "wordpress";
107147 };
108148 dbPassword = mkOption {
109149 default = "wordpress";
110− description = "The password to the respective dbUser.";
150+ description = "The mysql password to the respective dbUser.";
111151 example = "wordpress";
112152 };
113153 tablePrefix = mkOption {
@@ -127,7 +167,7 @@ in
127167 type = types.listOf types.path;
128168 description =
129169 ''
130− List of path(s) to respective plugin(s) which are symlinked from the 'plugins' directory. Note: These plugins need to be packaged before use.
170+ 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.
131171 '';
132172 example = ''
133173 # Wordpress plugin 'akismet' installation example
@@ -153,7 +193,7 @@ in
153193 type = types.listOf types.path;
154194 description =
155195 ''
156− List of path(s) to respective theme(s) which are symlinked from the 'theme' directory. Note: These themes need to be packaged before use.
196+ 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.
157197 '';
158198 example = ''
159199 # For shits and giggles, let's package the responsive theme
@@ -174,6 +214,11 @@ in
174214 themes = [ responsiveTheme ];
175215 '';
176216 };
217+ languages = mkOption {
218+ default = [];
219+ description = "Installs wordpress language packs based on the list, see wordpress.nix for possible translations.";
220+ example = "[ \"en_GB\" \"de_DE\" ];";
221+ };
177222 extraConfig = mkOption {
178223 default = "";
179224 example =
@@ -198,12 +243,15 @@ in
198243 # we should use systemd dependencies here
199244 #waitForUnit("network-interfaces.target");
200245 if [ ! -d ${serverInfo.fullConfig.services.mysql.dataDir}/${config.dbName} ]; then
246+ echo "Need to create the database '${config.dbName}' and grant permissions to user named '${config.dbUser}'."
201247 # Wait until MySQL is up
202248 while [ ! -e /var/run/mysql/mysqld.pid ]; do
203249 sleep 1
204250 done
205251 ${pkgs.mysql}/bin/mysql -e 'CREATE DATABASE ${config.dbName};'
206252 ${pkgs.mysql}/bin/mysql -e 'GRANT ALL ON ${config.dbName}.* TO ${config.dbUser}@localhost IDENTIFIED BY "${config.dbPassword}";'
253+ else
254+ echo "Good, no need to do anything database related."
207255 fi
208256 '';
209257}