https://lynn.sh/guix-emacs-file-manager.html
[profile] LYNN
Fedi | Git | Emacs
---------------------------------------------------------------------
anime-emacs.png
Using Emacs as your file manager in Guix
The Problem
File managers. They manage your files. Some examples are windows
explorer, GNOME's Nautilus, or Dolphin for KDE. If you are a real
sicko and only use the terminal, you may be familiar with ranger as
well. They are a perceived necessity in modern-day computing with how
easy they make moving, copying, and deleting files.
Well, I don't use any of those. It's not from any self-imposed rule,
or a sense of superiority,I just simply forgot they exist. Whenever I
need to edit files, I have emacs. Whenever I need to open files, I
have emacs. And whenever I need to delete files, well I tend to do
that in the command-line. This is just how I have gotten comfortable
working with documents and code and it has not brought on any real
need for a file manager. My one exception is when I download
something from the internet, and click Show In Folder. Nothing
happens.
Programs on linux use a database called xdg-mime to match certain
file types to default programs to handle them. On systems like GNOME
or KDE, with full-fledged desktop environments, these are set to
things that would make sense: images are opened in an image-viewer.
Text files are opened in a text editor, and directories are opened in
the file manager. This sounds like a lot of unique programs: an image
viewer, a text editor, and a file manager? This problem of
too-many-programs that I made up for myself is easily solved with a
single program, emacs!
MIME stands for "Multi-purpose Internet Mail Extensions and looks
something like text/html or application/json. If you do any work
on the frontend of the web, you've encountered these!
The "Solution"
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.
Date: 2025-05-23 Fri 00:00
Emacs 29.4 (Org mode 9.6.15)