9

What is the usefulness of setting

(setq
 package-selected-packages
 '(auctex                 ;
   jedi                   ; 
   magit                  ;
etc))

Doesn't emacs set the user-installed packages in package-selected-packages? What would we be doing if we override this variable?

Drew
  • 77,472
  • 10
  • 114
  • 243
Emmanuel Goldstein
  • 1,014
  • 8
  • 18
  • "This variable is used by package-autoremove to decide which packages are no longer needed. You can use it to (re)install packages on other machines by running package-install-selected-packages." – phils Jun 15 '20 at 13:53
  • 1
    So what if I have other packages manually installed by setting setq outside that variable? How will they be affected? – Emmanuel Goldstein Jun 15 '20 at 19:37
  • I don't understand the question, sorry. The quoted docstring tells you what that variable does, and I assume it will have those effects regardless of how it's set. – phils Jun 15 '20 at 21:11
  • So the variable just stores packages that we always want to keep. Are these installed/updated everytime we start emacs? – Emmanuel Goldstein Jun 16 '20 at 03:02
  • No. Is there some reason why you thought that might happen? – phils Jun 16 '20 at 05:17
  • Im new in Emacs so I'm trying to understand the difference between having those packages specified in that variable or having them specified individually "setq 'nameofpackage"). – Emmanuel Goldstein Jun 16 '20 at 09:21
  • I don't know what you mean by the latter alternative. What does that look like in elisp code, and what do you think it does? (setq nameofpackage VALUE) sets a variable called nameofpackage to value VALUE. It doesn't do anything else, unless that variable has a particular purpose in other code. – phils Jun 16 '20 at 11:00
  • @phils By the latter alternative I mean having (setq nameofpackage t) instead of "(setq package-selected-packages '(list of names of package)". What would be the difference? – Emmanuel Goldstein Jun 17 '20 at 02:53
  • There's nothing similar about them. (setq nameofpackage t) won't tell the package system anything. It just sets a variable. If no other elisp uses a variable by that name, then it essentially has no effect whatsoever. If that is a variable which is used by other code (such as, but not necessarily, the package in question), then you'd have to read the docstring for that variable to learn what the effect of setting it to t would be. – phils Jun 17 '20 at 04:45
  • Setting the variable package-selected-packages has an effect because there is elisp code in the package manager which uses that variable. – phils Jun 17 '20 at 04:51

1 Answers1

8

From the variable’s documentation:

Store here packages installed explicitly by user.

Whatever you explicitly install (e.g. with the package-install command, or with a package “manager” like use-package), gets into this variable.

This variable is fed automatically by Emacs when installing a new package.

So it happens automatically! In fact, it gets saved to your custom-file if set via customize (which package.el does for you).

Still from the documentation (also mentioned by @phils in a comment)

This variable is used by package-autoremove to decide which packages are no longer needed.

Whenever you issue the package-autoremove command it consults this variable. package-autoremove will remove everything that is

  • not marked as explicitly installed (ie. not in this list), and
  • not a dependency of an explicitly installed package.

The documentation also tells another use-case:

You can use it to (re)install packages on other machines by running package-install-selected-packages.

So simply populate this variable manually, and use that command to install your favourite packages on all your machines.


To have a better picture, go to the package manager using list-packages. You will see a bunch of

  • available packages; these are available in the configured repositories (see package-archives), but not installed on your system
  • installed packages; these are what you installed explicitly. This list is exactly the same as your package-selected-packages.
  • dependency packages; these are packages that are installed on your system, but that are not in package-selected-packages. Under normal circumstances these are installed as dependencies of other packages
  • built-in packages; these are shipped with your Emacs installation and always available. You can’t remove them.
  • obsolete packages; these are packages you have installed, but there is a newer version available
  • incompat packages are packages that can’t be installed on your system. The reason is always stated by describe-package, e.g. “because it depends on Emacs 27” or “because it depends on uninstallable packages”.

For (much) more info, see the emacs(Packages) info node.

GergelyPolonkai
  • 758
  • 6
  • 12
  • Thanks, so clear and extensive. I primarily asked because, even if the packages I install get copied into that variable in custom.el, I have "setq package-selected-packages" in my other init file which is run the last when loading emacs. So any user could tailor this list in case we don't want all packages we have at the moment (normally I install many packages per day just to test and forget about them, so here the idea is that I should keep the ones relevant). Does this make sense? – Emmanuel Goldstein Jun 19 '20 at 02:51
  • Actually, whatever works for you. ☺ I add packages i want to test to my config file with use-package, and if i donʼt need them, i remove them from the config. I never set package.el variables directly anywhere. – GergelyPolonkai Jun 19 '20 at 15:22