<commit_before>{ stdenv
, fetchTritonPatch
, fetchurl
, perl
, which
, patchelf

, bash
, iana_etc
, mime-types
, tzdata

, channel ? "1.6"
}:

let
  sources = import ./sources.nix;

  source = sources."${channel}";

  goPlatform =
    if stdenv.hostSystem == "x86_64-linux" then
      "linux-amd64"
    else
      throw "Unsupported System";

  goBootstrap = stdenv.mkDerivation {
    name = "go-bootstrap";

    src = fetchurl {
      url = "https://storage.googleapis.com/golang/go${channel}.${goPlatform}.tar.gz";
      allowHashOutput = false;
      sha256 = source.sha256Bootstrap."${stdenv.hostSystem}";
    };

    nativeBuildInputs = [
      patchelf
    ];

    buildPhase = ''
      strip bin/*
      find bin -type f -exec patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" {} \;
    '';

    installPhase = ''
      mkdir -p $out/share
      ln -s .././ $out/share/go
      cp -r bin src pkg $out

      # Test that the install worked
      $out/bin/go help
    '';
  };
in

stdenv.mkDerivation {
  name = "go-${source.version}";

  src = fetchurl {
    url = "https://storage.googleapis.com/golang/go${source.version}.src.tar.gz";
    inherit (source) sha256;
  };

  # perl is used for testing go vet
  nativeBuildInputs = [
    perl
    which
  ];

  # I'm not sure what go wants from its 'src', but the go installation manual
  # describes an installation keeping the src.
  preUnpack = ''
    mkdir -p $out/share
    cd $out/share
  '';

  prePatch = ''
    # Ensure that the source directory is named go
    cd ..
    if [ ! -d go ]; then
      mv * go
    fi

    cd go
  '';

  patches = [
    (fetchTritonPatch {
      rev = "e55948eaf64c06f2c147cb6b18522a9d9bf72641";
      file = "go/remove-tools.patch";
      sha256 = "275c4428ce5c0ff45e853f93b8259ed656fd2c53cdb83aeb287a9f305c1f84a7";
    })
  ];

  postPatch = ''
    patchShebangs ./ # replace /bin/bash

    # The os test wants to read files in an existing path. Just don't let it be /usr/bin.
    sed -i 's,/usr/bin,'"`pwd`", src/os/os_test.go
    sed -i 's,/bin/pwd,'"`type -P pwd`", src/os/os_test.go
    # Disable the hostname test
    sed -i '/TestHostname/areturn' src/os/os_test.go
    # Remove the api check as it never worked
    sed -i '/src\/cmd\/api\/run.go/ireturn nil' src/cmd/dist/test.go

    sed -i '\#"/etc/mime.types",#i"${mime-types}/etc/mime.types",' src/mime/type_unix.go
    sed -i 's,/etc/protocols,${iana_etc}/etc/protocols,g' src/net/lookup_unix.go
    sed -i 's,/etc/services,${iana_etc}/etc/services,g' src/net/port_unix.go src/net/parse_test.go
    sed -i '\#"/usr/share/zoneinfo/",#i"${tzdata}/share/zoneinfo/",' src/time/zoneinfo_unix.go

    # We need to fix shebangs which will be used in an output script
    # We can't use patch shebangs because this is an embedded script fix
    sed -i 's,#!/usr/bin/env bash,#! ${stdenv.shell},g' misc/cgo/testcarchive/test.bash
  '';

  GOOS = "linux";
  GOARCH =
    if stdenv.system == "i686-linux" then
      "386"
    else if stdenv.system == "x86_64-linux" then
      "amd64"
    else
      throw "Unsupported system.";
  GO386 = 387; # from Arch: don't assume sse2 on i686
  CGO_ENABLED = 1;
  GOROOT_BOOTSTRAP = "${goBootstrap}/share/go";

  # These optimizations / security hardenings break the `os` library
  optimize = false;
  fortifySource = false;

  installPhase = ''
    mkdir -p "$out/bin"
    export GOROOT="$(pwd)/"
    export GOBIN="$out/bin"
    export PATH="$GOBIN:$PATH"
    cd ./src
    echo Building
    ./all.bash
  '';

  preFixup = ''
    rm -r $out/share/go/pkg/bootstrap
    rm -r $out/share/go/{doc,misc}
    find $out -type f \( -name run -or -name \*.bash -or -name \*.sh \) -delete
    rm -r $out/share/go/src/cmd/go/testdata
  '';

  setupHook = ./setup-hook.sh;

  disallowedReferences = [ goBootstrap bash ];

  meta = with stdenv.lib; {
    branch = "1.6";
    homepage = http://golang.org/;
    description = "The Go Programming language";
    license = licenses.bsd3;
    maintainers = with maintainers; [
      wkennington
    ];
    platforms = with platforms;
      x86_64-linux;
  };
}
<commit_msg>go: Fix references and strip binaries<commit_after>{ stdenv
, fetchTritonPatch
, fetchurl
, perl
, which
, patchelf

, bash
, iana_etc
, mime-types
, tzdata

, channel ? "1.6"
}:

let
  sources = import ./sources.nix;

  source = sources."${channel}";

  goPlatform =
    if stdenv.hostSystem == "x86_64-linux" then
      "linux-amd64"
    else
      throw "Unsupported System";

  goBootstrap = stdenv.mkDerivation {
    name = "go-bootstrap";

    src = fetchurl {
      url = "https://storage.googleapis.com/golang/go${channel}.${goPlatform}.tar.gz";
      allowHashOutput = false;
      sha256 = source.sha256Bootstrap."${stdenv.hostSystem}";
    };

    nativeBuildInputs = [
      patchelf
    ];

    buildPhase = ''
      strip bin/*
      find bin -type f -exec patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" {} \;
    '';

    installPhase = ''
      mkdir -p $out/share
      ln -s .././ $out/share/go
      cp -r bin src pkg $out

      # Test that the install worked
      $out/bin/go help
    '';
  };
in

stdenv.mkDerivation {
  name = "go-${source.version}";

  src = fetchurl {
    url = "https://storage.googleapis.com/golang/go${source.version}.src.tar.gz";
    inherit (source) sha256;
  };

  # perl is used for testing go vet
  nativeBuildInputs = [
    perl
    which
  ];

  # I'm not sure what go wants from its 'src', but the go installation manual
  # describes an installation keeping the src.
  preUnpack = ''
    mkdir -p $out/share
    cd $out/share
  '';

  prePatch = ''
    # Ensure that the source directory is named go
    cd ..
    if [ ! -d go ]; then
      mv * go
    fi

    cd go
  '';

  patches = [
    (fetchTritonPatch {
      rev = "e55948eaf64c06f2c147cb6b18522a9d9bf72641";
      file = "go/remove-tools.patch";
      sha256 = "275c4428ce5c0ff45e853f93b8259ed656fd2c53cdb83aeb287a9f305c1f84a7";
    })
  ];

  postPatch = ''
    patchShebangs ./ # replace /bin/bash

    # The os test wants to read files in an existing path. Just don't let it be /usr/bin.
    sed -i 's,/usr/bin,'"`pwd`", src/os/os_test.go
    sed -i 's,/bin/pwd,'"`type -P pwd`", src/os/os_test.go
    # Disable the hostname test
    sed -i '/TestHostname/areturn' src/os/os_test.go
    # Remove the api check as it never worked
    sed -i '/src\/cmd\/api\/run.go/ireturn nil' src/cmd/dist/test.go

    sed -i '\#"/etc/mime.types",#i"${mime-types}/etc/mime.types",' src/mime/type_unix.go
    sed -i 's,/etc/protocols,${iana_etc}/etc/protocols,g' src/net/lookup_unix.go
    sed -i 's,/etc/services,${iana_etc}/etc/services,g' src/net/port_unix.go src/net/parse_test.go
    sed -i '\#"/usr/share/zoneinfo/",#i"${tzdata}/share/zoneinfo/",' src/time/zoneinfo_unix.go

    # We need to fix shebangs which will be used in an output script
    # We can't use patch shebangs because this is an embedded script fix
    sed -i 's,#!/usr/bin/env bash,#! ${stdenv.shell},g' misc/cgo/testcarchive/test.bash
  '';

  GOOS = "linux";
  GOARCH =
    if stdenv.system == "i686-linux" then
      "386"
    else if stdenv.system == "x86_64-linux" then
      "amd64"
    else
      throw "Unsupported system.";
  GO386 = 387; # from Arch: don't assume sse2 on i686
  CGO_ENABLED = 1;
  GOROOT_BOOTSTRAP = "${goBootstrap}/share/go";

  # These optimizations / security hardenings break the `os` library
  optimize = false;
  fortifySource = false;

  installPhase = ''
    mkdir -p "$out/bin"
    export GOROOT="$(pwd)/"
    export GOBIN="$out/bin"
    export PATH="$GOBIN:$PATH"
    cd ./src
    echo Building
    ./all.bash
  '';

  preFixup = ''
    rm -r $out/share/go/pkg/bootstrap
    rm -r $out/share/go/{doc,misc}
    find $out -type f \( -name run -or -name \*.bash -or -name \*.sh \) -delete
    rm -r $out/share/go/src/cmd/go/testdata

    while read exe; do
      strip $exe || true
      patchelf --shrink-rpath $exe || true
    done < <(find $out/share/ \( -executable -and -not -type d \) -or -name \*.a)

    TMPREP="$(printf "/%*s" "$(( ''${#TMPDIR} - 1))" | tr ' ' 'x')"
    while read file; do
      echo "Removing $TMPDIR from $file" >&2
      sed -i "s,$TMPDIR,$TMPREP,g" "$file"
    done < <(grep -r "$TMPDIR" $out | sed "s,.*\(''${out}[^ :]*\).*,\1,g" | sort | uniq)
  '';

  setupHook = ./setup-hook.sh;

  disallowedReferences = [ goBootstrap bash ];

  meta = with stdenv.lib; {
    branch = "1.6";
    homepage = http://golang.org/;
    description = "The Go Programming language";
    license = licenses.bsd3;
    maintainers = with maintainers; [
      wkennington
    ];
    platforms = with platforms;
      x86_64-linux;
  };
}

{'fraction_non_alphanumeric': 0.1517138256268691, 'fraction_numerical': 0.02680009201748332, 'mean_word_length': 2.5032232070910556, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '942277', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3213, 'n_words': 771}