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

                             (asdf:system-depends-on (asdf:find-system :nyxt/gtk)))))
      "sbcl")))

(defvar nyxt-guix-profile-directory "~/.guix-temp-profiles/nyxt"
  "Default directory where to dump auto-generated Guix profiles for Nyxt development.")

(cl-defun nyxt-guix-lazy-environment (root &key preserve guix-env-args run-args)
  "Return the command to load a Guix pure environment.
If the environment already exists, don't regenerate it.

PRESERVE is a list of environment variables to preserve.

GUIX-ENV-ARGS is passed to the `guix environment', before \"--\".
GUIX-ENV-ARGS must be shell-quoted (for instance with `shell-quote-argument').

RUN-ARGS are passed after GUIX-ENV-ARGS and \"--\", or, if the
environment already exists, after sourcing \"etc/profile\"."
  (setq root (expand-file-name (or root
                                   (concat nyxt-guix-profile-directory "/nyxt"))))
  (make-directory (file-name-directory root) :parents)
  (let ((root-env (concat root "/etc/profile")))
    (list (executable-find "bash") "--norc" "--noprofile" "-c"
          (format "if [ -e '%s' ]; then %s ; else %s ; fi"
                  root-env
                  (format "source '%s' && %s" (shell-quote-argument root-env)
                          (mapconcat #'shell-quote-argument run-args " "))
                  (mapconcat #'identity (append (list "guix" "environment" "-r" (shell-quote-argument root) "--pure")
                                                (apply #'nyxt--guix-preserve-vars preserve)
                                                guix-env-args
                                                (list "--")
                                                (mapcar #'shell-quote-argument run-args)) " ")))))

(cl-defun nyxt-make-guix-sbcl-for-nyxt (nyxt-checkout
                                        &key image-path container no-grafts preserve force
                                        guix-profile-root)
  "Run an SBCL executable image with all Nyxt dependencies pre-loaded.

The image is generated as needed and cached as IMAGE-PATH.
