Skip to content

Commit b71b7ee

Browse files
committedMay 19, 2017
fetchFromGitHub: Allow private repos, hosted githubs
1 parent 06767b8 commit b71b7ee

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed
 

‎pkgs/build-support/fetchurl/default.nix

+15-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ in
5959

6060
, recursiveHash ? false
6161

62+
, # Shell code to build a netrc file for BASIC auth
63+
netrcPhase ? null
64+
65+
, # Impure env vars (http://nixos.org/nix/manual/#sec-advanced-attributes)
66+
# needed for netrcPhase
67+
netrcImpureEnvVars ? []
68+
6269
, # Shell code executed after the file has been fetched
6370
# successfully. This can do things like check or transform the file.
6471
postFetch ? ""
@@ -118,11 +125,18 @@ else stdenv.mkDerivation {
118125

119126
outputHashMode = if (recursiveHash || executable) then "recursive" else "flat";
120127

121-
inherit curlOpts showURLs mirrorsFile impureEnvVars postFetch downloadToTemp executable;
128+
inherit curlOpts showURLs mirrorsFile postFetch downloadToTemp executable;
129+
130+
impureEnvVars = impureEnvVars ++ netrcImpureEnvVars;
122131

123132
# Doing the download on a remote machine just duplicates network
124133
# traffic, so don't do that.
125134
preferLocalBuild = true;
126135

136+
postHook = if netrcPhase == null then null else ''
137+
${netrcPhase}
138+
curlOpts="$curlOpts --netrc-file $PWD/netrc"
139+
'';
140+
127141
inherit meta;
128142
}

‎pkgs/top-level/all-packages.nix

+19-4
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@ with pkgs;
185185

186186
fetchFromGitHub = {
187187
owner, repo, rev, name ? "${repo}-${rev}-src",
188-
fetchSubmodules ? false,
188+
fetchSubmodules ? false, private ? false,
189+
githubBase ? "github.com", varPrefix ? null,
189190
... # For hash agility
190-
}@args:
191+
}@args: assert private -> !fetchSubmodules;
191192
let
192-
baseUrl = "https://github.com/${owner}/${repo}";
193-
passthruAttrs = removeAttrs args [ "owner" "repo" "rev" "fetchSubmodules" ];
193+
baseUrl = "https://${githubBase}/${owner}/${repo}";
194+
passthruAttrs = removeAttrs args [ "owner" "repo" "rev" "fetchSubmodules" "private" "githubBase" "varPrefix" ];
195+
varBase = "NIX${if varPrefix == null then "" else "_${varPrefix}"}_GITHUB_PRIVATE_";
194196
in if fetchSubmodules then
195197
fetchgit ({
196198
inherit name rev fetchSubmodules;
@@ -203,6 +205,19 @@ with pkgs;
203205
inherit name;
204206
url = "${baseUrl}/archive/${rev}.tar.gz";
205207
meta.homepage = "${baseUrl}/";
208+
} // lib.optionalAttrs private {
209+
netrcPhase = ''
210+
if [ -z "''$${varBase}USERNAME" -o -z "''$${varBase}PASSWORD" ]; then
211+
echo "Error: Private fetchFromGitHub requires the nix building process (nix-daemon in multi user mode) to have the ${varBase}USERNAME and ${varBase}PASSWORD env vars set." >&2
212+
exit 1
213+
fi
214+
cat > netrc <<EOF
215+
machine ${githubBase}
216+
login ''$${varBase}USERNAME
217+
password ''$${varBase}PASSWORD
218+
EOF
219+
'';
220+
netrcImpureEnvVars = [ "${varBase}USERNAME" "${varBase}PASSWORD" ];
206221
} // passthruAttrs) // { inherit rev; };
207222

208223
fetchFromBitbucket = {

0 commit comments

Comments
 (0)
Please sign in to comment.