diff options
| author | Kyren223 <Kyren223@proton.me> | 2024-12-18 12:08:39 +0200 |
|---|---|---|
| committer | Kyren223 <Kyren223@proton.me> | 2024-12-18 12:08:39 +0200 |
| commit | 10c5d98db1b49c6a7315bcf43a58e399a0507bb5 (patch) | |
| tree | 0b4ba662377383fc1b5d8994fa670dfcd3e1f2e4 | |
| parent | 74c1ef70ce35cf0d082bf7e987288961e99fd961 (diff) | |
Added git sync for my notes dir and made the battery notifications run
less frequently
| -rw-r--r-- | hosts/laptop-nixos/configuration.nix | 27 | ||||
| -rwxr-xr-x | scripts/git-auto-sync.sh | 86 |
2 files changed, 111 insertions, 2 deletions
diff --git a/hosts/laptop-nixos/configuration.nix b/hosts/laptop-nixos/configuration.nix index 6b6f1b9..3ce7d43 100644 --- a/hosts/laptop-nixos/configuration.nix +++ b/hosts/laptop-nixos/configuration.nix @@ -149,8 +149,8 @@ systemd.timers."battery-notifier" = { wantedBy = [ "timers.target" ]; timerConfig = { - OnBootSec = "1m"; - OnUnitActiveSec = "1m"; + OnBootSec = "3m"; + OnUnitActiveSec = "3m"; Unit = "battery-notifier.service"; }; }; @@ -163,6 +163,29 @@ }; }; + systemd.timers."git-auto-sync" = { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnBootSec = "5m"; + OnUnitActiveSec = "5m"; + Unit = "git-auto-sync.service"; + }; + }; + + systemd.services."git-auto-sync" = { + script = "$HOME/scripts/git-auto-sync.sh"; + path = [ + pkgs.git + pkgs.gh + pkgs.keychain + pkgs.openssh + ]; + serviceConfig = { + Type = "oneshot"; + User = "kyren"; + }; + }; + # Open ports in the firewall. # networking.firewall.allowedTCPPorts = [ ... ]; # networking.firewall.allowedUDPPorts = [ ... ]; diff --git a/scripts/git-auto-sync.sh b/scripts/git-auto-sync.sh new file mode 100755 index 0000000..e51eef2 --- /dev/null +++ b/scripts/git-auto-sync.sh @@ -0,0 +1,86 @@ +# Make sure my ssh key is loaded in ssh-agent for use when committing +eval $(keychain --quiet --eval $HOME/.ssh/id_ed25519) + +REPOS=( + "$HOME/personal/dairy" + "/path/to/repo2" + "/path/to/repo3" +) + +COMMIT_MSG="Auto-sync: $(date +'%Y-%m-%d %H:%M:%S')" + +success_count=0 +failure_count=0 +failed_repos=() + +sync_repo() { + local repo_path="$1" + echo "Syncing $repo_path..." + + cd "$repo_path" || { + echo "Failed to access $repo_path" + failed_repos+=("$repo_path") + ((failure_count++)) + return + } + + # Ensure it's a git repo + if ! git rev-parse --is-inside-work-tree &>/dev/null; then + echo "$repo_path is not a valid Git repo" + echo "Current directory: $(pwd)" + git status || echo "Git status failed in $repo_path" + failed_repos+=("$repo_path") + ((failure_count++)) + return + fi + + if git diff --quiet; then + echo "No unstaged changes in $repo_path" + else + echo "Committing changes in $repo_path..." + git add -A + if ! git commit -m "$COMMIT_MSG"; then + echo "Failed to commit changes in $repo_path" + failed_repos+=("$repo_path") + ((failure_count++)) + return + fi + fi + + if ! git pull --rebase; then + if [[ $(git status --porcelain) == "" ]]; then + echo "No changes to pull in $repo_path" + else + echo "Failed to pull changes in $repo_path" + failed_repos+=("$repo_path") + ((failure_count++)) + return + fi + fi + + if ! git push; then + echo "Failed to push changes in $repo_path" + failed_repos+=("$repo_path") + ((failure_count++)) + return + fi + + echo "$repo_path synced successfully" + ((success_count++)) +} + +# Iterate through each repo +for repo in "${REPOS[@]}"; do + sync_repo "$repo" +done + +# Output results +echo "Sync completed! $success_count succeeded, $failure_count failed, ${#REPOS[@]} total." + +# Check if any repositories partially failed +if [[ $failure_count -gt 0 ]]; then + echo "Failed repos:" + for repo in "${failed_repos[@]}"; do + echo " - $repo" + done +fi |
