fork/home-manager
git clone https://git.julia.blue/fork/home-manager
feb70061
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . git ; 8 9gitIniType = with types ; 10let 11primitiveType = either str ( either bool int ); 12multipleType = either primitiveType ( listOf primitiveType ); 13sectionType = attrsOf multipleType ; 14supersectionType = attrsOf ( either multipleType sectionType ); 15in attrsOf supersectionType ; 16 17signModule = types . submodule { 18options = { 19key = mkOption { 20type = types . nullOr types . str ; 21description = '' 22The default GPG signing key fingerprint. 23 24Set to `null` to let GnuPG decide what signing key 25to use depending on commit’s author. 26'' ; 27}; 28 29signByDefault = mkOption { 30type = types . bool ; 31default = false ; 32description = "Whether commits and tags should be signed by default." ; 33}; 34 35gpgPath = mkOption { 36type = types . str ; 37default = " ${ pkgs . gnupg } /bin/gpg2" ; 38defaultText = "\${pkgs.gnupg}/bin/gpg2" ; 39description = "Path to GnuPG binary to use." ; 40}; 41}; 42}; 43 44includeModule = types . submodule ({ config , ...} :{ 45options = { 46condition = mkOption { 47type = types . nullOr types . str ; 48default = null ; 49description = '' 50Include this configuration only when {var}`condition` 51matches. Allowed conditions are described in 52{manpage}`git-config(1)`. 53'' ; 54}; 55 56path = mkOption { 57type = with types ; either str path ; 58description = "Path of the configuration file to include." ; 59}; 60 61contents = mkOption { 62type = types . attrsOf types . anything ; 63default = { }; 64example = literalExpression '' 65{ 66user = { 67email = "bob@work.example.com"; 68name = "Bob Work"; 69signingKey = "1A2B3C4D5E6F7G8H"; 70}; 71commit = { 72gpgSign = true; 73}; 74}; 75'' ; 76description = '' 77Configuration to include. If empty then a path must be given. 78 79This follows the configuration structure as described in 80{manpage}`git-config(1)`. 81'' ; 82}; 83 84contentSuffix = mkOption { 85type = types . str ; 86default = "gitconfig" ; 87description = '' 88Nix store name for the git configuration text file, 89when generating the configuration text from nix options. 90'' ; 91 92}; 93}; 94config . path = mkIf ( config . contents != { }) ( mkDefault 95( pkgs . writeText ( hm . strings . storeFileName config . contentSuffix ) 96( generators . toGitINI config . contents ))); 97}); 98 99in { 100meta . maintainers = [ maintainers . rycee ]; 101 102options = { 103programs . git = { 104enable = mkEnableOption "Git" ; 105 106package = mkOption { 107type = types . package ; 108default = pkgs . git ; 109defaultText = literalExpression "pkgs.git" ; 110description = '' 111Git package to install. Use {var}`pkgs.gitAndTools.gitFull` 112to gain access to {command}`git send-email` for instance. 113'' ; 114}; 115 116userName = mkOption { 117type = types . nullOr types . str ; 118default = null ; 119description = "Default user name to use." ; 120}; 121 122userEmail = mkOption { 123type = types . nullOr types . str ; 124default = null ; 125description = "Default user email to use." ; 126}; 127 128aliases = mkOption { 129type = types . attrsOf types . str ; 130default = { }; 131example = { co = "checkout" ; }; 132description = "Git aliases to define." ; 133}; 134 135signing = mkOption { 136type = types . nullOr signModule ; 137default = null ; 138description = "Options related to signing commits using GnuPG." ; 139}; 140 141extraConfig = mkOption { 142type = types . either types . lines gitIniType ; 143default = { }; 144example = { 145core = { whitespace = "trailing-space,space-before-tab" ; }; 146url . "ssh://git@host" . insteadOf = "otherhost" ; 147}; 148description = '' 149Additional configuration to add. The use of string values is 150deprecated and will be removed in the future. 151'' ; 152}; 153 154hooks = mkOption { 155type = types . attrsOf types . path ; 156default = { }; 157example = literalExpression '' 158{ 159pre-commit = ./pre-commit-script; 160} 161'' ; 162description = '' 163Configuration helper for Git hooks. 164See <https://git-scm.com/docs/githooks> 165for reference. 166'' ; 167}; 168 169iniContent = mkOption { 170type = gitIniType ; 171internal = true ; 172}; 173 174ignores = mkOption { 175type = types . listOf types . str ; 176default = [ ]; 177example = [ "*~" "*.swp" ]; 178description = "List of paths that should be globally ignored." ; 179}; 180 181attributes = mkOption { 182type = types . listOf types . str ; 183default = [ ]; 184example = [ "*.pdf diff=pdf" ]; 185description = "List of defining attributes set globally." ; 186}; 187 188includes = mkOption { 189type = types . listOf includeModule ; 190default = [ ]; 191example = literalExpression '' 192[ 193{ path = "~/path/to/config.inc"; } 194{ 195path = "~/path/to/conditional.inc"; 196condition = "gitdir:~/src/dir"; 197} 198] 199'' ; 200description = "List of configuration files to include." ; 201}; 202 203lfs = { 204enable = mkEnableOption "Git Large File Storage" ; 205 206skipSmudge = mkOption { 207type = types . bool ; 208default = false ; 209description = '' 210Skip automatic downloading of objects on clone or pull. 211This requires a manual {command}`git lfs pull` 212every time a new commit is checked out on your repository. 213'' ; 214}; 215}; 216 217difftastic = { 218enable = mkEnableOption "" // { 219description = '' 220Enable the {command}`difftastic` syntax highlighter. 221See <https://github.com/Wilfred/difftastic>. 222'' ; 223}; 224 225background = mkOption { 226type = types . enum [ "light" "dark" ]; 227default = "light" ; 228example = "dark" ; 229description = '' 230Determines whether difftastic should use the lighter or darker colors 231for syntax highlighting. 232'' ; 233}; 234 235color = mkOption { 236type = types . enum [ "always" "auto" "never" ]; 237default = "auto" ; 238example = "always" ; 239description = '' 240Determines when difftastic should color its output. 241'' ; 242}; 243 244display = mkOption { 245type = 246types . enum [ "side-by-side" "side-by-side-show-both" "inline" ]; 247default = "side-by-side" ; 248example = "inline" ; 249description = '' 250Determines how the output displays - in one column or two columns. 251'' ; 252}; 253}; 254 255delta = { 256enable = mkEnableOption "" // { 257description = '' 258Whether to enable the {command}`delta` syntax highlighter. 259See <https://github.com/dandavison/delta>. 260'' ; 261}; 262 263package = mkPackageOption pkgs "delta" { }; 264 265options = mkOption { 266type = with types ; 267let 268primitiveType = either str ( either bool int ); 269sectionType = attrsOf primitiveType ; 270in attrsOf ( either primitiveType sectionType ); 271default = { }; 272example = { 273features = "decorations" ; 274whitespace-error-style = "22 reverse" ; 275decorations = { 276commit-decoration-style = "bold yellow box ul" ; 277file-style = "bold yellow ul" ; 278file-decoration-style = "none" ; 279}; 280}; 281description = '' 282Options to configure delta. 283'' ; 284}; 285}; 286 287diff-so-fancy = { 288enable = mkEnableOption "" // { 289description = '' 290Enable the {command}`diff-so-fancy` diff colorizer. 291See <https://github.com/so-fancy/diff-so-fancy>. 292'' ; 293}; 294 295pagerOpts = mkOption { 296type = types . listOf types . str ; 297default = [ "--tabs=4" "-RFX" ]; 298description = '' 299Arguments to be passed to {command}`less`. 300'' ; 301}; 302 303markEmptyLines = mkOption { 304type = types . bool ; 305default = true ; 306example = false ; 307description = '' 308Whether the first block of an empty line should be colored. 309'' ; 310}; 311 312changeHunkIndicators = mkOption { 313type = types . bool ; 314default = true ; 315example = false ; 316description = '' 317Simplify git header chunks to a more human readable format. 318'' ; 319}; 320 321stripLeadingSymbols = mkOption { 322type = types . bool ; 323default = true ; 324example = false ; 325description = '' 326Whether the `+` or `-` at 327line-start should be removed. 328'' ; 329}; 330 331useUnicodeRuler = mkOption { 332type = types . bool ; 333default = true ; 334example = false ; 335description = '' 336By default, the separator for the file header uses Unicode 337line-drawing characters. If this is causing output errors on 338your terminal, set this to false to use ASCII characters instead. 339'' ; 340}; 341 342rulerWidth = mkOption { 343type = types . nullOr types . int ; 344default = null ; 345example = false ; 346description = '' 347By default, the separator for the file header spans the full 348width of the terminal. Use this setting to set the width of 349the file header manually. 350'' ; 351}; 352}; 353}; 354}; 355 356config = mkIf cfg . enable ( mkMerge [ 357{ 358home . packages = [ cfg . package ]; 359assertions = [{ 360assertion = let 361enabled = 362[ cfg . delta . enable cfg . diff-so-fancy . enable cfg . difftastic . enable ]; 363in count id enabled <= 1 ; 364message = 365"Only one of 'programs.git.delta.enable' or 'programs.git.difftastic.enable' or 'programs.git.diff-so-fancy.enable' can be set to true at the same time." ; 366}]; 367 368programs . git . iniContent . user = { 369name = mkIf ( cfg . userName != null ) cfg . userName ; 370= mkIf ( cfg . userEmail != null ) cfg . userEmail ; 371}; 372 373xdg . configFile = { 374"git/config" . text = generators . toGitINI cfg . iniContent ; 375 376"git/ignore" = mkIf ( cfg . ignores != [ ]) { 377text = concatStringsSep "\n" cfg . ignores + "\n" ; 378}; 379 380"git/attributes" = mkIf ( cfg . attributes != [ ]) { 381text = concatStringsSep "\n" cfg . attributes + "\n" ; 382}; 383}; 384} 385 386{ 387programs . git . iniContent = let 388hasSmtp = name :account :account . smtp != null ; 389 390genIdentity = name :account : 391with account ; 392nameValuePair "sendemail. ${ name } " ( if account . msmtp . enable then { 393smtpServer = " ${ pkgs . msmtp } /bin/msmtp" ; 394envelopeSender = "auto" ; 395from = address ; 396} else 397{ 398smtpEncryption = if smtp . tls . enable then 399( if smtp . tls . useStartTls 400|| versionOlder config . home . stateVersion "20.09" then 401"tls" 402else 403"ssl" ) 404else 405"" ; 406smtpSslCertPath = 407mkIf smtp . tls . enable ( toString smtp . tls . certificatesFile ); 408smtpServer = smtp . host ; 409smtpUser = userName ; 410from = address ; 411} // optionalAttrs ( smtp . port != null ) { 412smtpServerPort = smtp . port ; 413}); 414in mapAttrs' genIdentity 415( filterAttrs hasSmtp config . accounts . . accounts ); 416} 417 418( mkIf ( cfg . signing != null ) { 419programs . git . iniContent = { 420user . signingKey = mkIf ( cfg . signing . key != null ) cfg . signing . key ; 421commit . gpgSign = mkDefault cfg . signing . signByDefault ; 422tag . gpgSign = mkDefault cfg . signing . signByDefault ; 423gpg . program = cfg . signing . gpgPath ; 424}; 425}) 426 427( mkIf ( cfg . hooks != { }) { 428programs . git . iniContent = { 429core . hooksPath = let 430entries = 431mapAttrsToList ( name :path :{ inherit name path ; }) cfg . hooks ; 432in toString ( pkgs . linkFarm "git-hooks" entries ); 433}; 434}) 435 436( mkIf ( cfg . aliases != { }) { programs . git . iniContent . alias = cfg . aliases ; }) 437 438( mkIf ( lib . isAttrs cfg . extraConfig ) { 439programs . git . iniContent = cfg . extraConfig ; 440}) 441 442( mkIf ( lib . isString cfg . extraConfig ) { 443warnings = [ '' 444Using programs.git.extraConfig as a string option is 445deprecated and will be removed in the future. Please 446change to using it as an attribute set instead. 447'' ]; 448 449xdg . configFile . "git/config" . text = cfg . extraConfig ; 450}) 451 452( mkIf ( cfg . includes != [ ]) { 453xdg . configFile . "git/config" . text = let 454include = i : 455with i ; 456if condition != null then { 457includeIf .${ condition }. path = " ${ path } " ; 458} else { 459include . path = " ${ path } " ; 460}; 461in mkAfter ( concatStringsSep "\n" 462( map generators . toGitINI ( map include cfg . includes ))); 463}) 464 465( mkIf cfg . lfs . enable { 466home . packages = [ pkgs . git-lfs ]; 467 468programs . git . iniContent . filter . lfs = 469let skipArg = optional cfg . lfs . skipSmudge "--skip" ; 470in { 471clean = "git-lfs clean -- %f" ; 472process = 473concatStringsSep " " ([ "git-lfs" "filter-process" ] ++ skipArg ); 474required = true ; 475smudge = concatStringsSep " " 476([ "git-lfs" "smudge" ] ++ skipArg ++ [ "--" "%f" ]); 477}; 478}) 479 480( mkIf cfg . difftastic . enable { 481home . packages = [ pkgs . difftastic ]; 482 483programs . git . iniContent = let 484difftCommand = concatStringsSep " " [ 485" ${ pkgs . difftastic } /bin/difft" 486"--color ${ cfg . difftastic . color } " 487"--background ${ cfg . difftastic . background } " 488"--display ${ cfg . difftastic . display } " 489]; 490in { diff . external = difftCommand ; }; 491}) 492 493( let 494deltaPackage = cfg . delta . package ; 495deltaCommand = " ${ deltaPackage } /bin/delta" ; 496in mkIf cfg . delta . enable { 497home . packages = [ deltaPackage ]; 498 499programs . git . iniContent = { 500core . pager = deltaCommand ; 501interactive . diffFilter = " ${ deltaCommand } --color-only" ; 502delta = cfg . delta . options ; 503}; 504}) 505 506( mkIf cfg . diff-so-fancy . enable { 507home . packages = [ pkgs . diff-so-fancy ]; 508 509programs . git . iniContent = 510let dsfCommand = " ${ pkgs . diff-so-fancy } /bin/diff-so-fancy" ; 511in { 512core . pager = " ${ dsfCommand } | ${ pkgs . less } /bin/less ${ 513escapeShellArgs cfg . diff-so-fancy . pagerOpts 514} " ; 515interactive . diffFilter = " ${ dsfCommand } --patch" ; 516diff-so-fancy = { 517markEmptyLines = cfg . diff-so-fancy . markEmptyLines ; 518changeHunkIndicators = cfg . diff-so-fancy . changeHunkIndicators ; 519stripLeadingSymbols = cfg . diff-so-fancy . stripLeadingSymbols ; 520useUnicodeRuler = cfg . diff-so-fancy . useUnicodeRuler ; 521rulerWidth = mkIf ( cfg . diff-so-fancy . rulerWidth != null ) 522( cfg . diff-so-fancy . rulerWidth ); 523}; 524}; 525}) 526]); 527}