default.nix (3728B)
1 extra: 2 { config, lib, pkgs, ... }: 3 let imap-notify = (import <jb55pkgs> {}).imap-notify; 4 penv = pkgs.python3.withPackages (ps: with ps; [ dbus-python pygobject3 ]); 5 awake-from-sleep-fetcher = pkgs.writeScript "awake-from-sleep-fetcher" '' 6 #!${penv}/bin/python3 -u 7 8 import dbus 9 import datetime 10 import gobject 11 import os 12 from dbus.mainloop.glib import DBusGMainLoop 13 14 def start_home(): 15 print("starting email fetcher") 16 os.system("systemctl restart --user email-fetcher") 17 18 def handle_sleep_callback(sleeping): 19 if not sleeping: 20 # awoke from sleep 21 start_home() 22 23 DBusGMainLoop(set_as_default=True) # integrate into main loob 24 bus = dbus.SystemBus() # connect to dbus system wide 25 bus.add_signal_receiver( # defince the signal to listen to 26 handle_sleep_callback, # name of callback function 27 'PrepareForSleep', # signal name 28 'org.freedesktop.login1.Manager', # interface 29 'org.freedesktop.login1' # bus name 30 ) 31 32 loop = gobject.MainLoop() # define mainloop 33 loop.run() 34 ''; 35 36 notifier = user: pass: cmd: host: extra.util.writeBash "notifier" '' 37 set -e 38 39 arg="${host}" 40 41 # wait for connectivity 42 until ${pkgs.libressl.nc}/bin/nc -w 1 -vz ${host} 12788 &>/dev/null; do sleep 1; done 43 44 # run it once first in case we missed any from lost connectivity 45 ${cmd} || : 46 export IMAP_NOTIFY_USER=${user} 47 export IMAP_NOTIFY_PASS=${pass} 48 export IMAP_NOTIFY_CMD=${cmd} 49 export IMAP_NOTIFY_HOST=${host} 50 export IMAP_NOTIFY_TLS=no 51 exec ${imap-notify}/bin/imap-notify 52 ''; 53 in 54 with extra; { 55 systemd.user.services.email-fetcher = { 56 enable = if extra.is-minimal then false else true; 57 description = "email fetcher"; 58 59 environment = { 60 IMAP_ALLOW_UNAUTHORIZED = "0"; 61 IMAP_NOTIFY_PORT = "12788"; 62 }; 63 64 path = with pkgs; [ eject util-linux muchsync notmuch bash openssh ]; 65 66 serviceConfig.Type = "simple"; 67 serviceConfig.Restart = "always"; 68 serviceConfig.ExecStart = 69 let cmd = util.writeBash "email-fetcher" '' 70 set -e 71 export HOME=/home/jb55 72 export DATABASEDIR=$HOME/mail/personal 73 74 EVAR=/home/jb55/var/notify 75 LAST_COUNT=$EVAR/last-email-count 76 77 notify() { 78 local c=$(notmuch --config /home/jb55/.notmuch-config-personal count 'tag:inbox and not tag:filed and not tag:noise') 79 local lc=$c 80 if [ -f $LAST_COUNT ]; then 81 lc=$(<$LAST_COUNT) 82 fi 83 echo "$c" > $LAST_COUNT 84 if [ -f ~/var/notify/home ] && [ $c -ne $lc ]; then 85 echo "$c" > $EVAR/email-notify 86 ${pkgs.libnotify}/bin/notify-send -i email-new "You Got Mail (inbox $c)" 87 fi 88 } 89 90 ( 91 flock -x -w 100 200 || exit 1 92 if [ -f ~/var/notify/home ]; then 93 ${pkgs.libnotify}/bin/notify-send -i email-new "Fetching new mail..." 94 fi 95 muchsync -C /home/jb55/.notmuch-config-personal notmuch 96 notify 97 ) 200>/tmp/email-notify.lock 98 ''; 99 in notifier "jb55@jb55.com" private.personal-email-pass cmd "10.100.0.7"; 100 }; 101 102 systemd.user.services.awake-from-sleep-fetcher = { 103 enable = if extra.is-minimal then false else true; 104 description = ""; 105 106 path = with pkgs; [ systemd ]; 107 108 wantedBy = [ "default.target" ]; 109 after = [ "default.target" ]; 110 111 serviceConfig.ExecStart = "${awake-from-sleep-fetcher}"; 112 }; 113 114 }