From 709e45c2a8c231201d3f67c6a954021ca2a5f796 Mon Sep 17 00:00:00 2001 From: aszlig Date: Tue, 6 Jul 2021 08:59:40 +0200 Subject: modules/git: Switch to RFC-0042 In RFC-0042[1], the structural settings are exposed via a "settings" attribute, while in our module it's called "config". To make this less ambiguous (since there is already a "config" attribute passed to modules) and more in line with best practices, I renamed it accordingly. Additionally, the configuration file is now generated via *defining* another (read-only) option, which can be used by other modules to reference the path. The previous way this has been done was using the apply attribute to mkOption, which makes it really hard to access the original attributes for these settings. [1]: https://git.io/JcXmU Signed-off-by: aszlig --- .../user/aszlig/profiles/workstation/default.nix | 2 +- modules/user/aszlig/programs/git/default.nix | 66 +++++++++++++--------- 2 files changed, 40 insertions(+), 28 deletions(-) (limited to 'modules') diff --git a/modules/user/aszlig/profiles/workstation/default.nix b/modules/user/aszlig/profiles/workstation/default.nix index b8724668..3914ab2d 100644 --- a/modules/user/aszlig/profiles/workstation/default.nix +++ b/modules/user/aszlig/profiles/workstation/default.nix @@ -78,7 +78,7 @@ in { vuizvui.user.aszlig.programs.mpv.enable = true; vuizvui.user.aszlig.programs.git.enable = true; - vuizvui.user.aszlig.programs.git.config = { + vuizvui.user.aszlig.programs.git.settings = { color.ui = "auto"; merge.tool = "vimdiff3"; user.email = "aszlig@nix.build"; diff --git a/modules/user/aszlig/programs/git/default.nix b/modules/user/aszlig/programs/git/default.nix index 8965e5dc..e809077b 100644 --- a/modules/user/aszlig/programs/git/default.nix +++ b/modules/user/aszlig/programs/git/default.nix @@ -4,30 +4,6 @@ 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 @@ -41,9 +17,15 @@ in { options.vuizvui.user.aszlig.programs.git = { enable = lib.mkEnableOption "Git"; - config = lib.mkOption { - description = "System-wide default config for Git"; + configFile = lib.mkOption { + type = types.path; + readOnly = true; + description = '' + The path to the system-wide configuration file. + ''; + }; + settings = lib.mkOption { type = let scalar = types.oneOf [ types.bool types.int types.str ]; section = types.either (types.attrsOf scalar) scalar; @@ -56,11 +38,41 @@ in { guitool.foobar.noconsole = true; }; - apply = genConf; + description = '' + System-wide Git options as described in + git-config + 1 + . + ''; }; }; config = lib.mkIf cfg.enable { + vuizvui.user.aszlig.programs.git.configFile = 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 cfg.settings); + in pkgs.writeText "gitconfig" (joinLines lines); + environment.systemPackages = [ gitPatched pkgs.gitAndTools.git-remote-hg -- cgit 1.4.1