1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-18 00:59:44 +01:00
home-manager/modules/programs/git.nix

257 lines
6.8 KiB
Nix
Raw Normal View History

2017-01-07 19:16:26 +01:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.git;
gitIniType = with types;
let
primitiveType = either bool (either int str);
in
attrsOf (attrsOf primitiveType);
2017-08-24 01:03:01 +02:00
signModule = types.submodule {
options = {
key = mkOption {
type = types.str;
description = "The default GPG signing key fingerprint.";
};
signByDefault = mkOption {
type = types.bool;
default = false;
description = "Whether commits should be signed by default.";
};
gpgPath = mkOption {
type = types.str;
default = "${pkgs.gnupg}/bin/gpg2";
defaultText = "\${pkgs.gnupg}/bin/gpg2";
description = "Path to GnuPG binary to use.";
2017-01-07 19:16:26 +01:00
};
2017-08-24 01:03:01 +02:00
};
};
2017-01-07 19:16:26 +01:00
2019-01-29 12:33:10 +01:00
includeModule = types.submodule ({ config, ... }: {
2018-04-18 23:41:20 +02:00
options = {
condition = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Include this configuration only when <varname>condition</varname>
matches. Allowed conditions are described in
<citerefentry>
<refentrytitle>git-config</refentrytitle>
<manvolnum>1</manvolnum>
</citerefentry>.
'';
};
path = mkOption {
2019-01-29 12:33:10 +01:00
type = with types; either str path;
2018-04-18 23:41:20 +02:00
description = "Path of the configuration file to include.";
};
2019-01-29 12:33:10 +01:00
contents = mkOption {
type = types.attrs;
default = {};
description = ''
Configuration to include. If empty then a path must be given.
'';
};
2018-04-18 23:41:20 +02:00
};
2019-01-29 12:33:10 +01:00
config.path = mkIf (config.contents != {}) (
mkDefault (pkgs.writeText "contents" (generators.toINI {} config.contents))
);
});
2018-04-18 23:41:20 +02:00
2017-01-07 19:16:26 +01:00
in
{
meta.maintainers = [ maintainers.rycee ];
2017-01-07 19:16:26 +01:00
options = {
programs.git = {
enable = mkEnableOption "Git";
package = mkOption {
type = types.package;
default = pkgs.git;
defaultText = "pkgs.git";
description = ''
Git package to install. Use <varname>pkgs.gitAndTools.gitFull</varname>
to gain access to <command>git send-email</command> for instance.
'';
2017-01-07 19:16:26 +01:00
};
userName = mkOption {
type = types.nullOr types.str;
default = null;
2017-01-07 19:16:26 +01:00
description = "Default user name to use.";
};
userEmail = mkOption {
type = types.nullOr types.str;
default = null;
2017-01-07 19:16:26 +01:00
description = "Default user email to use.";
};
aliases = mkOption {
type = types.attrsOf types.str;
2017-01-07 19:16:26 +01:00
default = {};
example = { co = "checkout"; };
2017-01-07 19:16:26 +01:00
description = "Git aliases to define.";
};
signing = mkOption {
type = types.nullOr signModule;
default = null;
description = "Options related to signing commits using GnuPG.";
};
extraConfig = mkOption {
type = types.either types.lines gitIniType;
default = {};
example = {
core = { whitespace = "trailing-space,space-before-tab"; };
};
2017-01-07 19:16:26 +01:00
description = "Additional configuration to add.";
};
iniContent = mkOption {
type = gitIniType;
internal = true;
};
ignores = mkOption {
type = types.listOf types.str;
default = [];
example = [ "*~" "*.swp" ];
description = "List of paths that should be globally ignored.";
};
2018-04-18 23:41:20 +02:00
includes = mkOption {
type = types.listOf includeModule;
default = [];
example = literalExample ''
[
{ path = "~/path/to/config.inc"; }
{
path = "~/path/to/conditional.inc";
condition = "gitdir:~/src/dir";
}
]
'';
description = "List of configuration files to include.";
};
lfs = {
enable = mkEnableOption "Git Large File Storage";
skipSmudge = mkOption {
type = types.bool;
default = false;
description = ''
Skip automatic downloading of objects on clone or pull.
This requires a manual <command>git lfs pull</command>
every time a new commit is checked out on your repository.
'';
};
};
2017-01-07 19:16:26 +01:00
};
};
config = mkIf cfg.enable (
mkMerge [
{
home.packages = [ cfg.package ];
programs.git.iniContent.user = {
name = mkIf (cfg.userName != null) cfg.userName;
email = mkIf (cfg.userEmail != null) cfg.userEmail;
2017-01-07 19:16:26 +01:00
};
xdg.configFile = {
"git/config".text = generators.toINI {} cfg.iniContent;
"git/ignore" = mkIf (cfg.ignores != []) {
text = concatStringsSep "\n" cfg.ignores + "\n";
};
};
2017-01-07 19:16:26 +01:00
}
{
programs.git.iniContent =
let
hasSmtp = name: account: account.smtp != null;
genIdentity = name: account: with account;
nameValuePair "sendemail \"${name}\"" ({
smtpEncryption = if smtp.tls.enable then "tls" else "";
smtpServer = smtp.host;
smtpUser = userName;
from = address;
}
// optionalAttrs (smtp.port != null) {
smtpServerPort = smtp.port;
});
in
mapAttrs' genIdentity
(filterAttrs hasSmtp config.accounts.email.accounts);
}
(mkIf (cfg.signing != null) {
programs.git.iniContent = {
user.signingKey = cfg.signing.key;
commit.gpgSign = cfg.signing.signByDefault;
gpg.program = cfg.signing.gpgPath;
};
})
(mkIf (cfg.aliases != {}) {
programs.git.iniContent.alias = cfg.aliases;
})
(mkIf (lib.isAttrs cfg.extraConfig) {
programs.git.iniContent = cfg.extraConfig;
})
(mkIf (lib.isString cfg.extraConfig) {
2017-11-12 12:53:10 +01:00
xdg.configFile."git/config".text = cfg.extraConfig;
})
2018-04-18 23:41:20 +02:00
(mkIf (cfg.includes != []) {
xdg.configFile."git/config".text = mkAfter
(concatMapStringsSep "\n"
(i: with i; ''
[${if (condition == null) then "include" else "includeIf \"${condition}\""}]
path = ${path}
'')
cfg.includes);
})
(mkIf cfg.lfs.enable {
home.packages = [ pkgs.git-lfs ];
programs.git.iniContent."filter \"lfs\"" =
let
skipArg = optional cfg.lfs.skipSmudge "--skip";
in
{
clean = "git-lfs clean -- %f";
process = concatStringsSep " " (
[ "git-lfs" "filter-process" ] ++ skipArg
);
required = true;
smudge = concatStringsSep " " (
[ "git-lfs" "smudge" ] ++ skipArg ++ [ "--" "%f" ]
);
};
})
]
2017-01-07 19:16:26 +01:00
);
}