Skip to content

Instantly share code, notes, and snippets.

Created Jun 23, 2022
What would you like to do?
Home mcron setup
;; This "home-environment" file can be passed to 'guix home reconfigure'
;; to reproduce the content of your profile. This is "symbolic": it only
;; specifies package names. To reproduce the exact same profile, you also
;; need to capture the channels being used, as returned by "guix describe".
;; See the "Replicating Guix" section in the manual.
(add-to-load-path (dirname (current-filename)))
(srfi srfi-171 meta)
(gnu home)
(gnu packages)
(gnu services)
(guix gexp)
(gnu home services shells)
(gnu home services shepherd)
(gnu home services mcron)
(cron mailsync)
(define %cron-home (string-append (dirname (current-filename)) "/cron"))
(define %path-chunks '("/.local/bin"
(define (dotfile-path dot)
(string-append (dirname (current-filename)) "/dotfiles/" dot))
(define (add-to-path str path)
;; Append a string to a path
(string-append path (if (string= "" str) "" ":") str))
(append %base-packages
(list (service
("ecc" . "emacsclient -nc")
("ecr" . "emacsclient -nr")
("ect" . "emacsclient -t")
("emacsd" . "emacs --daemon")
("grep" . "grep --color=auto")
("lg" . "ledger -f ~/Documents/Ledgers/2022/main.ledger")
("ll" . "ls -l")
("ls" . "ls -p --color=auto")
("rebooty" . "reboot")
`(("PATH" . ,(list-reduce add-to-path "$PATH" %path-chunks))))
(bashrc (list (local-file (dotfile-path ".bashrc") "bashrc")
(dotfile-path ".bash_prompt") "bash_prompt")))
(bash-profile (list (local-file
(dotfile-path ".bash_profile") "bash_profile")))))
(service home-shepherd-service-type
(services (list
(provision '(syncthing))
(start #~(make-forkexec-constructor
(stop #~(make-kill-destructor))
"Synchronize folders to other device"))
(provision '(pantalaimon))
(start #~(make-forkexec-constructor
(stop #~(make-kill-destructor))
"Crypto back-end server for ement.el"))))))
(service home-mcron-service-type
(jobs (list %mailsync-job)))))))
(define-module (cron mailsync)
#:use-module (guix gexp)
#:use-module (guix modules)
#:use-module (gnu services mcron)
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 format))
(define (nm-tag terms)
(system* "notmuch" "tag" (car terms) "--" (cdr terms)))
;; TODO make tagging recursive.
(define %mailsync-default-terms
'(("+inbox" . "path:/.*\\/INBOX/")
("+draft" . "path:/.*\\/Drafts/")
("+sent" . "path:/.*\\/Sent/")
("+trash" . "path:/.*\\/Trash/")
("+spam" . "path:/.*\\/Spam/")
;; Flat todos
("+todo" . "tag:inbox and tag:sent")
("-inbox" . "tag:inbox and tag:sent")
("-sent" . "tag:inbox and tag:sent")
;; Remove tags for moved messages
("-inbox" . "not path:/.*\\/INBOX/")
("-trash" . "not path:/.*\\/Trash/")
("-spam" . "not path:/.*\\/Spam/")
("+work" . "to:EML")
("+work" . "to:EML")
("+work" . "from:EML")
("+work" . "from:EML")))
(define (mailsync-update tags)
(system* "mbsync" "-a")
(nm-tag '("-new" . "tag:new"))
(system* "notmuch" "new")
(map nm-tag tags))
(define (mailsync-count-new-messages)
(let* ((port (open-input-pipe "notmuch count tag:new"))
(newcount (read-line port)))
(close-pipe port)
(string->number newcount)))
(define (mailsync-notify count)
(cond ((> count 0)
(system* "notify-send"
"-c" "email.arrived"
"-i" "/home/trevdev/.config/guix/home/cron/envelope.png"
"-t" (number->string (* 1000 60 14))
(format #f "You have ~d new messages" count)))))
(define-public %mailsync-job
#~(job '(next-minute-from
(next-minute) (range 0 60 15))
(lambda ()
(mailsync-update %mailsync-default-terms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment