Skip to content

akirak/use-package-tags

Repository files navigation

:tags keyword for use-package

This library adds :tags keyword to use-package, which can be used to enable only packages that belong to certain groups. You can group packages using the keyword, and select packages to enable by setting use-package-tags-current-profile variable.

It also provides a function for enumerating packages in a file/buffer based on tags. This can be used to install packages for your init file without relying on package.el, e.g. in Nix.

CI

Table of contents

Installation

This package is not available on MELPA yet.

Usage

:tags keyword for use-package

(require 'use-package)
(use-package use-package-tags)

;; Select packages using tags
;; This can be put in your custom-file, but it should be set
;; before you load use-package forms.
(setq use-package-tags-current-profile '(work))

;; Since it has no :tags, this package is always enabled
(use-package lsp-mode)

;; This package has a tag in the current profile, so it is enabled.
(use-package lsp-java
  ;; You wouldn't want to write Java at home (unless you are working
  ;; from home), so it doesn't have to be enabled outside of work.
  :tags (work))

;; This package has no tag in the current profile, so it is disabled.
(use-package my-game-on-emacs
  :tags (personal))

You can specify multiple tags in a :tags keyword field. If a package has at least one keyword in your current profile, it will be enabled.

Extracting package names from an init file

Another functionality provided by this package is to extract a list of packages from an initialization file. This feature is primarily targeted at Nix users. You can get a list of all packages in use-package forms in a file, or select packages using tags:

;; Get a list of packages in the current buffer by tags
(use-package-tags-select '(programming blog))

;; You can specify a file name using :from keyword
(use-package-tags-select '(programming blog)
  :from (expand-file-name "init.el" user-emacs-directory))

;; The :from argument also accepts t, which is evaluated to
;; `use-package-tags-init-files' (default: user-init-file)
(use-package-tags-select '(programming blog)
  :from t)

;; You can select all packages by passing t as the query
(use-package-tags-select t)

;; If you set :installable to t, it returns packages on the registry
;; (requires epkg.el)
(use-package-tags-select t :installable t)

You can save the result to a file:

;; Save the list of packages to packages.txt in plain text
(with-temp-buffer
  (setq buffer-file-name "packages.txt")
  (insert (use-package-tags-select t :from "init.el"
                                   :installable t
                                   :as 'lines))
  (save-buffer))

The function handles some built-in keywords of use-package to exclude packages that satisfy at least one of the following conditions:

  • Having a non-nil :disabled property.
  • Having an :if property that evaluates to nil.

Not all keywords are supported right now, but it can be extended to support more keywords.

This package also provides use-package-tags-collect-tags function, which returns a list of all tags in a source.

About

Add :tags keyword to use-package, to allow contextual package activation

Topics

Resources

License

Stars

Watchers

Forks