Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preliminary support for private dependencies using builtins.fetchGit #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 86 additions & 24 deletions builder/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,88 @@
, pkgs
}:
let
inherit (builtins) split elemAt filter typeOf;

nixVersion = builtins.substring 0 3 builtins.nixVersion;
isNix24Plus = lib.versionAtLeast nixVersion "2.4";

parseGoMod = import ./parser.nix;
parseVersion = import ./parse-version.nix;

removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}'';

fetchGoModule =
{ hash
, goPackagePath
fetchGoModule = (
lib.makeOverridable (
{ hash
, goPackagePath
, version
, private ? false
, go ? pkgs.go
}:
if private then
fetchGoModulePrivate
{
inherit goPackagePath version;
} else
stdenvNoCC.mkDerivation {
name = "${baseNameOf goPackagePath}_${version}";
builder = ./fetch.sh;
inherit goPackagePath version;
nativeBuildInputs = [ go jq ];
outputHashMode = "recursive";
outputHashAlgo = null;
outputHash = hash;
SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt";
impureEnvVars = lib.fetchers.proxyImpureEnvVars;
}
)
);

# A "best effort" attempt at generalising fetching private repositories
# It's very likely that more advanced use cases needs to be done manually
# and that we'll need to have some UX for that.
#
# This version works for popular forges such as Github and Gitlab.
fetchGoModulePrivate =
{ goPackagePath
, version
, go ? pkgs.go
}:
stdenvNoCC.mkDerivation {
name = "${baseNameOf goPackagePath}_${version}";
builder = ./fetch.sh;
inherit goPackagePath version;
nativeBuildInputs = [ go jq ];
outputHashMode = "recursive";
outputHashAlgo = null;
outputHash = hash;
SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt";
impureEnvVars = lib.fetchers.proxyImpureEnvVars;
};
let
parsedVersion = parseVersion version;

segments = filter (s: typeOf s != "list") (split "/" goPackagePath);
seg = elemAt segments;
domain = seg 0;

url = "git@${domain}:${seg 1}/${seg 2}.git";
sourceRoot = lib.concatStringsSep "/" (lib.drop 3 segments);

src = builtins.fetchGit
{
inherit url;
} // lib.optionalAttrs isNix24Plus {
allRefs = true;
} // lib.optionalAttrs (parsedVersion.rev != "") {
# Nix has a bug handling short revs so this won't work.
inherit (parsedVersion) rev;
} // lib.optionalAttrs (parsedVersion.version != "v0.0.0") {
ref = "refs/tags/${parsedVersion.version}";
};

in
if sourceRoot != "" then
stdenvNoCC.mkDerivation
{
name = "${baseNameOf goPackagePath}_${version}-wrapper";
inherit src;
dontConfigure = true;
dontBuild = true;
dontFixup = true;
installPhase = ''
cd "${sourceRoot}"
cp -a . $out
'';
} else src;

buildGoApplication =
{ modules
Expand All @@ -43,6 +103,7 @@ let
, allowGoReference ? false
, meta ? { }
, passthru ? { }
, srcOverrides ? self: super: { }
, ...
}@attrs:
let
Expand All @@ -69,15 +130,16 @@ let
nativeBuildInputs = [ go ];
json = builtins.toJSON modulesStruct;

sources = builtins.toJSON (
lib.mapAttrs
(goPackagePath: meta: fetchGoModule {
goPackagePath = meta.replaced or goPackagePath;
inherit (meta) version hash;
inherit go;
})
modulesStruct.mod
);
sources = builtins.toJSON (builtins.removeAttrs
((lib.makeExtensible (self: (
lib.mapAttrs
(goPackagePath: meta: fetchGoModule {
goPackagePath = meta.replaced or goPackagePath;
inherit (meta) version hash;
inherit go;
})
modulesStruct.mod
))).extend srcOverrides) [ "extend" "__unfix__" ]);

passAsFile = [ "json" "sources" ];
}
Expand Down
41 changes: 41 additions & 0 deletions builder/parse-version.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
let
inherit (builtins) elemAt match hasAttr removeAttrs;
in
ver:
removeAttrs
(builtins.foldl' (acc: f: if hasAttr "rest" acc then f acc else acc)
{
version = "";
rev = "";
versionSuffix = "";
date = "";
rest = ver;
} [
(acc:
let
m = match "([^-]+)-(.*)" acc.rest;
e = elemAt m;
in
if m != null then {
version = e 0;
rest = e 1;
} else removeAttrs acc [ "rest" ] // {
version = ver;
})
(acc:
let
m = elemAt (match "(.*)-(.*)" acc.rest);
in
acc // {
rev = m 1;
rest = m 0;
})
(acc:
let
m = elemAt (match "(.*)\\.(.*)" acc.rest);
in
acc // {
versionSuffix = m 0;
date = m 1;
})
]) [ "rest" ]
12 changes: 0 additions & 12 deletions builder/parser.nix
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@ let
}
);

parseVersion = ver:
let
m = elemAt (match "([^-]+)-?([^-]*)-?([^-]*)" ver);
v = elemAt (match "([^+]+)\\+?(.*)" (m 0));
in
{
version = v 0;
versionSuffix = v 1;
date = m 1;
rev = m 2;
};

parseReplace = data: (
data // {
replace =
Expand Down
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildGoApplication {
pname = "gomod2nix";
version = "0.1";
src = lib.cleanSourceWith {
filter = name: type: ! lib.hasSuffix "tests" name;
filter = name: type: (! lib.hasSuffix "tests" name) && (! lib.hasSuffix "builder" name);
src = lib.cleanSource ./.;
};
modules = ./gomod2nix.toml;
Expand Down