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