-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This module helps setting Git remotes when the workspaces are named after a Github repository. It sets remotes urls looking like this, where only 'username' is a variable: "${http or ssh}${username}${workspace name}"
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
{ pkgs, config, lib, ... }: | ||
|
||
# Automatically set git remotes URL for Github repositories. | ||
# To easily use this module, use a reusable workspace like: | ||
# | ||
# gh = { | ||
# github.origin = "my username"; | ||
# }; | ||
# | ||
# It's also a good place to set 'github.ssh_prefix'. | ||
# Then import in every workspaces that use Github: | ||
# | ||
# imports = [ gh ]; | ||
# | ||
|
||
with lib; | ||
|
||
let | ||
conf = config.github; | ||
|
||
mkUrl = prefix: org: "${prefix}${org}/${config.name}"; | ||
mkHttpUrl = mkUrl "https://github.com/"; | ||
mkSshUrl = mkUrl conf.ssh_prefix; | ||
|
||
mkFetchUrl = if conf.private then | ||
assert assertMsg (conf.ssh_prefix != null) | ||
"'github.ssh_prefix' must be set when 'github.private' is set to 'true'."; | ||
mkSshUrl | ||
else | ||
mkHttpUrl; | ||
mkPushUrl = if conf.ssh_prefix != null then mkSshUrl else mkHttpUrl; | ||
|
||
mkPushPullUrl = org: { | ||
fetch = mkFetchUrl org; | ||
push = mkPushUrl org; | ||
}; | ||
|
||
in { | ||
options = with lib; { | ||
github.origin = mkOption { | ||
type = types.nullOr types.string; | ||
default = null; | ||
description = '' | ||
Set 'git.remotes.origin' to point to the Github repository | ||
'<this value>/<workspace name>'. | ||
''; | ||
}; | ||
|
||
github.up = mkOption { | ||
type = types.nullOr types.string; | ||
default = null; | ||
description = '' | ||
Same as the 'origin' option but for a remote named 'up'. | ||
''; | ||
}; | ||
|
||
github.extra_remotes = mkOption { | ||
type = types.listOf types.string; | ||
default = [ ]; | ||
description = '' | ||
Extra remotes, named after the user hosting the repository. Useful for forks, for example. | ||
''; | ||
}; | ||
|
||
github.ssh_prefix = mkOption { | ||
type = types.nullOr types.string; | ||
default = null; | ||
description = '' | ||
Prefix to use for SSH urls, which are used for 'push' url. If this is | ||
unset, only HTTPS urls are used. | ||
''; | ||
}; | ||
|
||
github.private = mkOption { | ||
type = types.bool; | ||
default = false; | ||
description = "Use the SSH url for both push and fetch."; | ||
}; | ||
}; | ||
|
||
config = { | ||
git.remotes = { | ||
up = lib.mkIf (conf.up != null) (mkFetchUrl conf.up); | ||
origin = lib.mkIf (conf.origin != null) (mkPushPullUrl conf.origin); | ||
} // lib.genAttrs conf.extra_remotes mkPushPullUrl; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters