Compare commits

..

No commits in common. "1dcd14cb6df2d0083282575a8e1bdca18dd71fa0" and "6ca1c6688fb8fb6245ad1dfcf74f4fc268d8e724" have entirely different histories.

6 changed files with 51 additions and 56 deletions

View file

@ -4,30 +4,30 @@
After trying several approaches and switching between different setups, this method finally met all of my requirements: After trying several approaches and switching between different setups, this method finally met all of my requirements:
- As simple as possible - As simple as possible
- Multiple hosts are managed in one repository - Multiple machines are managed in one repository
- Identical configurations for one program on several hosts only need to be adjusted in one place - Identical configurations for one program on several machines only need to be adjusted in one place
- Different configurations for one program on several hosts are no problem - Different configurations for one program on several machines are no problem
- Set everything up with one command - Set everything up with one command
- Updating configurations shall require nothing more than =git pull= - Updating configurations shall require nothing more than =git pull=
This is a reference repository which contains a bash script (=dotlink=) and some example dotfiles for two hosts (=host1= and =host2=). This is a reference repository which contains a bash script (=dotlink=) and some example dotfiles for two machines (=host1= and =host2=).
*Warning*: If you want to try this make a backup of your dotfiles! *Warning*: If you want to try this make a backup of your dotfiles!
The script doesn't overwrite existing files but you never know. The script doesn't overwrite existing files but you never know.
** Concept ** Concept
The concept is based on symlinks and two directories, =common= and =hosts=. The concept is based on symlinks and two directories, =common= and =machines=.
The =hosts= directory contains subdirectories for all hosts on which dotfiles are managed. The =machines= directory contains subdirectories for all machines on which dotfiles are managed.
They need to match the /hostname/ of the host (in this repository =host1= and =host2=) and mimic the corresponding =$HOME= directories. They need to match the /hostname/ of the machine (in this repository =host1= and =host2=) and mimic the corresponding =$HOME= directories.
The =common= directory contains configs which are present on multiple hosts. The =common= directory contains configs which are present on multiple machines.
It doesn't follow any specific structure, you can choose what suits your setup. It doesn't follow any specific structure, you can choose what suits your setup.
Subdirectories with program names, followed by the configuration files (in this repository only =mpv=) make probably the most sense but its up to you. Subdirectories with program names, followed by the configuration files (in this repository only =mpv=) make probably the most sense but its up to you.
The =common= directory should never contain symlinks. The =common= directory should never contain symlinks.
Symlinks from the =hosts= directory to the =common= directory make the configs available on multiple hosts and they can be adjusted in one place (see =mpv= in this repository). Symlinks from the =machines= directory to the =common= directory make the configs available on multiple machines and they can be adjusted in one place (see =mpv= in this repository).
Here is the tree output from this repository: Here is the tree output from this repository:
@ -37,7 +37,7 @@
│   └── mpv │   └── mpv
│   ├── input.conf │   ├── input.conf
│   └── mpv.conf │   └── mpv.conf
├── hosts ├── machines
│   ├── host1 │   ├── host1
│   │   ├── .config │   │   ├── .config
│   │   │   └── mpv -> ../../../common/mpv/ │   │   │   └── mpv -> ../../../common/mpv/
@ -52,38 +52,38 @@
The *mpv* configuration is shared between hosts, =host1= has an *OfflineIMAP* configuration in his home directory and =host2= has a *beets* configuration in his =.config= directory. The *mpv* configuration is shared between hosts, =host1= has an *OfflineIMAP* configuration in his home directory and =host2= has a *beets* configuration in his =.config= directory.
Every file from every =hosts/<hostname>= directory can then be linked to the corresponding path into the home directory of the host. Every file from every =machines/<hostname>= directory can then be linked to the corresponding path into the home directory of the machine.
This can be done by hand or with help of the =dotlink= script. This can be done by hand or with help of the =dotlink= script.
If you updated a configuration somewhere else just call =git pull= and thats it. If you updated a configuration somewhere else just call =git pull= and thats it.
** Script usage ** Script usage
When the =dotlink= script is executed, all files from the =hosts/<hostname>= directory, which matches the current /hostname/, are linked to their destination in the =$HOME= directory. When the =dotlink= script is executed, all files from the =machines/<hostname>= directory, which matches the current /hostname/, are linked to their destination in the =$HOME= directory.
Executing the script is only neccessary when new files were added which are not linked yet. Executing the script is only neccessary when new files were added which are not linked yet.
The script can be executed from everywhere, it is just important that it's stored next to a =hosts= directory like in this repository. The script can be executed from everywhere, it is just important that it's stored next to a =machines= directory like in this repository.
#+begin_example #+begin_example
Usage: dotlink [OPTIONS] Usage: dotlink [OPTIONS]
Link all files from hosts/$HOSTNAME to $HOME. Simple dotfile management based on symlinks.
OPTIONS OPTIONS
-h, --help Show this help message and exit -h, --help Show this help message
-u, --unlink Remove current links -u, --unlink Remove current links
#+end_example #+end_example
** Add configuration files for only one host ** Adding configuration files for only one host
Add the files to =hosts/<hostname>/<path_in_home>= and execute the =dotlink= script on the modified host. Add the files to =machines/<hostname>/<path_in_home>=.
If you want to keep all configuration files in the =common= directory and just use symlinks in the =hosts= directory, you can follow the instructions below. If you want to keep all configuration files in the =common= directory and just use symlinks in the =machines= directory, you can follow the instructions below.
** Add configuration files for multiple hosts ** Adding configuration files for multiple host
It is important that the links from the =hosts= directory to the =common= directory are relative. It is important that the links from the =machines= directory to the =common= directory are relative.
Follow these steps to add new configurations for multiple hosts: Follow these steps to add new configurations for multiple machines:
1. Add the files somewhere to the =common= directory 1. Add the files somewhere to the =common= directory
2. Execute =ln -rs common/<config_or_directory> hosts/<hostname>/<path_in_home>= for every host on which the files should be present 2. Execute =ln -rs common/<config_or_directory> machines/<hostname>/<path_in_home>= for every machine on which the files should be present
3. Execute the =dotlink= script on every modified host 3. Execute the =dotlink= script on every modified machine

