summary refs log tree commit diff
path: root/rust-nightly.nix
blob: 3f1fbe4020d7d65784e5724a0a5067f0d758eb07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{ stdenv, lib, buildEnv, makeWrapper, runCommand, fetchurl, zlib, rsync, curl }:

# rustc and cargo nightly binaries

let
  convertPlatform = system:
    if      system == "i686-linux"    then "i686-unknown-linux-gnu"
    else if system == "x86_64-linux"  then "x86_64-unknown-linux-gnu"
    else if system == "i686-darwin"   then "i686-apple-darwin"
    else if system == "x86_64-darwin" then "x86_64-apple-darwin"
    else abort "no snapshot to bootstrap for this platform (missing target triple)";

  thisSys = convertPlatform stdenv.system;

  defaultDateFile = builtins.fetchurl
    "https://static.rust-lang.org/dist/channel-rust-nightly-date.txt";
  defaultDate = lib.removeSuffix "\n" (builtins.readFile defaultDateFile);

  mkUrl = { pname, archive, date, system }:
    "${archive}/${date}/${pname}-nightly-${system}.tar.gz";

  fetch = args: let
      url = mkUrl { inherit (args) pname archive date system; };
      download = builtins.fetchurl (url + ".sha256");
      contents = builtins.readFile download;
      sha256 = args.hash or (lib.head (lib.strings.splitString " " contents));
    in fetchurl { inherit url sha256; };

  generic = { pname, archive, exes }:
      { date ? defaultDate, system ? thisSys, ... } @ args:
      stdenv.mkDerivation rec {
    name = "${pname}-${version}";
    version = "nightly-${date}";
    preferLocalBuild = true;
    # TODO meta;
    outputs = [ "out" "doc" ];
    src = fetch (args // { inherit pname archive system date; });
    nativeBuildInputs = [ rsync ];
    dontStrip = true;
    installPhase = ''
      rsync --chmod=u+w -r ./*/ $out/
    '';
    preFixup = if stdenv.isLinux then let
      # it's overkill, but fixup will prune
      rpath = "$out/lib:" + lib.makeLibraryPath [ zlib stdenv.cc.cc.lib curl ];
    in ''
      for executable in ${lib.concatStringsSep " " exes}; do
        patchelf \
          --interpreter "$(< $NIX_CC/nix-support/dynamic-linker)" \
          --set-rpath "${rpath}" \
          "$out/bin/$executable"
      done
      for library in $out/lib/*.so; do
        patchelf --set-rpath "${rpath}" "$library"
      done
    '' else "";
  };

in rec {
  rustc = generic {
    pname = "rustc";
    archive = "https://static.rust-lang.org/dist";
    exes = [ "rustc" "rustdoc" ];
  };

  rustcWithSysroots = { rustc, sysroots ? [] }: buildEnv {
    name = "combined-sysroots";
    paths = [ rustc ] ++ sysroots;
    pathsToLink = [ "/lib" "/share" ];
    #buildInputs = [ makeWrapper ];
    # Can't use wrapper script because of https://github.com/rust-lang/rust/issues/31943
    postBuild = ''
      mkdir -p $out/bin/
      cp ${rustc}/bin/* $out/bin/
    '';
  };

  rust-std = { date ? defaultDate, system ? thisSys, ... } @ args:
      stdenv.mkDerivation rec {
    # Strip install.sh, etc
    pname = "rust-std";
    version = "nightly-${date}";
    name = "${pname}-${version}-${system}";
    src = fetch (args // {
      inherit pname date system;
      archive = "https://static.rust-lang.org/dist";
    });
    installPhase = ''
      mkdir -p $out
      mv ./*/* $out/
      rm $out/manifest.in
    '';
  };

  cargo = generic {
    pname = "cargo";
    archive = "https://static.rust-lang.org/dist";
    exes = [ "cargo" ];
  };

  rust = generic {
    pname = "rust";
    archive = "https://static.rust-lang.org/dist";
    exes = [ "rustc" "rustdoc" "cargo" ];
  };
}