{ config, lib, pkgs, ... }: let inherit (lib) types; cfg = config.vuizvui.user.aszlig.programs.git; genConf = attrs: let escStr = s: "\"${lib.escape [ "\"" "\\" ] s}\""; mkVal = v: if lib.isBool v && v then "true" else if lib.isBool v && !v then "false" else escStr (toString v); mkLine = key: val: "${key} = ${mkVal val}"; joinLines = lib.concatStringsSep "\n"; filterNull = lib.filterAttrs (lib.const (v: !(isNull v))); mkSection = sect: subsect: vals: '' [${sect}${lib.optionalString (subsect != null) " ${escStr subsect}"}] ${joinLines (lib.mapAttrsToList mkLine (filterNull vals))} ''; mkConf = sect: content: let subs = lib.filterAttrs (lib.const lib.isAttrs) content; nonSubs = lib.filterAttrs (lib.const (s: !lib.isAttrs s)) content; maybePlain = lib.optional (nonSubs != {}); plainSects = lib.singleton (mkSection sect null nonSubs); in lib.mapAttrsToList (mkSection sect) subs ++ maybePlain plainSects; lines = lib.flatten (lib.mapAttrsToList mkConf attrs); in pkgs.writeText "gitconfig" (joinLines lines); gitPatched = pkgs.gitFull.overrideAttrs (git: { makeFlags = let oldFlags = git.makeFlags or []; newVal = "ETC_GITCONFIG=${cfg.config}"; in if lib.isList oldFlags then oldFlags ++ [ newVal ] else "${oldFlags} ${newVal}"; }); in { options.vuizvui.user.aszlig.programs.git = { enable = lib.mkEnableOption "Git"; config = lib.mkOption { description = "System-wide default config for Git"; type = let scalar = types.oneOf [ types.bool types.int types.str ]; section = types.either (types.attrsOf scalar) scalar; in types.attrsOf (types.attrsOf section); default = {}; example = { color.ui = "auto"; merge.tool = "vimdiff"; guitool.foobar.noconsole = true; }; apply = genConf; }; }; config = lib.mkIf cfg.enable { environment.systemPackages = [ gitPatched pkgs.gitAndTools.git-remote-hg pkgs.gitAndTools.hub ]; }; }