nix-configs

Personal NixOS and home-manager configuration files
Log | Files | Refs

notes.nix (1982B)


      1 { pkgs, ...}: let
      2   notes = pkgs.writeShellScriptBin "notes" ''
      3 
      4     notes_dir="$SYNCDIR/notes"
      5     export notes_dir
      6     commit_msg="Commit $(date +"%Y-%m-%dT%H:%M")"
      7 
      8     # Pull latest from git
      9     git -C "$notes_dir" pull >/dev/null &
     10     pull_pid=$!
     11 
     12     # Sync without editing a file
     13     if [ "$1" = "sync" ]; then
     14       # Wait for the pull to finish before modifying/committing anything.
     15       if ! wait "$pull_pid"; then
     16         echo "Git pull failed."
     17         exit 1
     18       fi
     19 
     20       # Delete empty notes files
     21       find "$notes_dir" -type f -not -path '*/.git/*' -empty -print -delete
     22 
     23       # Commit anything new
     24       git -C "$notes_dir" add -A
     25       if git -C "$notes_dir" diff --cached --quiet; then
     26         echo "No changes made; nothing to commit."
     27       else
     28         git -C "$notes_dir" commit -m "$commit_msg" || exit 1
     29       fi
     30 
     31       # Push everything
     32       git -C "$notes_dir" push
     33       exit $?
     34     fi
     35 
     36     # Interactive file selection
     37     if ! edit_file=$(
     38       git -C "$notes_dir" ls-files |
     39         SHELL="$(command -v bash)" fzf --preview '
     40           if file --mime-type --brief -- "$notes_dir"/{} | grep -q "^text/"; then
     41             cat -- "$notes_dir"/{}
     42           else
     43             echo "Binary file"
     44           fi
     45         ' \
     46         --preview-window='right:60%:wrap'
     47     ); then
     48       kill "$pull_pid" 2>/dev/null
     49       wait "$pull_pid" 2>/dev/null
     50       exit 0
     51     fi
     52 
     53     # Wait for the pull before editing/committing.
     54     if ! wait "$pull_pid"; then
     55       echo "Git pull failed."
     56       exit 1
     57     fi
     58 
     59     # Edit our file
     60     nvim "$notes_dir/$edit_file"
     61 
     62     # Delete empty notes files again
     63     find "$notes_dir" -type f -not -path '*/.git/*' -empty -print -delete
     64 
     65     # Commit anything new
     66     git -C "$notes_dir" add -A
     67     if git -C "$notes_dir" diff --cached --quiet; then
     68       echo "No changes made; nothing to commit."
     69       exit 0
     70     fi
     71     git -C "$notes_dir" commit -m "$commit_msg"
     72 
     73   '';
     74 in {
     75   environment.systemPackages = [notes];
     76 }