How to use local variables in muttrc

100 views Asked by At

I'm interested to know if it is possible to assign and use local variables in muttrc. For instance, let's say I have credentials in some file. I'll read this file as is, e.g.

set creds=`cat file`

and, then I would like to use $creds within my muttrc file for further processing, e.g.

set my_var=`head -1 $creds`
set my_foo=`tail -1 $creds`

Is it possible, and, if so, what is correct syntax (the one I shown above does not work for me)?

1

There are 1 answers

0
mph On

You were close.

mutt is able to "source" files that contain configuration commands. It's similar to when you make a change to a file like .bashrc and want to read the changes into your current login with source ~/.bashrc. Basically, you're telling mutt to load an additional configuration file and process it before continuing on with processing your primary muttrc.

The insecure version of what you want to do is to make a file called creds that looks something like this:

set [email protected]
set imap_pass=mysecretpassword

Then source that file in your main .muttrc:

source ~/.mutt/creds # or wherever you put it

When mutt is loading and gets to that line, it'll read that file and process it as if it were part of a muttrc file, then it'll continue on to the next line in your .muttrc.

However, this approach means your passwords are sitting around in plaintext, which is not a best practice. So you can stop there and live with the risks (accidentally checking into version control, for instance) or you can improve the situation with gpg:

First, encrypt your creds file with gpg, e.g. gpg -r [email protected] --encrypt creds. That will give you an encrypted file called creds.gpg.

Second, change your .muttrc to decrypt that file when you first run mutt by replacing the line where you sourced creds with this one:

source "gpg -d ~/.mutt/creds.gpg |"

When you first run mutt, you'll get a password prompt to authenticate in gpg key, the file will be decrypted, and mutt will set the imap_user and imap_pass configuration variables before moving on to process the rest of muttrc.

(Once you have this working, don't forget to delete the unencrypted creds file.)

You could put just your password in there, but using source is useful for keeping your muttrc simple if you use mutt with multiple accounts, so you can make a creds.work, creds.personal, etc. It's also great for storing discrete account configs. I tend to follow the pattern:

personal.profile
  -> personal.credentials
  -> personal.sig
work.profile
  -> work.credentials
  -> work.sig

The foo.profile files contain all the configuration directives for a given account, such as set signature = ~/.mutt/personal.sig, set realname = "John Doe"` server variables, mailboxes, etc.

Macros can switch from one account to the other:

macro index .cp '<sync-mailbox><enter-command>source ~/.mutt/personal.profile<enter><change-folder>!<enter>'

macro index .cw '<sync-mailbox><enter-command>source ~/.mutt/iterable.profile<enter><change-folder>!<enter>'

So if I'm in the index of my personal account and want to switch to my work account, I tap .cw (for "change to work") and that sources my work.profile account, which in turn sources the signature and decrypts my work credentials. When I want to switch back, I tap .cp (for "change to personal") and it sources my personal.profile which then sources all the supporting files for that account.

Using source is a good way to break a long muttrc into logical pieces, such as macros, scores, colors, etc. and it makes it easier to make variants for multiple profiles/accounts by just tacking on the .work or .personal extension and sourcing them from the main foo.profile file.

My main muttrc just sets the things all the accounts should have in common, such as the cache directory, general configuration (date_format, index_format, attribution), and it pulls in my default profile with source ~/.mutt/personal.profile.