guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Proof of Concept: Import Emacs' use-packaged packages into Guix' man


From: Mitchell Schmeisser
Subject: Re: Proof of Concept: Import Emacs' use-packaged packages into Guix' manifest.scm
Date: Tue, 27 Dec 2022 19:51:31 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)

Mekeor Melire <mekeor@posteo.de> writes:

>> Perhaps you are thinking about this upside down?
>>
>> emacs has support for guix via `emacs-guix`.
>> Would it not make more sense to make a drop in replacement for
>> `use-package` which is syntactically identical but instead of
>> downloading stuff from the emacs package eco-system it installs it from
>> guix into a special emacs managed profile.
>>
>> Then to use it you would just install the `guix-emacs-use-package`
>> instead of `emacs-use-package`.
>>
>> - Mitchell
>
> That's an interesting idea, thanks for sharing. I think it'd be nice if you 
> shared it not only with me but with the whole mailing-list (guix-devel).

Here is a naive implementation which extends use-package to use guix to
ensure packages.

It turns out `use-package` allows you to customize the ensure function.

This is mostly a plagiarized version of the use-package-ensure-elpa.
It creates a profile in ~/.emacs.d./guix-profile by default and every
package which is marked `:ensure t` will be prompted for install.
Set `use-package-ensure-function` to #'use-package-ensure-guix.

#+BEGIN_SRC emacs-lisp
(require 'use-package)
(require 'use-package-guix)

(setq use-package-ensure-function #'use-package-ensure-guix)
#+END_SRC

This is kind of nice because we can now use the `emacs-guix` interfaces
for managing our `use-package` specifications i.e. updates/removal

It is a bit annoying at first because it prompts for y/n verification
for every package but I think this behavior is desirable.

This is my first emacs package so I'm sure there are many things which
can be improved.

#+BEGIN_SRC emacs-lisp
(require 'guix)
(require 'guix-profiles)
(require 'guix-read)
(require 'guix-ui-package)


(defgroup use-package-guix nil
  "use-package support for guix"
  :group 'use-package-ensure)

(defcustom use-package-profile (concat (getenv "HOME") "/.emacs.d/guix-profile")
  "Location of use-package guix profile"
  :type 'string
  :group 'use-package-guix)

(defun guix-package-installed-p (package)
  (bui-assoc-value package 'installed))

(defun canonicalize-name (package-name)
  "Make sure package name has \"emacs-\" prefix"
  (if (string-match "^emacs-.+" package-name)
      package-name
    (concat "emacs-" package-name)))

(defun emacs-package->guix-package (package)
  "Return guix package from package name"
  (car (guix-output-list-get-entries use-package-profile 'name
                                     (canonicalize-name package))))

(defun guix-package-id (package)
  (bui-entry-non-void-value package 'id))

(defun guix-install-package (package)
  (if (guix-package-installed-p package)
      t
    (guix-process-package-actions
     use-package-profile
     `((install (,(string-to-number (car (split-string (bui-entry-id package) 
":"))) "out"))))
     (current-buffer)))

(defun guix-installed-packages ()
  (guix-output-list-get-entries use-package-profile 'installed))


(defun use-package-ensure-guix (name args _state &optional _no-refresh)
  (dolist (ensure args)
    (let ((package
           (or (and (eq ensure t)
                    (use-package-as-symbol name))
               ensure)))
      (when package
        (when (consp package)
          (use-package-pin-package (car package) (cdr package))
          (setq package (car package)))

        (let ((package (emacs-package->guix-package (use-package-as-string 
package))))
          (unless (guix-package-installed-p package)
            (condition-case-unless-debug err
                (progn
                  (when (assoc package (bound-and-true-p
                                        package-pinned-packages))
                    (package-read-all-archive-contents))
                  (if (assoc package package-archive-contents)
                      (package-install package)
                    (package-refresh-contents)
                    (when (assoc package (bound-and-true-p
                                          package-pinned-packages))
                      (package-read-all-archive-contents))
                    (guix-install-package package))
                  t)
              (error
               (display-warning 'use-package
                                (format "Failed to install %s: %s"
                                        name (error-message-string err))
                                :error)))))))))

;;;###autoload
(add-to-list 'load-path (concat use-package-profile "/share/emacs/site-lisp"))

(provide 'use-package-guix)
#+END_SRC



reply via email to

[Prev in Thread] Current Thread [Next in Thread]