Skip to content

Instantly share code, notes, and snippets.

@trev-dev
Created Jun 23, 2022
Embed
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)))
(use-modules
(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)
(packages))
(define %cron-home (string-append (dirname (current-filename)) "/cron"))
(define %path-chunks '("/.local/bin"
"~/.local/npm/bin"
"~/.local/yarn/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))
(home-environment
(packages
(specifications->packages
(append %base-packages
%emacs
%multimedia
%browsers
%chat-clients
%devel)))
(services
(list (service
home-bash-service-type
(home-bash-configuration
(aliases
'(
("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")
))
(environment-variables
`(("PATH" . ,(list-reduce add-to-path "$PATH" %path-chunks))))
(bashrc (list (local-file (dotfile-path ".bashrc") "bashrc")
(local-file
(dotfile-path ".bash_prompt") "bash_prompt")))
(bash-profile (list (local-file
(dotfile-path ".bash_profile") "bash_profile")))))
(service home-shepherd-service-type
(home-shepherd-configuration
(services (list
(shepherd-service
(provision '(syncthing))
(start #~(make-forkexec-constructor
'("syncthing")))
(stop #~(make-kill-destructor))
(documentation
"Synchronize folders to other device"))
(shepherd-service
(provision '(pantalaimon))
(start #~(make-forkexec-constructor
'("pantalaimon")))
(stop #~(make-kill-destructor))
(documentation
"Crypto back-end server for ement.el"))))))
(service home-mcron-service-type
(home-mcron-configuration
(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)
(mailsync-notify
(mailsync-count-new-messages)))
"mbsync-nm-job"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment