I'm sure some of you reading this are rolling your eyes or reciting the well-known "Emacs is a great OS, but …" I use it a lot, though. I already use dired-mode
to manage my writing and programming. It's something that is powerful, and comfortable to use. If you are following along and use something else, it is also quite easy to adjust my code here as well.
Ok, with that out of the way let's look at what we actually want to do: xdg-mime needs to connect "Opening a directory" with "opening emacs in dired-mode
." First, we need the MIME type for directories. A quick search shows that it is called
inode/directory
.
I have no idea why it is called inode/directory, and I couldn't find an explanation. I've learned not to ask questions about these names.
Opening emacs in dired-mode
is the easy part: if you pass emacs a directory instead of a file, it automatically opens in the appropriate mode. Great! Now we need to tie these two concepts together.
If we were using a non-declarative system, such as Debian, I would suggest doing something like creating a .desktop
file, running xdg-mime default emacs-dired.desktop inode/directory
and that would be it. You could theoretically do this on Guix as well. There is a big downside with this strategy, though: the changes you make are not documented anywhere on your system. If you wanted to duplicate your setup on a new laptop, for example, you would have to either note down this change you made or run into it again, hopefully remember your solution, and then reproduce it. Maybe this works for others, but I know myself. I will not remember, I will run into it again, and then I will get frustrated again. That's why we have guix-home
configuration.
home-xdg-mime-applications-service-type
is a very helpful service in the guix-home
configuration tool. It allows you to create xdg-desktop-entry
for each .desktop
file you would normally need to create, link those to the MIME type of your choice, and that's it. It is also all written down explicitly, so there is no confusion on what steps were taken. Let's look at the most basic example to solve our issue:
(home-environment (services (list (service home-xdg-mime-applications-service-type (home-xdg-mime-applications-configuration (default '((inode/directory . emacs-dired.desktop))) (desktop-entries (list (xdg-desktop-entry (file "emacs-dired") (name "Emacs Dired as a file manager") (type 'application) (config '((exec . "emacsclient -c -a emacs %u") (mimetype . "inode/directory")))))))))))
If this is nonsense to you, I apologize. I'm definitely not the person who should be teaching you lisp. You should definitely learn it, though! The configuration language for Guix is called
Guile Scheme
, read up on it.
I use emacsclient
here because I have emacs always running as a daemon. -c
tells it to create a new frame, and -a emacs
tells it to fall-back to a regular emacs session if there is some reason my daemon has stopped running.
And that's it! It works perfectly after a quick guix home reconfigure
. To expand, just add a new xdg-desktop-entry
that runs whatever script you wish, and then tie it to a MIME type in the default
list.
I considered adding things for image types, text types, etc. This would be useful to do if you used a regular file manager. With emacs though, I am already in the program I need for these types. Opening them from dired-mode
already puts them into emacs, so I decided against the necessity for this. If you are using something other than emacs for this setup, I recommend adding the following MIME types:
Mime Type | Application Type |
image/jpeg | image viewer |
image/png | image viewer |
image/svg | image viewer |
image/webp | image viewer |
text/plain | text editor |
text/html | text editor |
text/x-makefile | text editor |
application/pdf | pdf viewer |
x-scheme-handler/http | browser |
x-scheme-handler/https | browser |
There are more, but if you are using a minimal system and want full control this will give you a good starting point. Thanks for reading!
If you are learning Guix and need help figuring something out, I highly recommend checking out rde by abcdw. It is a framework for Guix to help create reproducable systems, and also a wealth of knowledge when it comes to configuring.