61
dotlink
View file

@ -24,10 +24,10 @@ function print_usage {
cat <<EOF cat <<EOF
Usage: dotlink [OPTIONS] Usage: dotlink [OPTIONS]
Link all files from hosts/\$HOSTNAME to \$HOME. Simple dotfile management based on symlinks.
OPTIONS OPTIONS
-h, --help Show this help message and exit -h, --help Show this help message
-u, --unlink Remove current links -u, --unlink Remove current links
EOF EOF
exit exit
@ -46,7 +46,7 @@ while (( "$#" )); do
unlink=true unlink=true
shift shift
;; ;;
-*) -*|--*=)
error "Unsupported flag: $1" error "Unsupported flag: $1"
;; ;;
*) *)
@ -55,44 +55,39 @@ while (( "$#" )); do
esac esac
done done
# Get hostname of this machine and filter out hostnames of subdomains like .local
hostname="$(hostname)"
hostname="${hostname%%.*}"
# Get current dotfile directory for later linking # Get current dotfile directory for later linking
dotfiles="$(dirname "$(realpath "$0")")" dotfiles="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Check if dotfiles for host exist # Check if dotfiles for machine exist
if [ ! -d "${dotfiles}/hosts/${hostname}" ]; then if [ ! -d "$dotfiles/machines/$HOSTNAME" ]; then
error "No dotfiles for host ${text_bold}${hostname}${text_reset} found, make sure the directory ${text_bold}${dotfiles}/hosts/${hostname}${text_reset} exists" error "No dotfiles for machine $text_bold$HOSTNAME$text_reset found"
exit 1
fi fi
# Get dotfiles for current host # Get dotfiles for current machine
mapfile -t files < <(find -L "${dotfiles}/hosts/${hostname}" -type f -printf '%P\n') cd "$dotfiles/machines/$HOSTNAME"
files=( $(find -L -type f -printf '%P\n'))
if [ "$unlink" == true ]; then if [ "$unlink" == true ]; then
log "Unlinking ${text_bold}${#files[@]}${text_reset} files..\n" log "Unlinking $text_bold${#files[@]}$text_reset files..\n"
else else
log "Linking ${text_bold}${#files[@]}${text_reset} files..\n" log "Linking $text_bold${#files[@]}$text_reset files..\n"
fi fi
for file in "${files[@]}"; do for file in "${files[@]}"; do
src="${dotfiles}/hosts/${hostname}/${file}"
trgt="${HOME}/${file}"
# Unlink files # Unlink files
if [ "$unlink" == true ]; then if [ "$unlink" == true ]; then
if [ -L "$trgt" ] && [ "$(readlink "$trgt")" == "$src" ]; then if [ -L "$HOME/$file" ] && [ "$(readlink $HOME/$file)" == "$dotfiles/machines/$HOSTNAME/$file" ]; then
rm "$trgt" rm "$HOME/$file"
log "Unlinked ${text_bold}${trgt}${text_reset}" log "Unlinked $text_bold$HOME/$file$text_reset"
# Remove target directory if empty # Remove base directory if empty
if ! [ "$(ls -A "$(dirname "$trgt")")" ]; then if ! [ "$(ls -A $(dirname $HOME/$file))" ]; then
rmdir "$(dirname "$trgt")" rmdir "$(dirname $HOME/$file)"
log "Removed empty directory ${text_bold}$(dirname "$trgt")${text_reset}" log "Removed empty directory $text_bold$(dirname $HOME/$file)$text_reset"
fi fi
fi fi
@ -100,28 +95,28 @@ for file in "${files[@]}"; do
else else
# Check if target is a link # Check if target is a link
if [ -L "$trgt" ]; then if [ -L "$HOME/$file" ]; then
if [ "$(readlink "$trgt")" != "$src" ]; then if [ "$(readlink $HOME/$file)" != "$dotfiles/machines/$HOSTNAME/$file" ]; then
warning "${text_bold}${trgt}${text_reset} is a link but doesn't point to this repository, it will not be linked" warning "$text_bold$HOME/$file$text_reset is a link but doesn't point to this repository, it will not be linked"
continue continue
fi fi
# Check if target is a file or directory # Check if target is a file or directory
elif [ -f "$trgt" ]; then elif [ -f "$HOME/$file" ]; then
warning "${text_bold}${trgt}${text_reset} exists and will not be linked" warning "$text_bold$HOME/$file$text_reset exists and will not be linked"
continue continue
# Create link # Create link
else else
# Create target directory if not existent # Create target directory if not existent
mkdir -p "$(dirname "$trgt")" mkdir -p "$(dirname $HOME/$file)"
# Link file # Link file
ln -s "$src" "$trgt" ln -s "$dotfiles/machines/$HOSTNAME/$file" "$HOME/$file"
log "Linked ${text_bold}${src}${text_reset} to ${text_bold}${trgt}${text_reset}" log "Linked $text_bold$dotfiles/$file$text_reset to $text_bold$HOME/$file$text_reset"
fi fi
fi fi