     ; Restore paths
     (setenv "PATH" (assoc-default 'path-backup hsenv-active-environment))
     (setq exec-path (assoc-default 'exec-path-backup hsenv-active-environment))
+    ; Unset variables
+    (setenv "PACKAGE_DB_FOR_GHC")
+    (setenv "PACKAGE_DB_FOR_GHC_PKG")
+    (setenv "PACKAGE_DB_FOR_GHC_MOD")
+    (setenv "PACKAGE_DB_FOR_CABAL")
+    (setenv "HSENV")
+    (setenv "HSENV_NAME")
+    (setenv "HASKELL_PACKAGE_SANDBOX")
     ; Destroy the hsenv active environment
     (let ((old-dir (cdr (assoc 'dir hsenv-active-environment))))
       (setq hsenv-active-environment nil)


(defconst hsenv-path-prepend-file "path_var_prependix")
(defconst hsenv-ghc-package-path-file "ghc_package_path_var")

(defun hsenv-valid-dirp (hsenv-dir)
  (let ((valid (and (file-accessible-directory-p hsenv-dir)

(defun hsenv-read-file-content (hsenv-dir file)
  (with-temp-buffer
    (insert-file-contents (concat hsenv-dir file))
    (replace-regexp-in-string "\n+$" "" (buffer-string))))

(defun hsenv-replace-pkg (template package-dbs)
  (apply #'concat
         (mapcar #'(lambda (db)
                     (concat template db))
                 package-dbs)))

(defun hsenv-activate-environment (hsenv-dir env env-name)
  "Activate the Virtual Haskell Environment in directory HSENV-DIR"
  (when (and (hsenv-valid-dirp hsenv-dir)
             (hsenv-is-not-active))

    ; Prepend paths
    (let* ((new-hsenv-active-environment (list `(path-backup . ,(getenv "PATH"))
                                               `(exec-path-backup . ,exec-path)
                                               `(dir . ,hsenv-dir)))
           (path-prepend (hsenv-read-file-content hsenv-dir
                                                  hsenv-path-prepend-file))
           (package-db (hsenv-read-file-content hsenv-dir hsenv-ghc-package-path-file))
           (package-dbs (split-string package-db ":"))
           (suffix (hsenv-select-opt-suffix)))
      (setenv "PATH" (concat  path-prepend ":" (getenv "PATH")))
      (setq exec-path (append (split-string path-prepend ":") exec-path))
      (setenv "PACKAGE_DB_FOR_GHC"
              (concat "-no-user-package-" suffix
                      (hsenv-replace-pkg (concat " -package-" suffix "=") package-dbs)))
      (setenv "PACKAGE_DB_FOR_CABAL"
              (hsenv-replace-pkg " --package-db=" package-dbs))
      (setenv "PACKAGE_DB_FOR_GHC_PKG"
              (concat "--no-user-package-" suffix
                      (hsenv-replace-pkg (concat " --package-" suffix "=") package-dbs)))
      (setenv "PACKAGE_DB_FOR_GHC_MOD"
              (concat "-g -no-user-package-" suffix
                      (hsenv-replace-pkg (concat " -g -package-" suffix "=") package-dbs)))
      (setenv "HASKELL_PACKAGE_SANDBOX" package-db)
      (setenv "HSENV" env)
      (setenv "HSENV_NAME" env-name)
      ; Save an hsenv active environment and backup paths
      (setq hsenv-active-environment new-hsenv-active-environment)
      (message "Environment activated: %s" hsenv-dir))))

(defun hsenv-env-name-from-dir (directory)
  "Return the name of an environment based on DIRECTORY."
  (save-match-data
    (let ((offs (string-match "[.]hsenv\\([^\\/]*\\)$" directory)))
      (cond
       (offs
        (substring directory (+ 6 offs)))
       ((string-match "[.]hsenv$" directory)
        "(default)")
       (t
        (error "Not an hsenv directory %s" directory))))))

;;; Tests:
;; (and (equal "foo" (hsenv-env-name-from-dir "/home/bar/baz/.hsenv_foo"))
