reddit / emacs

Proper use of `(package-intialize)` and `package` in init files

nonreligious · 2022-08-14 08:46
I've been using Emacs for a couple of years, slowly learning and tuning up my config. I was trying to fix a bug using `emacs -Q` and then evaluating a minimal elisp block to load the locations of installed packages, but I found I was getting an error when evaluating `(package-initialize)`. &nbsp; I'm now on Emacs 27.2, and I've found out that apparently `(package-initialize)` isn't required for `v>27.0`. If that's the case, my first `config.org` block for packages must be a little incorrect. Here it is: &nbsp; (require 'package) (let* ((no-ssl (and (memq system-type '(windows-nt ms-dos)) (not (gnutls-available-p)))) (proto (if no-ssl "http" "https"))) (when no-ssl (warn "\ Your version of Emacs does not support SSL connections, which is unsafe because it allows man-in-the-middle attacks. There are two things you can do about this warning: 1. Install an Emacs version that does support SSL and be safe. 2. Remove this warning from your init file so you won't see it again.")) ;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired (add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t) (add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t) (when (< emacs-major-version 24) ;; For important compatibility libraries like cl-lib (add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/"))))) (setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3") (package-initialize) ;; Load paths to various packages and themes (add-to-list 'load-path "~/.emacs.d/org-9.1.14/lisp/") (add-to-list 'load-path "~/.emacs.d/org-9.1.14/contrib/lisp" t) (add-to-list 'custom-theme-load-path "~/.emacs.d/themes/") (add-to-list 'load-path "~/.emacs.d/vendor/emacs-powerline") &nbsp; Is the `(package-initialize)` redundant here? Is the rest more-or-less appropriate? &nbsp; I actually handle almost all my packages with `use-package`, but I don't have a `(require 'use-package)` line, presumably because it isn't mentioned on the [github README](https://github.com/jwiegley/use-package#installing-use-package).
arthurno1 · 2022-08-14 20:17
You don't need to use package-initialize anywhere. You are probably looking at some old configs or blog posts and tutorials. Things have changed, since Emacs 27 or so, at least I was told so bu Monnier some time ago on emacs mailing list (you will have to search the mail list for yourself, I am posting from my phone). I haven't looked at your code, so can't tell you what you do wrong, but you can skip package-initialize completely.
vifon · 2022-08-14 23:54
Since Emacs 27 the part before `(package-initialize)` goes into `early-init.el` and the rest goes into `init.el` as usual.
nonreligious · 2022-08-15 05:48
Thanks, indeed I am using the same config from a few years ago, and missed the significance of this change.
nonreligious · 2022-08-15 05:53
Thanks. I recently switched to a rudimentary literate configuration that basically tangles my `config.org` file to `config.el` and loads it from `init.el`. I've seem some more complicated versions which tangle the package related stuff to `early-init.el`, I suppose I'll have to try it out.
[deleted] · 2022-08-16 07:19
[deleted]