fork/home-manager
git clone https://git.julia.blue/fork/home-manager
6dfbdc97
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . services . unison ; 8 9pairOf = t : 10let list = types . addCheck ( types . listOf t ) ( l :length l == 2 ); 11in list // { description = list . description + " of length 2" ; }; 12 13pairOptions = { 14options = { 15stateDirectory = mkOption { 16type = types . path ; 17default = " ${ config . xdg . dataHome } /unison" ; 18defaultText = "$XDG_DATA_HOME/unison" ; 19description = '' 20Unison state directory to use. 21'' ; 22}; 23 24commandOptions = mkOption rec { 25type = with types ; attrsOf ( either str ( listOf str )); 26apply = mergeAttrs default ; 27default = { 28repeat = "watch" ; 29sshcmd = " ${ pkgs . openssh } /bin/ssh" ; 30ui = "text" ; 31auto = "true" ; 32batch = "true" ; 33log = "false" ; # don't log to file, handled by systemd 34}; 35description = '' 36Additional command line options as a dictionary to pass to the 37`unison` program. 38 39Use a list of strings to declare the same option multiple times. 40 41See 42{manpage}`unison(1)` 43for a list of available options. 44'' ; 45}; 46 47roots = mkOption { 48type = pairOf types . str ; 49example = literalExpression '' 50[ 51"/home/user/documents" 52"ssh://remote/documents" 53] 54'' ; 55description = '' 56Pair of roots to synchronise. 57'' ; 58}; 59}; 60}; 61 62serialiseArg = key :val : 63concatStringsSep " " 64( forEach ( toList val ) ( x :escapeShellArg "- ${ key } = ${ escape [ "=" ] x } " )); 65 66serialiseArgs = args :concatStringsSep " " ( mapAttrsToList serialiseArg args ); 67 68makeDefs = gen : 69mapAttrs' 70( name :pairCfg :nameValuePair "unison-pair- ${ name } " ( gen name pairCfg )) 71cfg . pairs ; 72 73in { 74meta . maintainers = with maintainers ; [ pacien ]; 75 76options . services . unison = { 77enable = mkEnableOption "Unison synchronisation" ; 78 79package = mkPackageOption pkgs "unison" { 80example = "pkgs.unison.override { enableX11 = false; }" ; 81}; 82 83pairs = mkOption { 84type = with types ; attrsOf ( submodule pairOptions ); 85default = { }; 86example = literalExpression '' 87{ 88"my-documents" = { 89roots = [ 90"/home/user/documents" 91"ssh://remote/documents" 92]; 93}; 94} 95'' ; 96description = '' 97Unison root pairs to keep synchronised. 98'' ; 99}; 100}; 101 102config = mkIf cfg . enable { 103assertions = [ 104( lib . hm . assertions . assertPlatform "services.unison" pkgs 105lib . platforms . linux ) 106]; 107 108systemd . user . services = makeDefs ( name :pairCfg :{ 109Unit = { 110Description = "Unison pair sync ( ${ name } )" ; 111# Retry forever, useful in case of network disruption. 112StartLimitIntervalSec = 0 ; 113}; 114 115Service = { 116Restart = "always" ; 117RestartSec = 60 ; 118 119CPUSchedulingPolicy = "idle" ; 120IOSchedulingClass = "idle" ; 121 122Environment = [ "UNISON=' ${ toString pairCfg . stateDirectory } '" ]; 123ExecStart = '' 124${ cfg . package } /bin/unison \ 125${ serialiseArgs pairCfg . commandOptions } \ 126${ strings . concatMapStringsSep " " escapeShellArg pairCfg . roots } 127'' ; 128}; 129 130Install = { WantedBy = [ "default.target" ]; }; 131}); 132}; 133}