Download packages without fetching repositories when using Straight*

2022-07-02T02:53:13+0900: Caveat: dependency packages will be installed twice; which one gets loaded will be up to whether Straight or Package added it to load-path first. This is why no one really recommends this, and I didn't keep this in mind when I wrote this.)

straight.el is awesome as it clones the source repositories of each package being installed. This makes it easy to install packages not submitted to a package archive, make changes and perhaps contribute to packages, etc.

However, sometimes this is not desirable and you really just want what the package archives offer, without cloning repositories: for example, you might be installing packages like protobuf-mode which live in a much larger repository and would take ages to download, even with shallow clone enabled via setting straight-vc-git-default-clone-depth to 1.

Solution? Just use package.el together with Straight!

Seriously, this actually works.

After Straight is set up (and with straight-package-neutering-mode still active), this just works:

(require 'package)
(setq package-archives
      '(("gnu" . "https://elpa.gnu.org/packages/")
        ("nongnu" . "https://elpa.nongnu.org/nongnu/")
        ("melpa" . "https://melpa.org/packages/")))
(package-read-all-archive-contents)
(unless package-archive-contents
  (package-refresh-contents))

(package-install 'corfu-terminal)
(package-activate 'corfu-terminal)

(require 'straight)
(straight-use-package 'corfu)

You may need to clean ~/.emacs.d/elpa from time to time, and of course your init file stops being purely-functional when you do this, so it's up to you to decide which tradeoff is worth it.

A full example

This is a minimal init file that sets up Corfu and corfu-terminal. Corfu is installed with Straight, while corfu-terminal is installed with package.el.

package.el might automatically activate everything in ~/.emacs.d/elpa. You can set package-enable-at-startup to nil in early-init.el to avoid this, or clean that folder up manually when the automatic activation causes problems.

(setq straight-vc-git-default-clone-depth 1)

(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(require 'package)
(setq package-archives
      '(("gnu" . "https://elpa.gnu.org/packages/")
        ("nongnu" . "https://elpa.nongnu.org/nongnu/")
        ("melpa" . "https://melpa.org/packages/")))
(package-read-all-archive-contents)
(unless package-archive-contents
  (package-refresh-contents))

(package-install 'corfu-terminal)
(package-activate 'corfu-terminal)

(require 'straight)
(straight-use-package 'corfu)

(global-corfu-mode)
(corfu-terminal-mode)
(setq corfu-auto t)
(setq tab-always-indent 'complete)
(setq-default major-mode 'emacs-lisp-mode)
(setq initial-buffer-choice t)