fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5# Documentation was partially copied from the muchsync manual. 6# See http://www.muchsync.org/muchsync.html 7 8let 9cfg = config . services . muchsync ; 10syncOptions = { 11options = { 12frequency = mkOption { 13type = types . str ; 14default = "*:0/5" ; 15description = '' 16How often to run {command}`muchsync`. This 17value is passed to the systemd timer configuration as the 18`OnCalendar` option. See 19{manpage}`systemd.time(7)` 20for more information about the format. 21'' ; 22}; 23 24sshCommand = mkOption { 25type = types . str ; 26default = " ${ pkgs . openssh } /bin/ssh -CTaxq" ; 27defaultText = "ssh -CTaxq" ; 28description = '' 29Specifies a command line to pass to {command}`/bin/sh` 30to execute a command on another machine. 31 32Note that because this string is passed to the shell, 33special characters including spaces may need to be escaped. 34'' ; 35}; 36 37upload = mkOption { 38type = types . bool ; 39default = true ; 40description = '' 41Whether to propagate local changes to the remote. 42'' ; 43}; 44 45local = { 46checkForModifiedFiles = mkOption { 47type = types . bool ; 48default = false ; 49description = '' 50Check for locally modified files. 51Without this option, muchsync assumes that files in a maildir are 52never edited. 53 54{option}`checkForModifiedFiles` disables certain 55optimizations so as to make muchsync at least check the timestamp on 56every file, which will detect modified files at the cost of a longer 57startup time. 58 59This option is useful if your software regularly modifies the 60contents of mail files (e.g., because you are running offlineimap 61with "synclabels = yes"). 62'' ; 63}; 64 65importNew = mkOption { 66type = types . bool ; 67default = true ; 68description = '' 69Whether to begin the synchronisation by running 70{command}`notmuch new` locally. 71'' ; 72}; 73}; 74 75remote = { 76host = mkOption { 77type = types . str ; 78description = '' 79Remote SSH host to synchronize with. 80'' ; 81}; 82 83muchsyncPath = mkOption { 84type = types . str ; 85default = "" ; 86defaultText = "$PATH/muchsync" ; 87description = '' 88Specifies the path to muchsync on the server. 89Ordinarily, muchsync should be in the default PATH on the server 90so this option is not required. 91However, this option is useful if you have to install muchsync in 92a non-standard place or wish to test development versions of the 93code. 94'' ; 95}; 96 97checkForModifiedFiles = mkOption { 98type = types . bool ; 99default = false ; 100description = '' 101Check for modified files on the remote side. 102Without this option, muchsync assumes that files in a maildir are 103never edited. 104 105{option}`checkForModifiedFiles` disables certain 106optimizations so as to make muchsync at least check the timestamp on 107every file, which will detect modified files at the cost of a longer 108startup time. 109 110This option is useful if your software regularly modifies the 111contents of mail files (e.g., because you are running offlineimap 112with "synclabels = yes"). 113'' ; 114}; 115 116importNew = mkOption { 117type = types . bool ; 118default = true ; 119description = '' 120Whether to begin the synchronisation by running 121{command}`notmuch new` on the remote side. 122'' ; 123}; 124}; 125}; 126}; 127 128in { 129meta . maintainers = with maintainers ; [ pacien ]; 130 131options . services . muchsync = { 132remotes = mkOption { 133type = with types ; attrsOf ( submodule syncOptions ); 134default = { }; 135example = literalExpression '' 136{ 137server = { 138frequency = "*:0/10"; 139remote.host = "server.tld"; 140}; 141} 142'' ; 143description = '' 144Muchsync remotes to synchronise with. 145'' ; 146}; 147}; 148 149config = let 150mapRemotes = gen : 151with attrsets ; 152mapAttrs' 153( name :remoteCfg :nameValuePair "muchsync- ${ name } " ( gen name remoteCfg )) 154cfg . remotes ; 155in mkIf ( cfg . remotes != { }) { 156assertions = [ 157( hm . assertions . assertPlatform "services.muchsync" pkgs platforms . linux ) 158 159{ 160assertion = config . programs . notmuch . enable ; 161message = '' 162The muchsync module requires 'programs.notmuch.enable = true'. 163'' ; 164} 165]; 166 167systemd . user . services = mapRemotes ( name :remoteCfg :{ 168Unit = { Description = "muchsync sync service ( ${ name } )" ; }; 169Service = { 170CPUSchedulingPolicy = "idle" ; 171IOSchedulingClass = "idle" ; 172Environment = [ 173''"PATH= ${ pkgs . notmuch } /bin"'' 174''"NOTMUCH_CONFIG= ${ config . home . sessionVariables . NOTMUCH_CONFIG } "'' 175''"NMBGIT= ${ config . home . sessionVariables . NMBGIT } "'' 176]; 177ExecStart = concatStringsSep " " ([ " ${ pkgs . muchsync } /bin/muchsync" ] 178++ [ "-s ${ escapeShellArg remoteCfg . sshCommand } " ] 179++ optional ( ! remoteCfg . upload ) "--noup" 180 181# local configuration 182++ optional remoteCfg . local . checkForModifiedFiles "-F" 183++ optional ( ! remoteCfg . local . importNew ) "--nonew" 184 185# remote configuration 186++ [ ( escapeShellArg remoteCfg . remote . host ) ] 187++ optional ( remoteCfg . remote . muchsyncPath != "" ) 188"-r ${ escapeShellArg remoteCfg . remote . muchsyncPath } " 189++ optional remoteCfg . remote . checkForModifiedFiles "-F" 190++ optional ( ! remoteCfg . remote . importNew ) "--nonew" ); 191}; 192}); 193 194systemd . user . timers = mapRemotes ( name :remoteCfg :{ 195Unit = { Description = "muchsync periodic sync ( ${ name } )" ; }; 196Timer = { 197Unit = "muchsync- ${ name } .service" ; 198OnCalendar = remoteCfg . frequency ; 199Persistent = true ; 200}; 201Install = { WantedBy = [ "timers.target" ]; }; 202}); 203}; 204}