2020-10-12 00:22:58 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.gh;
|
|
|
|
|
2021-11-04 16:42:44 +00:00
|
|
|
yamlFormat = pkgs.formats.yaml { };
|
|
|
|
|
|
|
|
settingsType = types.submodule {
|
|
|
|
freeformType = yamlFormat.type;
|
|
|
|
# These options are only here for the mkRenamedOptionModule support
|
|
|
|
options = {
|
|
|
|
aliases = mkOption {
|
|
|
|
type = with types; attrsOf str;
|
|
|
|
default = { };
|
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
co = "pr checkout";
|
|
|
|
pv = "pr view";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Aliases that allow you to create nicknames for gh commands.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
editor = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
The editor that gh should run when creating issues, pull requests, etc.
|
|
|
|
If blank, will refer to environment.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
git_protocol = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "https";
|
|
|
|
example = "ssh";
|
|
|
|
description = ''
|
|
|
|
The protocol to use when performing Git operations.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
in {
|
2021-11-04 16:42:44 +00:00
|
|
|
meta.maintainers = [ maintainers.gerschtli maintainers.berbiche ];
|
|
|
|
|
|
|
|
imports = (map (x:
|
|
|
|
mkRenamedOptionModule [ "programs" "gh" x ] [
|
|
|
|
"programs"
|
|
|
|
"gh"
|
|
|
|
"settings"
|
|
|
|
x
|
|
|
|
]) [ "aliases" "editor" ]) ++ [
|
|
|
|
(mkRenamedOptionModule [ "programs" "gh" "gitProtocol" ] [
|
|
|
|
"programs"
|
|
|
|
"gh"
|
|
|
|
"settings"
|
|
|
|
"git_protocol"
|
|
|
|
])
|
2023-08-08 10:19:01 +00:00
|
|
|
(mkRenamedOptionModule [ "programs" "gh" "enableGitCredentialHelper" ] [
|
|
|
|
"programs"
|
|
|
|
"gh"
|
|
|
|
"gitCredentialHelper"
|
|
|
|
"enable"
|
|
|
|
])
|
2021-11-04 16:42:44 +00:00
|
|
|
];
|
2020-10-12 00:22:58 +00:00
|
|
|
|
|
|
|
options.programs.gh = {
|
|
|
|
enable = mkEnableOption "GitHub CLI tool";
|
|
|
|
|
2021-11-04 16:42:44 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.gh;
|
|
|
|
defaultText = literalExpression "pkgs.gh";
|
2023-08-08 10:19:01 +00:00
|
|
|
description = "Package providing {command}`gh`.";
|
2021-11-04 16:42:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = settingsType;
|
2020-10-12 00:22:58 +00:00
|
|
|
default = { };
|
2021-11-04 16:42:44 +00:00
|
|
|
description =
|
2023-08-08 10:19:01 +00:00
|
|
|
"Configuration written to {file}`$XDG_CONFIG_HOME/gh/config.yml`.";
|
2021-11-04 16:42:44 +00:00
|
|
|
example = literalExpression ''
|
2020-10-12 00:22:58 +00:00
|
|
|
{
|
2021-11-04 16:42:44 +00:00
|
|
|
git_protocol = "ssh";
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2021-11-04 16:42:44 +00:00
|
|
|
prompt = "enabled";
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2021-11-04 16:42:44 +00:00
|
|
|
aliases = {
|
|
|
|
co = "pr checkout";
|
|
|
|
pv = "pr view";
|
|
|
|
};
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
|
|
|
};
|
2021-11-04 16:42:44 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
gitCredentialHelper = {
|
|
|
|
enable = mkEnableOption "the gh git credential helper" // {
|
2021-11-04 16:42:44 +00:00
|
|
|
default = true;
|
|
|
|
};
|
2023-01-10 09:35:00 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
hosts = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2024-06-04 18:23:39 +00:00
|
|
|
default = [ "https://github.com" "https://gist.github.com" ];
|
2023-08-08 10:19:01 +00:00
|
|
|
description = "GitHub hosts to enable the gh git credential helper for";
|
|
|
|
example = literalExpression ''
|
|
|
|
[ "https://github.com" "https://github.example.com" ]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-01-10 09:35:00 +00:00
|
|
|
extensions = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
2023-08-08 10:19:01 +00:00
|
|
|
gh extensions, see <https://cli.github.com/manual/gh_extension>.
|
2023-01-10 09:35:00 +00:00
|
|
|
'';
|
|
|
|
example = literalExpression "[ pkgs.gh-eco ]";
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2021-11-04 16:42:44 +00:00
|
|
|
home.packages = [ cfg.package ];
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2021-11-04 16:42:44 +00:00
|
|
|
xdg.configFile."gh/config.yml".source =
|
2024-01-05 11:06:15 +00:00
|
|
|
yamlFormat.generate "gh-config.yml" ({ version = "1"; } // cfg.settings);
|
|
|
|
|
|
|
|
# Version 2.40.0+ of gh needs to migrate account formats, this needs to
|
|
|
|
# happen before the version = 1 is placed in the configuration file. Running
|
|
|
|
# `gh help` is sufficient to perform the migration. If the migration already
|
|
|
|
# has occurred, then this is a no-op.
|
|
|
|
#
|
|
|
|
# See https://github.com/nix-community/home-manager/issues/4744 for details.
|
|
|
|
home.activation.migrateGhAccounts =
|
|
|
|
let ghHosts = "${config.xdg.configHome}/gh/hosts.yml";
|
|
|
|
in hm.dag.entryBetween [ "linkGeneration" ] [ "writeBoundary" ] ''
|
2024-06-04 18:23:39 +00:00
|
|
|
if [[ ! -L "${ghHosts}" && -f "${ghHosts}" && $(grep --invert-match --quiet '^version:' ${ghHosts}) ]]; then
|
2024-01-05 11:06:15 +00:00
|
|
|
(
|
|
|
|
TMP_DIR=$(mktemp -d)
|
|
|
|
trap "rm --force --recursive $TMP_DIR" EXIT
|
|
|
|
cp "${ghHosts}" $TMP_DIR/
|
|
|
|
export GH_CONFIG_DIR=$TMP_DIR
|
2024-06-04 18:23:39 +00:00
|
|
|
run --silence ${getExe cfg.package} help
|
2024-01-05 11:06:15 +00:00
|
|
|
cp $TMP_DIR/hosts.yml "${ghHosts}"
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
'';
|
2021-11-04 16:42:44 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
programs.git.extraConfig.credential = mkIf cfg.gitCredentialHelper.enable
|
|
|
|
(builtins.listToAttrs (map (host:
|
|
|
|
lib.nameValuePair host {
|
|
|
|
helper = "${cfg.package}/bin/gh auth git-credential";
|
|
|
|
}) cfg.gitCredentialHelper.hosts));
|
2023-01-10 09:35:00 +00:00
|
|
|
|
|
|
|
xdg.dataFile."gh/extensions" = mkIf (cfg.extensions != [ ]) {
|
|
|
|
source = pkgs.linkFarm "gh-extensions" (builtins.map (p: {
|
|
|
|
name = p.pname;
|
|
|
|
path = "${p}/bin";
|
|
|
|
}) cfg.extensions);
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
}
|