fork/home-manager
git clone https://git.julia.blue/fork/home-manager
a4218048
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6cfg = config . programs . borgmatic ; 7 8yamlFormat = pkgs . formats . yaml { }; 9 10mkNullableOption = args : 11lib . mkOption ( args // { 12type = lib . types . nullOr args . type ; 13default = null ; 14}); 15 16cleanRepositories = repos : 17map ( repo : 18if builtins . isString repo then { 19path = repo ; 20} else 21removeNullValues repo ) repos ; 22 23mkRetentionOption = frequency : 24mkNullableOption { 25type = types . int ; 26description = 27"Number of ${ frequency } archives to keep. Use -1 for no limit." ; 28example = 3 ; 29}; 30 31extraConfigOption = mkOption { 32type = yamlFormat . type ; 33default = { }; 34description = "Extra settings." ; 35}; 36 37repositoryOption = types . submodule { 38options = { 39path = mkOption { 40type = types . str ; 41example = "ssh://myuser@myrepo.myserver.com/./repo" ; 42description = "Path of the repository." ; 43}; 44 45label = mkOption { 46type = types . nullOr types . str ; 47default = null ; 48example = "remote" ; 49description = '' 50Short text describing the repository. Can be used with the 51`--repository` flag to select a repository. 52'' ; 53}; 54}; 55}; 56 57consistencyCheckModule = types . submodule { 58options = { 59name = mkOption { 60type = types . enum [ "repository" "archives" "data" "extract" ]; 61description = "Name of consistency check to run." ; 62example = "repository" ; 63}; 64 65frequency = mkNullableOption { 66type = types . strMatching "([[:digit:]]+ .*)|always" ; 67description = "Frequency of this type of check" ; 68example = "2 weeks" ; 69}; 70}; 71}; 72 73configModule = types . submodule ({ config , ...} :{ 74config . location . extraConfig . exclude_from = 75mkIf config . location . excludeHomeManagerSymlinks 76( mkAfter [ ( toString hmExcludeFile ) ]); 77options = { 78location = { 79sourceDirectories = mkOption { 80type = types . listOf types . str ; 81description = "Directories to backup." ; 82example = literalExpression "[config.home.homeDirectory]" ; 83}; 84 85repositories = mkOption { 86type = types . listOf ( types . either types . str repositoryOption ); 87apply = cleanRepositories ; 88example = literalExpression '' 89[ 90{ 91"path" = "ssh://myuser@myrepo.myserver.com/./repo"; 92"label" = "server"; 93} 94{ 95"path" = "/var/lib/backups/local.borg"; 96"label" = "local"; 97} 98] 99'' ; 100description = '' 101List of local or remote repositories with paths and optional labels. 102'' ; 103}; 104 105excludeHomeManagerSymlinks = mkOption { 106type = types . bool ; 107description = '' 108Whether to exclude Home Manager generated symbolic links from 109the backups. This facilitates restoring the whole home 110directory when the Nix store doesn't contain the latest 111Home Manager generation. 112'' ; 113default = false ; 114example = true ; 115}; 116 117extraConfig = extraConfigOption ; 118}; 119 120storage = { 121encryptionPasscommand = mkNullableOption { 122type = types . str ; 123description = "Command writing the passphrase to standard output." ; 124example = 125literalExpression ''"''${pkgs.password-store}/bin/pass borg-repo"'' ; 126}; 127extraConfig = extraConfigOption ; 128}; 129 130retention = { 131keepWithin = mkNullableOption { 132type = types . strMatching "[[:digit:]]+[Hdwmy]" ; 133description = "Keep all archives within this time interval." ; 134example = "2d" ; 135}; 136 137keepSecondly = mkRetentionOption "secondly" ; 138keepMinutely = mkRetentionOption "minutely" ; 139keepHourly = mkRetentionOption "hourly" ; 140keepDaily = mkRetentionOption "daily" ; 141keepWeekly = mkRetentionOption "weekly" ; 142keepMonthly = mkRetentionOption "monthly" ; 143keepYearly = mkRetentionOption "yearly" ; 144 145extraConfig = extraConfigOption ; 146}; 147 148consistency = { 149checks = mkOption { 150type = types . listOf consistencyCheckModule ; 151default = [ ]; 152description = "Consistency checks to run" ; 153example = literalExpression '' 154[ 155{ 156name = "repository"; 157frequency = "2 weeks"; 158} 159{ 160name = "archives"; 161frequency = "4 weeks"; 162} 163{ 164name = "data"; 165frequency = "6 weeks"; 166} 167{ 168name = "extract"; 169frequency = "6 weeks"; 170} 171]; 172'' ; 173}; 174 175extraConfig = extraConfigOption ; 176}; 177 178output = { extraConfig = extraConfigOption ; }; 179 180hooks = { extraConfig = extraConfigOption ; }; 181}; 182}); 183 184removeNullValues = attrSet :filterAttrs ( key :value :value != null ) attrSet ; 185 186hmFiles = builtins . attrValues config . home . file ; 187hmSymlinks = ( lib . filter ( file :! file . recursive ) hmFiles ); 188hmExcludePattern = file :'' 189${ config . home . homeDirectory } / ${ file . target } 190'' ; 191hmExcludePatterns = lib . concatMapStrings hmExcludePattern hmSymlinks ; 192hmExcludeFile = pkgs . writeText "hm-symlinks.txt" hmExcludePatterns ; 193 194writeConfig = config : 195generators . toYAML { } ( removeNullValues ({ 196source_directories = config . location . sourceDirectories ; 197repositories = config . location . repositories ; 198encryption_passcommand = config . storage . encryptionPasscommand ; 199keep_within = config . retention . keepWithin ; 200keep_secondly = config . retention . keepSecondly ; 201keep_minutely = config . retention . keepMinutely ; 202keep_hourly = config . retention . keepHourly ; 203keep_daily = config . retention . keepDaily ; 204keep_weekly = config . retention . keepWeekly ; 205keep_monthly = config . retention . keepMonthly ; 206keep_yearly = config . retention . keepYearly ; 207checks = config . consistency . checks ; 208} // config . location . extraConfig // config . storage . extraConfig 209// config . retention . extraConfig // config . consistency . extraConfig 210// config . output . extraConfig // config . hooks . extraConfig )); 211in { 212meta . maintainers = [ maintainers . DamienCassou ]; 213 214options = { 215programs . borgmatic = { 216enable = mkEnableOption "Borgmatic" ; 217 218package = mkPackageOption pkgs "borgmatic" { }; 219 220backups = mkOption { 221type = types . attrsOf configModule ; 222description = '' 223Borgmatic allows for several named backup configurations, 224each with its own source directories and repositories. 225'' ; 226example = literalExpression '' 227{ 228personal = { 229location = { 230sourceDirectories = [ "/home/me/personal" ]; 231repositories = [ "ssh://myuser@myserver.com/./personal-repo" ]; 232}; 233}; 234work = { 235location = { 236sourceDirectories = [ "/home/me/work" ]; 237repositories = [ "ssh://myuser@myserver.com/./work-repo" ]; 238}; 239}; 240}; 241'' ; 242}; 243}; 244}; 245 246config = mkIf cfg . enable { 247assertions = [ 248( lib . hm . assertions . assertPlatform "programs.borgmatic" pkgs 249lib . platforms . linux ) 250]; 251 252xdg . configFile = with lib . attrsets ; 253mapAttrs' ( configName :config : 254nameValuePair ( "borgmatic.d/" + configName + ".yaml" ) { 255text = writeConfig config ; 256}) cfg . backups ; 257 258home . packages = [ cfg . package ]; 259}; 260}