twpayne/chezmoi

Possibility to watch/scan a folder for new files?

Closed this issue · 5 comments

What exactly are you trying to do?

I'd like to ensure my Remmina (remote desktop software) connections are synced. Remmina creates a new file for each saved connection in ~/.local/share/remmina/ . I've added the folder to chezmoi, but it only adds the exact files existing at the moment, when new ones are created it doesn't know of them.

What have you tried so far?

adding the folder.

Where else have you checked for solutions?

Additional context

I don't think watching the filesystem for changes is in scope or anywhere near it - but i assume there might be a chezmoi script that could be ran on "chezmoi status" or the like that checks the folder for new files?

i am considering something like a cron job/systemd timer that just adds the directory every 5 minutes, but i'd rather prevent that extra interaction if i can keep things within chezmoi.

You can use Watchman for this. There's an example that's the opposite to what you want in chezmoi's documentation, but you should be able to adapt this to run chezmoi add --force whenever your Remmina config directory changes.

thanks. not sure if i want to run a massive facebook service on all my machines, but i'll check it out.

For anyone else with similar goals, but unwilling to run facebook's 50k loc monstrosity for it:

i imagine most linux distributions have inotify-tools available, offering inotifywait as a much simpler way of doing this.

i wrote a short shell script to do what i need:

#!/bin/bash

WATCHED_DIR="$HOME/.local/share/remmina"

while inotifywait -e create -e modify "$WATCHED_DIR"; do
    chezmoi add "$WATCHED_DIR"
done

echo "inotifywait encountered an error, exiting the script"
exit 1

note: this currently also automatically adds it on editing the connections. i'll use it for now, but if there's too many commits happening due to autocommit i'll likely remove the -e modify and sync it manually again. in my case the connections don't change too often though. i just need to have all of them in there.

Thank you, that's a much better solution than using Watchman, and documenting it here will help lots of people in the future.

glad you like it.

of course it does have some limitations, mainly not being cross platform, so documenting watchman might still make sense. although syncing home directories is a very linux thing to do imo, i still get that people might want their configs on other systems.

also, a way to reliably run that script is needed. a simple way is to add it to something like .bash_profile, but i'm never exactly sure when and how often that file gets ran, so i made a systemd user unit:

~/.config/systemd/user/watch-remmina.sh

[Unit]
Description=Watch directory for file changes, add to chezmoi on change

[Service]
Type=simple
# Use %h for the home directory path in systemd service files, here the script is in ~/scripts/
ExecStart=/bin/bash %h/scripts/watch-remmina.sh

[Install]
WantedBy=default.target

and a chezmoi run once file to enable it: run_once_after_enable-watch-remmina.sh (in the chezmoi dir)

#!/bin/bash

systemctl --user daemon-reload

systemctl --user enable watch-remmina.service

systemctl --user start watch-remmina.service