This will get most of what I need installed and configure some of it. Some stuff will still require some manual interventions like setting up Nvidia drivers on Arch.
I'll automate and improve things as I go along.
To deploy the dotfiles on a new machine, run:
sh -c "$(curl -fsLS get.chezmoi.io)" -- init --verbose --apply LeonardoMor
chezmoi
documentation is pretty
excellent, I'll just put here some stuff that I find useful to keep in mind.
There is a source directory, $(chezmoi source-path)
and a target directory,
which is the one that you want to manage and typically your $HOME
.
chezmoi
will produce a desired, target state based mainly on two things:
- The source files and directories located at the source dir
- The
${XDG_CONFIG_HOME}/chezmoi/chezmoi.toml
file
You can use this file to manage machine to machine differences. The example given in the docs is your git config. You might want to use your personal email for your personal computer and your work email for your work computer.
Now it might be desirable to have a machine-specific chezmoi.toml
files, so
you'd want to template it. You can add a file .chezmoi.toml.tmpl
to the source
dir that will produce a local chezmoi.toml
once chezmoi init
is run.
But some things on chezmoi.toml
need to be variable. So you need some way to
indicate those instead of having them substituted by constants on
chezmoi init
.
All the functions (and syntax) from
text/template, and the
text template functions from sprig
are
available in chezmoi
. So, one way to do this is to use the print
function.
For example, this is the configuration for the merge tool on the template:
Which produces:
[merge]
command = "nvim"
args = [
"{{.Destination}}",
"{{.Source}}",
"{{.Target}}"
]
on the actual config file. Alternatively, you can use a lot of "{{"
and
"}}"
.
There is a better way: NixOS. I'll not be going down that rabbit hole any time soon. So here is my way to do it. Very likely there are better ways.
run_onchange
scripts are executed to install (and uninstall on macOS) packages
when a change is detected in the package file. The file is different on macOS
and Linux.
For Linux, I do it in exactly the way it's described in the user guide.
Theres an install_cmd
that is defined in chezmoi.toml
depending on the
version of Linux that's detected.
The run_onchange
script tracks changes on the file
.chezmoidata/packages.toml
. When there's a change, the list of packages is
formed from the toml list packages.linux
and passed to the install_cmd
.
Packages removed from the list are not uninstalled. To keep the list faithful to the actual packages I use, uninstalled packages have to be manually removed from the list.
It is worth noting, this packages file cannot be a template.
To tell the full story, on both Linux and macOS, some things have to be
installed before reading the source state. Arch Linux needs paru
and macOS
needs Homebrew, both to install packages. And any OS needs gnupg
to decrypt
secrets.
I can install and uninstall packages somewhat declaratively on macOS thanks to Brew-file.
Brew-file is installed after the other pre-requisites. Then there's a wrap setup
on the .profile
, along with auto-completion on .bashrc
.
This wrap makes it so that brew install
and brew uninstall
update the
brewfile accordingly. And thanks to the following few lines in .bashrc
, these
changes will be pushed to the dotfiles repo:
if {{ printf "%s/bin/brew" .brew_prefix }} --version >/dev/null 2>&1; then
# Reference: https://stackoverflow.com/a/65980738/7830232
eval {{ printf "$(%s/bin/brew shellenv)" .brew_prefix | quote }}
# Set Homebrew-file
if [ -f {{ printf "%s/etc/brew-wrap" .brew_prefix }} ]; then
. {{ printf "%s/etc/brew-wrap" .brew_prefix }}
_post_brewfile_update() {
chezmoi git add .
read -rp 'Commit message? '
chezmoi git -- commit -m "$REPLY"
chezmoi git push origin master
}
fi
fi
But the brewfile can also be edited manually. So for example, editing it with the command:
chezmoi edit --apply "${XDG_CONFIG_HOME}/brewfile/Brewfile"
Will:
- Update and apply the updates to the home dir
- Commit and push the changes
- Will trigger the
.chezmoiscripts/darwin/run_onchange_install_packages.sh
This script is very simple
All thanks to Brew-file.
I prefer asymmetric encryption with gpg. So I created a gpg key pair and pass my email as the recipient to chezmoi. Secrets will be encrypted with my public key and decrypted with my private key on the local machine. You have to responsible for that one key.
The config is to simply add the following at the top of .chezmoi.toml
:
encryption = "gpg"
[gpg]
recipient = "leomc145@gmail.com"
The location of modifier keys varies widely across keyboards. The keys on the home row however are consistent. I only use QWERTY keyboards, so it makes a lot of sense to use homerow mods. It allows me to get typing productively on any computer right away.
My personal keyboard can be configured with the Keychron Launcher, which is, at least, based on QMK.
But there are times when I'm stuck with a laptop keyboard and so to get the same mods and a consistent experience, kanata is my chosen tool.
The config is simply a template made in a Lisp-like syntax. Notice that the config is positional, each key on the source is mapped to whatever is on the same position on the target.
In general, you want kanata to run as a service and to start with the OS.
For macOS, we have to rely on launchctl
, for which there's not a lot of config
examples online, and so we have only its man.
There's a service file that has to be created. This is a template, mostly
because it depends on brew --prefix
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>$service_name</string>
<key>ProgramArguments</key>
<array>
<string>{{ .brew_prefix }}/bin/kanata</string>
<string>--cfg</string>
<string>{{ joinPath .XDG_CONFIG_HOME "kanata" "kanata.kbd" }}</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/Library/Logs/Kanata/kanata.out.log</string>
<key>StandardErrorPath</key>
<string>/Library/Logs/Kanata/kanata.err.log</string>
</dict>
</plist>
All that has to be done with this file is done with this script.
Similarly, for Linux there's a
service file
for systemd
and thanks to that, it's one that we can track with chezmoi.
The setup script is based on these instructions
- [] Configure kanata on Windows
I still have to do this. Although very unlikely, I might game on Windows with the laptop keyboard. Because of that, I'll need two layers, one with the mods and the other without