     ; 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)

(defun hsenv-read-file-content (hsenv-dir file)
  (with-temp-buffer
    (insert-file-contents (concat hsenv-dir file))
    (buffer-string)))

(defun hsenv-activate-environment (hsenv-dir)
  "Activate the Virtual Haskell Environment in directory HSENV-DIR"
  (when (and (hsenv-valid-dirp hsenv-dir)
             (hsenv-is-not-active))
    ; Create an hsenv active environment and backup paths
    (setq hsenv-active-environment (list `(path-backup . ,(getenv "PATH"))
                                         `(exec-path-backup . ,exec-path)
                                         `(dir . ,hsenv-dir)))
    ; Prepend paths
    (let* ((path-prepend (hsenv-read-file-content hsenv-dir
                                                  hsenv-path-prepend-file)))
      (setenv "PATH" (concat  path-prepend ":" (getenv "PATH")))
      (setq exec-path (append (split-string path-prepend ":") exec-path)))
    ; Set ghc-package
    (setenv "GHC_PACKAGE_PATH"
            (hsenv-read-file-content hsenv-dir hsenv-ghc-package-path-file))
    (message "Environment activated: %s" hsenv-dir)))

(defun hsenv-env-name-from-dir (directory)
  "Return the name of an environment based on DIRECTORY."
  (let ((offs (string-match "[.]hsenv_\\([^\\/]*\\)$" directory)))
    (cond
     (offs
      (substring directory (+ 7 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"))


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

(defun hsenv-compare-ghc-version (version-string &optional threshold)
  (save-match-data
    (when (string-match "\\(\\([0-9]+\\.?\\)+\\)$" version-string)
      (let* ((threshold (or threshold (list 7 6 1)))
             (version (match-string 1 version-string))
             (version-numbers
              (mapcar #'string-to-number (split-string version "\\."))))
        (block nil
            (mapcar* #'(lambda (v1 v2)
                         (when (< v1 v2)
                           (return 'lt))
                         (when (> v1 v2)
                           (return 'gt)))
                     version-numbers
                     threshold)
            'eq)))))

(defun hsenv-select-opt-suffix ()
  (let ((cmp-result (hsenv-compare-ghc-version (shell-command-to-string "ghc --version"))))
    (unless cmp-result
      (error "Cannot get GHC version"))
    (if (eq 'lt cmp-result)
        "conf"
      "db")))

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