diff --git a/doc/languages-frameworks/go.xml b/doc/languages-frameworks/go.xml index ab4c9f0f7c8..6f6e7925a1b 100644 --- a/doc/languages-frameworks/go.xml +++ b/doc/languages-frameworks/go.xml @@ -3,12 +3,91 @@ xml:id="sec-language-go"> Go - - The function buildGoPackage builds standard Go programs. - +
+ Go modules - - buildGoPackage + + The function buildGoModule builds Go programs managed + with Go modules. It builds a + Go + modules through a two phase build: + + + + An intermediate fetcher derivation. This derivation will be used to fetch + all of the dependencies of the Go module. + + + + + A final derivation will use the output of the intermediate derivation to + build the binaries and produce the final output. + + + + + + + buildGoModule + +pet = buildGoModule rec { + name = "pet-${version}"; + version = "0.3.4"; + + src = fetchFromGitHub { + owner = "knqyf263"; + repo = "pet"; + rev = "v${version}"; + sha256 = "0m2fzpqxk7hrbxsgqplkg7h2p7gv6s1miymv3gvw0cz039skag0s"; + }; + + modSha256 = "1879j77k96684wi554rkjxydrj8g3hpp0kvxz03sd8dmwr3lh83j"; + + subPackages = [ "." ]; + + meta = with lib; { + description = "Simple command-line snippet manager, written in Go"; + homepage = https://github.com/knqyf263/pet; + license = licenses.mit; + maintainers = with maintainers; [ kalbasit ]; + platforms = platforms.linux ++ platforms.darwin; + }; +} + + + + + is an example expression using + buildGoModule, the following arguments are of special significance to the + function: + + + + modSha256 is the hash of the output of the + intermediate fetcher derivation. + + + + + subPackages limits the builder from building child + packages that have not been listed. If subPackages is + not specified, all child packages will be built. + + + + +
+ +
+ Go legacy + + + The function buildGoPackage builds legacy Go programs, + not supporting Go modules. + + + + buildGoPackage deis = buildGoPackage rec { name = "deis-${version}"; @@ -29,56 +108,56 @@ deis = buildGoPackage rec { buildFlags = "--tags release"; } - + - - is an example expression using - buildGoPackage, the following arguments are of special significance to the - function: - - - - goPackagePath specifies the package's canonical Go - import path. - - - - - subPackages limits the builder from building child - packages that have not been listed. If subPackages is - not specified, all child packages will be built. - - - In this example only github.com/deis/deis/client will - be built. - - - - - goDeps is where the Go dependencies of a Go program are - listed as a list of package source identified by Go import path. It could - be imported as a separate deps.nix file for - readability. The dependency data structure is described below. - - - - - buildFlags is a list of flags passed to the go build - command. - - - - + + is an example expression using + buildGoPackage, the following arguments are of special significance to the + function: + + + + goPackagePath specifies the package's canonical Go + import path. + + + + + subPackages limits the builder from building child + packages that have not been listed. If subPackages is + not specified, all child packages will be built. + + + In this example only github.com/deis/deis/client will + be built. + + + + + goDeps is where the Go dependencies of a Go program + are listed as a list of package source identified by Go import path. It + could be imported as a separate deps.nix file for + readability. The dependency data structure is described below. + + + + + buildFlags is a list of flags passed to the go build + command. + + + + - - The goDeps attribute can be imported from a separate - nix file that defines which Go libraries are needed and - should be included in GOPATH for - buildPhase. - + + The goDeps attribute can be imported from a separate + nix file that defines which Go libraries are needed and + should be included in GOPATH for + buildPhase. + - - deps.nix + + deps.nix [ { @@ -101,60 +180,62 @@ deis = buildGoPackage rec { } ] - + - - - - - goDeps is a list of Go dependencies. - - - - - goPackagePath specifies Go package import path. - - - - - fetch type that needs to be used to get package source. - If git is used there should be url, - rev and sha256 defined next to it. - - - - + + + + + goDeps is a list of Go dependencies. + + + + + goPackagePath specifies Go package import path. + + + + + fetch type that needs to be used to get package + source. If git is used there should be + url, rev and + sha256 defined next to it. + + + + - - To extract dependency information from a Go package in automated way use - go2nix. It can - produce complete derivation and goDeps file for Go - programs. - + + To extract dependency information from a Go package in automated way use + go2nix. It can + produce complete derivation and goDeps file for Go + programs. + - - buildGoPackage produces - where - bin includes program binaries. You can test build a Go - binary as follows: + + buildGoPackage produces + where + bin includes program binaries. You can test build a Go + binary as follows: $ nix-build -A deis.bin - or build all outputs with: + or build all outputs with: $ nix-build -A deis.all - bin output will be installed by default with - nix-env -i or systemPackages. - + bin output will be installed by default with + nix-env -i or systemPackages. + - - You may use Go packages installed into the active Nix profiles by adding the - following to your ~/.bashrc: + + You may use Go packages installed into the active Nix profiles by adding the + following to your ~/.bashrc: for p in $NIX_PROFILES; do GOPATH="$p/share/go:$GOPATH" done - + +
diff --git a/pkgs/development/go-modules/generic/default.nix b/pkgs/development/go-modules/generic/default.nix new file mode 100644 index 00000000000..b4c18343aad --- /dev/null +++ b/pkgs/development/go-modules/generic/default.nix @@ -0,0 +1,214 @@ +{ go, cacert, git, lib, removeReferencesTo, stdenv }: + +{ name +, src +, buildInputs ? [] +, nativeBuildInputs ? [] +, passthru ? {} +, patches ? [] + +# modSha256 is the sha256 of the vendored dependencies +, modSha256 + +# We want parallel builds by default +, enableParallelBuilding ? true + +# Disabled flag +, disabled ? false + +# Do not enable this without good reason +# IE: programs coupled with the compiler +, allowGoReference ? false + +, meta ? {} + +, ... }@args': + +with builtins; + +let + args = removeAttrs args' [ "modSha256" "disabled" ]; + + removeReferences = [ ] ++ lib.optional (!allowGoReference) go; + + removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}''; + + go-modules = go.stdenv.mkDerivation { + name = "${name}-go-modules"; + + nativeBuildInputs = [ go git ]; + + inherit (args) src; + inherit (go) GOOS GOARCH; + + patches = args.patches or []; + + GO111MODULE = "on"; + + # XXX: Add support for other fetchers, such as hg, bzr and alike. + GIT_SSL_CAINFO = "${cacert}/etc/ssl/certs/ca-bundle.crt"; + + # Instruct Go where to find the cacert. + # SSL_CERT_FILE is used by Linux machines. + # NIX_SSL_CERT_FILE is used by Darwin machines based on + # pkgs/development/compilers/go/ssl-cert-file-1.9.patch. + NIX_SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; + SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; + + impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ + "GIT_PROXY_COMMAND" "SOCKS_SERVER" + ]; + + configurePhase = args.modConfigurePhase or '' + runHook preConfigure + + export GOCACHE=$TMPDIR/go-cache + export GOPATH="$TMPDIR/go" + + runHook postConfigure + ''; + + buildPhase = args.modBuildPhase or '' + runHook preBuild + + go mod download + + runHook postBuild + ''; + + installPhase = args.modInstallPhase or '' + runHook preInstall + + cp -r "''${GOPATH}/pkg/mod/cache/download" $out + + runHook postInstall + ''; + + dontFixup = true; + outputHashMode = "recursive"; + outputHashAlgo = "sha256"; + outputHash = modSha256; + }; + + package = go.stdenv.mkDerivation (args // { + nativeBuildInputs = [ removeReferencesTo go ] ++ nativeBuildInputs; + + inherit (go) GOOS GOARCH; + + GO111MODULE = "on"; + + configurePhase = args.configurePhase or '' + runHook preConfigure + + export GOCACHE=$TMPDIR/go-cache + export GOPATH="$TMPDIR/go" + export GOPROXY=file://${go-modules} + + runHook postConfigure + ''; + + buildPhase = args.buildPhase or '' + runHook preBuild + + buildGoDir() { + local d; local cmd; + cmd="$1" + d="$2" + . $TMPDIR/buildFlagsArray + echo "$d" | grep -q "\(/_\|examples\|Godeps\|testdata\)" && return 0 + [ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0 + local OUT + if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" -v -p $NIX_BUILD_CORES $d 2>&1)"; then + if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then + echo "$OUT" >&2 + return 1 + fi + fi + if [ -n "$OUT" ]; then + echo "$OUT" >&2 + fi + return 0 + } + + getGoDirs() { + local type; + type="$1" + if [ -n "$subPackages" ]; then + echo "./$subPackages" + else + find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique + fi + } + + if (( "''${NIX_DEBUG:-0}" >= 1 )); then + buildFlagsArray+=(-x) + fi + + if [ ''${#buildFlagsArray[@]} -ne 0 ]; then + declare -p buildFlagsArray > $TMPDIR/buildFlagsArray + else + touch $TMPDIR/buildFlagsArray + fi + if [ -z "$enableParallelBuilding" ]; then + export NIX_BUILD_CORES=1 + fi + for pkg in $(getGoDirs ""); do + buildGoDir install "$pkg" + done + '' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) '' + # normalize cross-compiled builds w.r.t. native builds + ( + dir=$GOPATH/bin/${go.GOOS}_${go.GOARCH} + if [[ -n "$(shopt -s nullglob; echo $dir/*)" ]]; then + mv $dir/* $dir/.. + fi + if [[ -d $dir ]]; then + rmdir $dir + fi + ) + '' + '' + runHook postBuild + ''; + + doCheck = args.doCheck or false; + checkPhase = args.checkPhase or '' + runHook preCheck + + for pkg in $(getGoDirs test); do + buildGoDir test "$pkg" + done + + runHook postCheck + ''; + + installPhase = args.installPhase or '' + runHook preInstall + + mkdir -p $out + dir="$GOPATH/bin" + [ -e "$dir" ] && cp -r $dir $out + + runHook postInstall + ''; + + preFixup = (args.preFixup or "") + '' + find $out/bin -type f -exec ${removeExpr removeReferences} '{}' + || true + ''; + + disallowedReferences = lib.optional (!allowGoReference) go; + + passthru = passthru // { inherit go go-modules; }; + + meta = { + # Add default meta information + platforms = go.meta.platforms or lib.platforms.all; + } // meta // { + # add an extra maintainer to every package + maintainers = (meta.maintainers or []) ++ + [ lib.maintainers.kalbasit ]; + }; + }); +in if disabled then + throw "${package.name} not supported for go ${go.meta.branch}" +else + package diff --git a/pkgs/development/go-modules/tools/setup-hook.sh b/pkgs/development/go-modules/tools/setup-hook.sh new file mode 100644 index 00000000000..e354e50c35a --- /dev/null +++ b/pkgs/development/go-modules/tools/setup-hook.sh @@ -0,0 +1 @@ +export GOTOOLDIR=@bin@/bin diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 2d9d4185bb4..a59625e0c95 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -13351,6 +13351,12 @@ in buildGoPackage = buildGo111Package; + buildGo111Module = callPackage ../development/go-modules/generic { + go = buildPackages.go_1_11; + }; + + buildGoModule = buildGo111Module; + go2nix = callPackage ../development/tools/go2nix { }; leaps = callPackage ../development/tools/leaps { };