summaryrefslogtreecommitdiff
path: root/nix/esp-rs.nix
blob: 619c6c41fbaa828e164d0f3c7476e7dd66808cc3 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# MIT licensed
# code taken from https://git.sr.ht/~olk/esp-rs-nix/blob/main/esp-rs.nix
{
  pkgs ? import <nixpkgs> {},
  lib ? pkgs.lib,
}: let
  versions = {
    esp-rust = {
      version = "1.93.0.0";
      hash = "sha256-837shgsDonrmUqZsIhUoR+93IR8tUat67PiHfShilJc=";
    };
    rust-src = {
      version = "1.93.0.0";
      hash = "sha256-upKiKRDvubE8obME4OVewTr6PjS/p0QOqjpQfEWBfE0=";
    };
    gcc = {
      version = "14.2.0_20241119";
      xtensa-hash = "sha256-pX2KCnUoGZtgqFmZEuNRJxDMQgqYYPRpswL3f3T0nWE=";
      riscv32-hash = "sha256-67O34FYUnVG2nfkfQj2yH874qDSYx4F/16xxPi0kNbY=";
    };
    defaultArch = "x86_64-unknown-linux-gnu";
    gccArch = "x86_64-linux-gnu";
  };

  mkGccToolchain = {
    name,
    arch,
    hash,
  }:
    pkgs.stdenv.mkDerivation {
      pname = "esp-${name}-gcc";
      version = versions.gcc.version;

      src = pkgs.fetchzip {
        url = "https://github.com/espressif/crosstool-NG/releases/download/esp-${versions.gcc.version}/${arch}-esp-elf-${versions.gcc.version}-${versions.gccArch}.tar.xz";
        inherit hash;
      };

      installPhase = ''
        runHook preInstall
        mkdir -p $out
        cp -r ./* $out/
        runHook postInstall
      '';

      meta = with lib; {
        description = "ESP32 ${name} GCC toolchain";
        homepage = "https://github.com/espressif/crosstool-NG";
        license = licenses.gpl2Plus;
        platforms = ["x86_64-linux"];
      };
    };

  esp-rust-build = pkgs.stdenv.mkDerivation {
    pname = "esp-rust-build";
    version = versions.esp-rust.version;

    src = pkgs.fetchzip {
      url = "https://github.com/esp-rs/rust-build/releases/download/v${versions.esp-rust.version}/rust-${versions.esp-rust.version}-${versions.defaultArch}.tar.xz";
      hash = versions.esp-rust.hash;
    };

    nativeBuildInputs = with pkgs; [autoPatchelfHook];
    buildInputs = with pkgs; [
      stdenv.cc.cc
      zlib
    ];

    patchPhase = ''
      patchShebangs ./install.sh
    '';

    installPhase = ''
      runHook preInstall
      mkdir -p $out
      ./install.sh --destdir=$out --prefix="" --disable-ldconfig --without=rust-docs-json-preview,rust-docs
      runHook postInstall
    '';

    meta = with lib; {
      description = "ESP32 Rust toolchain";
      homepage = "https://github.com/esp-rs/rust-build";
      license = with licenses; [
        mit
        asl20
      ];
      platforms = ["x86_64-linux"];
    };
  };

  esp-xtensa-gcc = mkGccToolchain {
    name = "xtensa";
    arch = "xtensa";
    hash = versions.gcc.xtensa-hash;
  };

  esp-riscv32-gcc = mkGccToolchain {
    name = "riscv32";
    arch = "riscv32";
    hash = versions.gcc.riscv32-hash;
  };

  rust-src = pkgs.fetchzip {
    url = "https://github.com/esp-rs/rust-build/releases/download/v${versions.rust-src.version}/rust-src-${versions.rust-src.version}.tar.xz";
    hash = versions.rust-src.hash;
  };

  esp-rs-base = pkgs.symlinkJoin {
    name = "esp-rs-base-${versions.esp-rust.version}";
    paths = [
      esp-rust-build
      esp-xtensa-gcc
      esp-riscv32-gcc
    ];
    meta = {
      description = "Base ESP32 toolchain components";
      platforms = ["x86_64-linux"];
    };
  };
in
  pkgs.stdenv.mkDerivation {
    pname = "esp-rs";
    version = versions.esp-rust.version;

    src = rust-src;

    nativeBuildInputs = with pkgs; [
      autoPatchelfHook
      pkg-config
    ];

    buildInputs = with pkgs; [
      stdenv.cc.cc
      zlib
    ];

    autoPatchelfIgnoreMissingDeps = ["*"];

    patchPhase = ''
      patchShebangs ./install.sh
    '';

    installPhase = ''
      runHook preInstall

      # Start with the base toolchain
      mkdir -p $out
      cp -rL ${esp-rs-base}/* $out/
      chmod -R u+w $out

      # Install rust-src on top
      ./install.sh --destdir=$out --prefix="" --disable-ldconfig

      runHook postInstall
    '';

    passthru = {
      inherit esp-rust-build esp-xtensa-gcc esp-riscv32-gcc;
      version = versions.esp-rust.version;
    };

    meta = with lib; {
      description = "Complete ESP32 Rust development toolchain";
      longDescription = ''
        A complete toolchain for developing Rust applications on ESP32 microcontrollers.
        Includes Rust compiler with ESP32 targets, Xtensa and RISC-V GCC toolchains,
        and all necessary components for no_std ESP-HAL development.
      '';
      homepage = "https://github.com/esp-rs";
      license = with licenses; [
        mit
        asl20
        gpl2Plus
      ];
      platforms = ["x86_64-linux"];
      maintainers = [];
    };
  }