             (delq nil
                   (mapcar
                    (lambda (x)
-                     (and (memq x configuration-layer--used-distant-packages)
-                          (configuration-layer/get-package x)))
-                   (oref layer :packages))))
-           (config-pkgs
-            (delq nil (mapcar
-                       (lambda (x)
-                         (let ((pkg (configuration-layer/get-package x)))
-                           (cfgl-package-set-property pkg :lazy-install nil)
-                           pkg))
-                       (oref layer :packages)))))
+                     (let* ((pkg-name (if (listp x) (car x) x))
+                            (pkg (configuration-layer/get-package pkg-name)))
+                       (cfgl-package-set-property pkg :lazy-install nil)
+                       (when (memq pkg-name
+                                   configuration-layer--used-distant-packages)
+                         pkg-name)))
+                   (oref layer :packages)))))
       (let ((last-buffer (current-buffer))
-            (sorted-inst (configuration-layer//sort-packages inst-pkgs))
-            (sorted-config (configuration-layer//sort-packages config-pkgs)))
+            (sorted-pkg (configuration-layer//sort-packages inst-pkgs)))
         (spacemacs-buffer/goto-buffer)
         (goto-char (point-max))
+        (configuration-layer//install-packages sorted-pkg)
+        (configuration-layer//configure-packages sorted-pkg)
+        (configuration-layer//load-layer-files layer '("keybindings.el"))
         (oset layer :lazy-install nil)
-        (configuration-layer//install-packages sorted-inst)
-        (configuration-layer//configure-packages sorted-config)
-        (configuration-layer//load-layer-files layer '("keybindings.el"))
         (switch-to-buffer last-buffer)))))
 
 (defun configuration-layer//install-packages (packages)

    (let ((extensions (spacemacs/mplist-get props :extensions)))
      (when (configuration-layer/layer-usedp layer-name)
        (let* ((layer (configuration-layer/get-layer layer-name))
               (packages (when layer (cfgl-layer-owned-packages layer))))
          ;; set lazy install flag for a layer if and only if its owned
          ;; distant packages are all not already installed
          (let ((lazy (cl-reduce
                       (lambda (x y) (and x y))
                       (mapcar (lambda (p)
                                 (or (not (eq layer-name (car (oref p :owners))))
                                     (null (package-installed-p
                                            (oref p :name)))))
                               (configuration-layer//get-distant-packages
                                packages t))
                       :initial-value t)))
            (oset layer :lazy-install lazy)
            (dolist (pkg packages)

(defun configuration-layer//auto-mode (layer-name mode)
  "Auto mode support of lazily installed layers."
  (let ((layer (configuration-layer/get-layer layer-name)))
    (when (or (oref layer :lazy-install)
              (not (configuration-layer/layer-usedp layer-name)))
      (configuration-layer//lazy-install-packages layer-name mode)))
  (when (fboundp mode) (funcall mode)))

(defun configuration-layer/filter-objects (objects ffunc)
  "Return a filtered OBJECTS list where each element satisfies FFUNC."
  (reverse (cl-reduce (lambda (acc x) (if (funcall ffunc x) (push x acc) acc))
                      objects
                      :initial-value nil)))

(defun configuration-layer//get-distant-packages (packages usedp)
  "Return the distant packages (ie to be intalled).
If USEDP is non nil then returns only the used packages; if it is nil then
return both used and unused packages."
  (configuration-layer/filter-objects
   packages
   (lambda (x)
     (let ((pkg (configuration-layer/get-package x)))
       (and (not (memq (oref pkg :location) '(built-in site local)))
            (not (stringp (oref pkg :location)))
            (or (null usedp)
                (and (not (null (oref pkg :owners)))
                     (cfgl-package-enabledp pkg)
                     (not (oref pkg :excluded)))))))))

(defun configuration-layer//get-private-layer-dir (name)
  "Return an absolute path to the private configuration layer string NAME."
