git: support nested section options

Closes #614
This commit is contained in:
arcnmx 2019-03-12 16:06:36 -07:00 committed by Robert Helgesson
parent 472d7731d6
commit 95382060eb
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
3 changed files with 58 additions and 14 deletions

View file

@ -6,6 +6,19 @@ let
cfg = config.programs.git; cfg = config.programs.git;
# create [section "subsection"] keys from "section.subsection" attrset names
mkSectionName = name:
let
containsQuote = strings.hasInfix ''"'' name;
sections = splitString "." name;
section = head sections;
subsections = tail sections;
subsection = concatStringsSep "." subsections;
in
if containsQuote || subsections == []
then name
else "${section} \"${subsection}\"";
# generation for multiple ini values # generation for multiple ini values
mkKeyValue = k: v: mkKeyValue = k: v:
let let
@ -13,15 +26,33 @@ let
in in
concatStringsSep "\n" (map mkKeyValue (toList v)); concatStringsSep "\n" (map mkKeyValue (toList v));
gitToIni = generators.toINI { inherit mkKeyValue; }; # converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI
gitFlattenAttrs =
let
recurse = path: value:
if isAttrs value then
mapAttrsToList (name: value: recurse ([name] ++ path) value) value
else if length path > 1 then
{ ${concatStringsSep "." (reverseList (tail path))}.${head path} = value; }
else
{ ${head path} = value; };
in
attrs: foldl recursiveUpdate {} (flatten (recurse [] attrs));
gitToIni = attrs:
let
toIni = generators.toINI { inherit mkKeyValue mkSectionName; };
in
toIni (gitFlattenAttrs attrs);
gitIniType = with types; gitIniType = with types;
let let
primitiveType = either str (either bool int); primitiveType = either str (either bool int);
multipleType = either primitiveType (listOf primitiveType); multipleType = either primitiveType (listOf primitiveType);
sectionType = attrsOf multipleType; sectionType = attrsOf multipleType;
supersectionType = attrsOf (either multipleType sectionType);
in in
attrsOf sectionType; attrsOf supersectionType;
signModule = types.submodule { signModule = types.submodule {
options = { options = {
@ -128,6 +159,7 @@ in
default = {}; default = {};
example = { example = {
core = { whitespace = "trailing-space,space-before-tab"; }; core = { whitespace = "trailing-space,space-before-tab"; };
url."ssh://git@host".insteadOf = "otherhost";
}; };
description = "Additional configuration to add."; description = "Additional configuration to add.";
}; };
@ -200,7 +232,7 @@ in
hasSmtp = name: account: account.smtp != null; hasSmtp = name: account: account.smtp != null;
genIdentity = name: account: with account; genIdentity = name: account: with account;
nameValuePair "sendemail \"${name}\"" ({ nameValuePair "sendemail.${name}" ({
smtpEncryption = if smtp.tls.enable then "tls" else ""; smtpEncryption = if smtp.tls.enable then "tls" else "";
smtpServer = smtp.host; smtpServer = smtp.host;
smtpUser = userName; smtpUser = userName;
@ -235,19 +267,23 @@ in
}) })
(mkIf (cfg.includes != []) { (mkIf (cfg.includes != []) {
xdg.configFile."git/config".text = mkAfter xdg.configFile."git/config".text =
(concatMapStringsSep "\n" let
(i: with i; '' include = i: with i;
[${if (condition == null) then "include" else "includeIf \"${condition}\""}] if condition != null
path = ${path} then { includeIf.${condition}.path = "${path}"; }
'') else { include.path = "${path}"; };
cfg.includes); in
mkAfter
(concatStringsSep "\n"
(map gitToIni
(map include cfg.includes)));
}) })
(mkIf cfg.lfs.enable { (mkIf cfg.lfs.enable {
home.packages = [ pkgs.git-lfs ]; home.packages = [ pkgs.git-lfs ];
programs.git.iniContent."filter \"lfs\"" = programs.git.iniContent.filter.lfs =
let let
skipArg = optional cfg.lfs.skipSmudge "--skip"; skipArg = optional cfg.lfs.skipSmudge "--skip";
in in

View file

@ -12,6 +12,12 @@ multiple=1
multiple=2 multiple=2
name=value name=value
[extra "backcompat.with.dots"]
previously=worked
[extra "subsection"]
value=test
[filter "lfs"] [filter "lfs"]
clean=git-lfs clean -- %f clean=git-lfs clean -- %f
process=git-lfs filter-process process=git-lfs filter-process
@ -27,10 +33,10 @@ name=John Doe
signingKey=00112233445566778899AABBCCDDEEFF signingKey=00112233445566778899AABBCCDDEEFF
[include] [include]
path = ~/path/to/config.inc path=~/path/to/config.inc
[includeIf "gitdir:~/src/dir"] [includeIf "gitdir:~/src/dir"]
path = ~/path/to/conditional.inc path=~/path/to/conditional.inc
[includeIf "gitdir:~/src/dir"] [includeIf "gitdir:~/src/dir"]
path = @git_include_path@ path=@git_include_path@

View file

@ -58,9 +58,11 @@ in
{ {
aliases.a2 = mkForce "baz"; aliases.a2 = mkForce "baz";
extraConfig."extra \"backcompat.with.dots\"".previously = "worked";
extraConfig.extra.boolean = true; extraConfig.extra.boolean = true;
extraConfig.extra.integer = 38; extraConfig.extra.integer = 38;
extraConfig.extra.multiple = [2]; extraConfig.extra.multiple = [2];
extraConfig.extra.subsection.value = "test";
} }
]; ];