diff --git a/.dir-locals.el b/.dir-locals.el
index 87cdaae807b988f97232e46cf1cb71008580974e..03d9a4ec8db5d7949b0d408faaac37fddce7e255 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -18,6 +18,7 @@
    (eval . (put 'manifest-entry 'scheme-indent-function 0))
    (eval . (put 'manifest-pattern 'scheme-indent-function 0))
    (eval . (put 'substitute-keyword-arguments 'scheme-indent-function 1))
+   (eval . (put 'with-store 'scheme-indent-function 1))
    (eval . (put 'with-error-handling 'scheme-indent-function 0))
    (eval . (put 'with-mutex 'scheme-indent-function 1))
    (eval . (put 'with-atomic-file-output 'scheme-indent-function 1))
diff --git a/guix/store.scm b/guix/store.scm
index 393eee8d1bf8298355fe86f42773b426659b0ce0..ede64341c5f079bc1a8037fcf8f0695e5e017b34 100644
--- a/guix/store.scm
+++ b/guix/store.scm
@@ -53,6 +53,7 @@ (define-module (guix store)
 
             open-connection
             close-connection
+            with-store
             set-build-options
             valid-path?
             query-path-hash
@@ -323,6 +324,17 @@ (define (close-connection server)
   "Close the connection to SERVER."
   (close (nix-server-socket server)))
 
+(define-syntax-rule (with-store store exp ...)
+  "Bind STORE to an open connection to the store and evaluate EXPs;
+automatically close the store when the dynamic extent of EXP is left."
+  (let ((store (open-connection)))
+    (dynamic-wind
+      (const #f)
+      (lambda ()
+        exp ...)
+      (lambda ()
+        (false-if-exception (close-connection store))))))
+
 (define current-build-output-port
   ;; The port where build output is sent.
   (make-parameter (current-error-port)))