0

I am using Emacs 29.1, which ships with use-package, and I want to install and load the MELPA bm package through the init file.

I am using the following test-init.el init file:

(defun my-packages ()
  (require 'package)
  (add-to-list 'package-archives '("gnu"   . "https://elpa.gnu.org/packages/"))
  (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
  (package-initialize)
  (require 'use-package)
  (setq use-package-always-ensure t)
  (use-package bm))

(my-packages)

and I start Emacs (using the new --init-directory switch) with:

/usr/bin/rm -r /tmp/emadir/*; emacs --init-directory /tmp/emadir -q --load /tmp/test-init.el

That is supposed to start Emacs with test-init.el and using /tmp/emadir as init directory, after emptying it. Emacs gives the following error:

⛔ Error (use-package): Cannot load bm

A possible workaround is to move the (use-package ...) expression at top level, that is:

(defun my-packages ()
  (require 'package)
  (add-to-list 'package-archives '("gnu"   . "https://elpa.gnu.org/packages/"))
  (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
  (package-initialize)
  (require 'use-package)
  (setq use-package-always-ensure t))

(my-packages)
(use-package bm)

That gives no errors, however, this is inconvenient for me, as my real init file is huge and I need to use functions to organise it.
What can I do to nest (use-package ...) calls inside functions?

6
  • 1
    Just adding a (package-install 'bm) just above (use-package bm) in your first block fixes it. I don't immediately understand why use-package does not pick up the (setq use-package-always-ensure t), but for sure there must be some expert here who can provide us with an explanation :) Commented Aug 18, 2023 at 15:42
  • @dalanicolai: Yes, I can use (unless (package-installed-p 'bm) (package-install 'bm)) (require 'bm), do this for every package, and put off use-package.
    – antonio
    Commented Aug 18, 2023 at 17:02
  • 1
    I am not sure why you'd need to 'initialize' use-package from a function. If you take all those lines out of the my-package function, except for the (use-package bm) then it will also work... Commented Aug 18, 2023 at 21:30
  • @dalanicolai: As I wrote, my init file is gigantic, to make it manageable code is grouped in functions. So there is a function tasked with loading and configuring stable packages.
    – antonio
    Commented Aug 18, 2023 at 22:25
  • it's not manageable if you can't debug it.
    – nega
    Commented Aug 19, 2023 at 4:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.