From: Liliana Marie Prikler <liliana.prikler@gmail.com>
To: guix-devel@gnu.org
Subject: Proof of concept: Shepherd + DBus = ♥
Date: Fri, 24 Feb 2023 21:55:40 +0100 [thread overview]
Message-ID: <c36def4a1962ac74542209ed2aad84dcace49b06.camel@gmail.com> (raw)
Hi Guix,
this comes a little late, as Gnome folks have decided that evaluating
arbitrary Javascript over DBus is perhaps not always the wisest idea¹,
but I want to share with you a cool nifty tool regardless.
For starters, recall that Guix can only really set up its profile
environment variables during login – that is if all processes are to
respect them. This brings us into a weird chimera state after running
`guix package': Any new variable introduced in our profile will be lost
until we restart our shell. Or will it?
Enter guile-ac-d-bus. With the newest version of Guile (3.0.9 at time
of writing), we can actually connect to the session bus and do cool
stuff with it. Watch me:
--8<---------------cut here---------------start------------->8---
(define (d-bus-send/wait-for-reply bus message)
(let ((serial (d-bus-write-message bus message)))
(d-bus-conn-flush bus)
(let loop ((msg (d-bus-read-message bus)))
(if (equal? (d-bus-headers-ref (d-bus-message-headers msg)
'REPLY_SERIAL)
serial)
msg
(loop (d-bus-read-message bus))))))
(let ((bus (d-bus-connect))
(message (make-d-bus-message
MESSAGE_TYPE_METHOD_CALL 0 #f '()
(vector
(header-PATH "/org/freedesktop/DBus")
(header-DESTINATION "org.freedesktop.DBus")
(header-INTERFACE "org.freedesktop.DBus")
(header-MEMBER "Hello"))
#f)))
(write (d-bus-send/wait-for-reply bus message))
(d-bus-disconnect bus))
--8<---------------cut here---------------end--------------->8---
Okay, but how does this solve our aforementioned problem?
Well...
--8<---------------cut here---------------start------------->8---
(define shell
(make <service>
#:docstring "Communicate with the GNOME Shell"
#:provides '(gnome shell)
#:start
(lambda ()
(let ((bus (d-bus-connect))
(message (make-d-bus-message
MESSAGE_TYPE_METHOD_CALL 0 #f '()
(vector
(header-PATH "/org/freedesktop/DBus")
(header-DESTINATION "org.freedesktop.DBus")
(header-INTERFACE "org.freedesktop.DBus")
(header-MEMBER "Hello"))
#f)))
(d-bus-send/wait-for-reply bus message)
bus))
#:stop
(lambda (conn)
(when conn (d-bus-disconnect conn))
#f)
#:actions
(let ((action->js
(lambda (action . args)
(format #f "imports.gi.GLib.~a(~{~s~^,~})" action args)))
(shell-exec
(lambda (conn js)
(let ((method-call
(make-d-bus-message
MESSAGE_TYPE_METHOD_CALL 0 #f '()
(vector
(header-PATH "/org/gnome/Shell")
(header-DESTINATION "org.gnome.Shell")
(header-INTERFACE "org.gnome.Shell")
(header-SIGNATURE "s")
(header-MEMBER "Eval"))
(list js))))
(let ((reply (d-bus-send/wait-for-reply conn
method-call)))
(apply peek 'reply (d-bus-message-body reply)))))))
(make-actions
(getenv
(lambda (conn var)
(and=> conn (cut shell-exec <>
(action->js 'getenv var)))))
(setenv
(lambda (conn var val)
(and=> conn (cut shell-exec <>
(action->js 'setenv var val)))))
(unsetenv
(lambda (conn var)
(and=> conn (cut shell-exec <>
(action->js 'unsetenv var)))))))))
--8<---------------cut here---------------end--------------->8---
Once you set the unsafe flag in Looking Glass and promise to be a very
good girl, you can now extract environment variables.
--8<---------------cut here---------------start------------->8---
$ herd getenv gnome PATH
;;; (reply #t "\"/gnu/store/s43dhx83c3a2g79vs5anf3wdmv9lwpi3-glib-
2.70.2-bin/bin:/run/setuid-
programs:/home/yuri/.config/guix/current/bin:$HOME/.guix-
profile/bin:$HOME/.guix-profile/sbin:/run/current-
system/profile/bin:/run/current-system/profile/sbin\"")
--8<---------------cut here---------------end--------------->8---
If you were naughty and didn't do the magic dance, you get a rather
unhelpful result instead.
--8<---------------cut here---------------start------------->8---
$ herd getenv gnome PATH
;;; (reply #f "")
--8<---------------cut here---------------end--------------->8---
Anyway, we can now talk to DBus services from Shepherd, although doing
so is currently a bit of a pain in the buttocks. Much of this is due
to guile-ac-d-bus being somewhat obtuse and not having enough fibers in
it. That being said, I'm sure there's some other use-case out there
that is a good fit for this (wanna talk to loginctl for hibernation
perhaps?)
Cheers
¹ How dare they? Don't they know that this breaks my spacebar heating
workflow?
next reply other threads:[~2023-02-24 20:56 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-24 20:55 Liliana Marie Prikler [this message]
2023-02-25 3:29 ` Proof of concept: Shepherd + DBus = ♥ Maxim Cournoyer
2023-02-25 18:38 ` Ludovic Courtès
2023-02-25 19:34 ` Liliana Marie Prikler
2023-03-06 17:28 ` Ludovic Courtès
2023-04-11 13:44 ` Simon Tournier
2023-04-12 5:30 ` Liliana Marie Prikler
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c36def4a1962ac74542209ed2aad84dcace49b06.camel@gmail.com \
--to=liliana.prikler@gmail.com \
--cc=guix-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).