Skip to content

Commit

Permalink
Add Glab
Browse files Browse the repository at this point in the history
  • Loading branch information
shikanime committed Oct 14, 2024
1 parent e1aec54 commit 7f82387
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
64 changes: 64 additions & 0 deletions modules/programs/glab.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{ config, lib, pkgs, ... }:

with lib;

let

cfg = config.programs.glab;
yamlFormat = pkgs.formats.yaml { };

in {
meta.maintainers = [ maintainers.shikanime ];

options.programs.glab = {
enable = mkEnableOption "GitLab CLI";

package = mkOption {
type = types.package;
default = pkgs.glab;
defaultText = literalExpression "pkgs.glab";
description = "Package providing {command}`glab`.";
};

settings = mkOption {
type = yamlFormat.type;
default = { };
description =
"Configuration written to {file}`$XDG_CONFIG_HOME/glab-cli/config.yml`.";
example = literalExpression ''
{
git_protocol = "ssh";
};
'';
};

gitCredentialHelper = {
enable = mkEnableOption "the glab git credential helper" // {
default = true;
};

hosts = mkOption {
type = types.listOf types.str;
default = [ "https://gitlab.com" ];
description =
"GitLab hosts to enable the glab git credential helper for";
example = literalExpression ''
["https://gitlab.com"]
'';
};
};
};

config = mkIf cfg.enable {
home.packages = [ cfg.package ];

xdg.configFile."glab-cli/config.yml".source =
yamlFormat.generate "glab-cli-config.yml" cfg.settings;

programs.git.extraConfig.credential = mkIf cfg.gitCredentialHelper.enable
(builtins.listToAttrs (map (host:
lib.nameValuePair host {
helper = "${cfg.package}/bin/glab auth git-credential";
}) cfg.gitCredentialHelper.hosts));
};
}
23 changes: 23 additions & 0 deletions tests/modules/programs/glab/config-file.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ config, lib, pkgs, ... }:

{
config = {
programs.glab = {
enable = true;
settings.git_protocol = "ssh";
settings.editor = "vim";
};

test.stubs.glab = { };

nmt.script = ''
assertFileExists home-files/.config/glab-cli/config.yml
assertFileContent home-files/.config/glab-cli/config.yml ${
builtins.toFile "config-file.yml" ''
git_protocol: ssh
editor: vim
''
}
'';
};
}
5 changes: 5 additions & 0 deletions tests/modules/programs/glab/credential-helper.git.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[credential "https://gitlab.com"]
helper = "@glab@/bin/glab auth git-credential"

[credential "https://gitlab.example.com"]
helper = "@glab@/bin/glab auth git-credential"
21 changes: 21 additions & 0 deletions tests/modules/programs/glab/credential-helper.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{ config, lib, pkgs, ... }:

{
programs.glab = {
enable = true;
gitCredentialHelper = {
enable = true;
hosts = [ "https://gitlab.com" "https://gitlab.example.com" ];
};
};

programs.git.enable = true;

test.stubs.glab = { };

nmt.script = ''
assertFileExists home-files/.config/git/config
assertFileContent home-files/.config/git/config \
${./credential-helper.git.conf}
'';
}
4 changes: 4 additions & 0 deletions tests/modules/programs/glab/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
glab-config-file = ./config-file.nix;
glab-credential-helper = ./credential-helper.nix;
}

0 comments on commit 7f82387

Please sign in to comment.