Subject: [PATCH 1/2] services: mysql: Fix missing modules on activation.

From: Christopher Baines <[email-masked]>
Date: Sat, 9 Sep 2017 15:53:42 +0100

Some systems using the MySQL service would fail to boot, giving the error:

  ERROR: no code for module (ice-9 popen)

* gnu/services/databases.scm (%mysql-activation): Wrap the gexp using
  with-imported-modules, to ensure that the required modules are available.
---
 gnu/services/databases.scm | 92 +++++++++++++++++++++++-----------------------
 1 file changed, 47 insertions(+), 45 deletions(-)

diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index de1f6b841..6b4e6e706 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -297,56 +297,58 @@ port=" (number->string port) "
   "Return an activation gexp for the MySQL or MariaDB database server."
   (let ((mysql  (mysql-configuration-mysql config))
         (my.cnf (mysql-configuration-file config)))
-    #~(begin
-        (use-modules (ice-9 popen)
-                     (guix build utils))
-        (let* ((mysqld  (string-append #$mysql "/bin/mysqld"))
-               (user    (getpwnam "mysql"))
-               (uid     (passwd:uid user))
-               (gid     (passwd:gid user))
-               (datadir "/var/lib/mysql")
-               (rundir  "/run/mysqld"))
-          (mkdir-p datadir)
-          (chown datadir uid gid)
-          (mkdir-p rundir)
-          (chown rundir uid gid)
-          ;; Initialize the database when it doesn't exist.
-          (when (not (file-exists? (string-append datadir "/mysql")))
-            (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
-                ;; For MySQL.
-                (system* mysqld
-                         (string-append "--defaults-file=" #$my.cnf)
-                         "--initialize"
-                         "--user=mysql")
-                ;; For MariaDB.
-                ;; XXX: The 'mysql_install_db' script doesn't work directly
-                ;;      due to missing 'mkdir' in PATH.
-                (let ((p (open-pipe* OPEN_WRITE mysqld
-                                     (string-append
-                                      "--defaults-file=" #$my.cnf)
-                                     "--bootstrap"
-                                     "--user=mysql")))
-                  ;; Create the system database, as does by 'mysql_install_db'.
-                  (display "create database mysql;\n" p)
-                  (display "use mysql;\n" p)
-                  (for-each
-                   (lambda (sql)
-                     (call-with-input-file
-                         (string-append #$mysql "/share/mysql/" sql)
-                       (lambda (in) (dump-port in p))))
-                   '("mysql_system_tables.sql"
-                     "mysql_performance_tables.sql"
-                     "mysql_system_tables_data.sql"
-                     "fill_help_tables.sql"))
-                  ;; Remove the anonymous user and disable root access from
-                  ;; remote machines, as does by 'mysql_secure_installation'.
-                  (display "
+    (with-imported-modules '((ice-9 popen)
+                             (guix build utils))
+      #~(begin
+          (use-modules (ice-9 popen)
+                       (guix build utils))
+          (let* ((mysqld  (string-append #$mysql "/bin/mysqld"))
+                 (user    (getpwnam "mysql"))
+                 (uid     (passwd:uid user))
+                 (gid     (passwd:gid user))
+                 (datadir "/var/lib/mysql")
+                 (rundir  "/run/mysqld"))
+            (mkdir-p datadir)
+            (chown datadir uid gid)
+            (mkdir-p rundir)
+            (chown rundir uid gid)
+            ;; Initialize the database when it doesn't exist.
+            (when (not (file-exists? (string-append datadir "/mysql")))
+              (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
+                  ;; For MySQL.
+                  (system* mysqld
+                           (string-append "--defaults-file=" #$my.cnf)
+                           "--initialize"
+                           "--user=mysql")
+                  ;; For MariaDB.
+                  ;; XXX: The 'mysql_install_db' script doesn't work directly
+                  ;;      due to missing 'mkdir' in PATH.
+                  (let ((p (open-pipe* OPEN_WRITE mysqld
+                                       (string-append
+                                        "--defaults-file=" #$my.cnf)
+                                       "--bootstrap"
+                                       "--user=mysql")))
+                    ;; Create the system database, as does by 'mysql_install_db'.
+                    (display "create database mysql;\n" p)
+                    (display "use mysql;\n" p)
+                    (for-each
+                     (lambda (sql)
+                       (call-with-input-file
+                           (string-append #$mysql "/share/mysql/" sql)
+                         (lambda (in) (dump-port in p))))
+                     '("mysql_system_tables.sql"
+                       "mysql_performance_tables.sql"
+                       "mysql_system_tables_data.sql"
+                       "fill_help_tables.sql"))
+                    ;; Remove the anonymous user and disable root access from
+                    ;; remote machines, as does by 'mysql_secure_installation'.
+                    (display "
 DELETE FROM user WHERE User='';
 DELETE FROM user WHERE User='root' AND
   Host NOT IN  ('localhost', '127.0.0.1', '::1');
 FLUSH PRIVILEGES;
 " p)
-                  (close-pipe p))))))))
+                    (close-pipe p)))))))))
 
 (define (mysql-shepherd-service config)
   (list (shepherd-service
-- 
2.14.1