about summary refs log tree commit diff
path: root/modules/user/aszlig/programs/git/default.nix
blob: 8965e5dc8da43f71f0651ca7d95f01a49dd996f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{ 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
    ];
  };
}