This repository has no description
3.1 kB
92 lines
1{ self, ... }:
2{
3 perSystem =
4 {
5 pkgs,
6 lib,
7 system,
8 ...
9 }:
10 {
11 packages.muchsync = self.libWithPkgs.${system}.makeWrapper pkgs.muchsync {
12 runtimeInputs = [ pkgs.notmuch ] ++ lib.optionals pkgs.stdenv.isLinux [ pkgs.iputils ];
13 env = {
14 NOTMUCH_CONFIG =
15 pkgs.writeText "notmuch-config"
16 self.packages.${pkgs.system}.mutt.passthru.notmuchConfig;
17 };
18 wrapper =
19 { exePath, envString, ... }:
20 ''
21 ${envString}
22 # Create Maildir structure if it doesn't exist
23 mkdir -p "$HOME/Maildir"/{cur,new,tmp}
24
25 # Check if a host is reachable
26 check_host() {
27 local host=$1
28 ping -W2 -c1 "$host" >/dev/null 2>&1
29 }
30
31 # Smart host selection - try spora, nether, retiolum, then tor
32 find_green_host() {
33 if check_host green.s; then
34 echo "green.s"
35 elif check_host green.n; then
36 echo "green.n"
37 elif check_host green.r; then
38 echo "green.r"
39 else
40 echo "tor"
41 fi
42 }
43
44 # Check if we should do auto-sync
45 # Auto-sync if no args or only verbose/config flags
46 auto_sync=false
47 if [ $# -eq 0 ]; then
48 auto_sync=true
49 else
50 # Check if all args are sync-compatible flags
51 all_sync_flags=true
52 for arg in "$@"; do
53 case "$arg" in
54 -v|-vv|-vvv|--verbose)
55 set -x # Enable shell debugging for verbose mode
56 ;; # Verbose flags
57 --config|--config=*) ;; # Config flags
58 -*) all_sync_flags=false; break ;; # Unknown flag, don't auto-sync
59 *) all_sync_flags=false; break ;; # Non-flag argument, don't auto-sync
60 esac
61 done
62 if [ "$all_sync_flags" = true ]; then
63 auto_sync=true
64 fi
65 fi
66
67 if [ "$auto_sync" = true ]; then
68 host=$(find_green_host)
69 echo "Syncing with $host..." >&2
70
71 if [ "$host" = "tor" ]; then
72 # Build dependencies lazily
73 echo "Building tornade..." >&2
74 tornade_path=$(nix build --no-link --print-out-paths "${self}#tornade")
75 clan_path=$(nix build --no-link --print-out-paths "${self}#clan-cli")
76
77 # Get tor hostname using clan CLI
78 tor_hostname=$(CLAN_DIR="${self}" "$clan_path/bin/clan" vars get green tor-ssh/hostname)
79
80 # Use muchsync with custom ssh command via tornade
81 exec ${exePath} -s "$tornade_path/bin/tornade ssh" "$@" lass@"$tor_hostname"
82 else
83 exec ${exePath} "$@" lass@"$host"
84 fi
85 else
86 # Pass through arguments to muchsync
87 exec ${exePath} "$@"
88 fi
89 '';
90 };
91 };
92}