                      "TERM"
                      "USER")
                    preserve-vars))))
+
+(defun nyxt--guix-preserve-vars (&rest preserve-vars)
+  "Return the arguments to pass to `guix environment' to preserve PRESERVE-VARS."
+  (mapcar (lambda (var) (format "--preserve=^%s$" var))
+          preserve-vars))
 
 (defun nyxt-mtime (file)
   "Return modification time of FILE."


(require 'cl-lib)

(defun nyxt-pure-env (&rest preserve-vars)
  "Return a pure `env' command as a list of string."
  (append '("env" "-i")
          (mapcar (lambda (var) (concat var "=" (getenv var)))

  (setq image-path (expand-file-name
                    (or image-path
                        (concat (nyxt-cache-dir) "/sbcl-nyxt.image"))))
  (let ((guix-def (concat nyxt-checkout "/build-scripts/guix.scm")))
    (when (or force
              (not (file-exists-p image-path))
              (time-less-p (nyxt-mtime image-path) (nyxt-mtime guix-def)))
      (message "Dumping Nyxt-optimized SBCL at %S..." image-path)
      (ignore-errors (delete-file image-path))
      (make-directory (file-name-directory image-path) :parents)
      ;; TODO: Can we reuse the same Guix environment?  Would probably be
      ;; faster, but then containerizing is harder.
      (let* ((output (get-buffer-create " *Guix SBCL for Nyxt*"))
             (status (apply #'call-process
                            "guix"
                            nil (list output t) nil
                            "environment"
                            `(,@(when no-grafts
                                  '("--no-grafts"))
                              "-l" ,guix-def
                              ,@(if container
                                    (list
                                     "--container"
                                     (concat "--share=" (file-name-directory image-path))
                                     (concat "--share=" nyxt-checkout "=/nyxt"))
                                  '("--pure"))
                              "--ad-hoc" "lisp-repl-core-dumper"
                              "--"
                              "sbcl" "--noinform" "--no-userinit" "--eval" "(require :asdf)"
                              "--eval" ,(prin1-to-string (nyxt--cl-dump-call image-path))
                              "--quit"))))
        (if (= status)
            (kill-buffer output)
          (error "Guix environment failed, see %s" output)
          (switch-to-buffer-other-window output))))
    (list (nyxt-guix-lazy-environment
           guix-profile-root
           :preserve preserve
           :guix-env-args (list "-l" (shell-quote-argument guix-def) "--ad-hoc" "gnupg")
           :run-args (list image-path)))))

(provide 'nyxt-guix)
