I want to declare all packages that I want to use in emacs in a init.el file. I wonder if its possible to load the missing packages with e.g. MELPA when I startup e
I liked @steckerhalter's answer because it doesn't require any package installation. I'm building upon it here with the following improvements:
looking at package-selected-packages variables so you don't have to specify the packages manually. This variable is populated by custom-set-variables when you install a package via the package manager.
do the network check only if there are packages not installed.
(let ((not-installed-packages
(seq-remove 'package-installed-p package-selected-packages)))
(when (consp not-installed-packages)
(setq my-onlinep nil)
(unless
(condition-case nil
(delete-process
(make-network-process
:name "my-check-internet"
:host "elpa.gnu.org"
:service 80))
(error t))
(setq my-onlinep t))
(when my-onlinep
(package-refresh-contents)
(dolist (p not-installed-packages)
(package-install p)))
(let ((still-not-installed-packages
(seq-remove 'package-installed-p package-selected-packages)))
(if (consp still-not-installed-packages)
(message "Still not installed: %s" still-not-installed-packages)
(message "Successfully installed: %s" not-installed-packages)))
))