├── .gitignore ├── README.md ├── flake.lock └── flake.nix /.gitignore: -------------------------------------------------------------------------------- 1 | /result* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esp32 2 | 3 | Rust with Xtensa support for Nix using flakes. 4 | 5 | ``` 6 | $ nix flake show github:knarkzel/esp32 7 | └───packages 8 | └───x86_64-linux 9 | └───esp32: package 'esp32' 10 | $ nix build github:knarkzel/esp32#esp32 11 | $ ls -a result 12 | . .. .cargo .rustup 13 | ``` 14 | 15 | ## Minimal example 16 | 17 | ```nix 18 | { 19 | inputs = { 20 | nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; 21 | esp32 = { 22 | url = "github:knarkzel/esp32"; 23 | inputs.nixpkgs.follows = "nixpkgs"; 24 | }; 25 | }; 26 | 27 | outputs = { 28 | self, 29 | nixpkgs, 30 | esp32, 31 | }: let 32 | pkgs = import nixpkgs {system = "x86_64-linux";}; 33 | idf-rust = esp32.packages.x86_64-linux.esp32; 34 | in { 35 | devShells.x86_64-linux.default = pkgs.mkShell { 36 | buildInputs = [ 37 | idf-rust 38 | ]; 39 | 40 | shellHook = '' 41 | export PATH="${idf-rust}/.rustup/toolchains/esp/bin:$PATH" 42 | export RUST_SRC_PATH="$(rustc --print sysroot)/lib/rustlib/src/rust/src" 43 | ''; 44 | }; 45 | }; 46 | } 47 | ``` 48 | 49 | ## Getting started 50 | 51 | Use the above flake, then follow the [Rust on ESP Book](https://esp-rs.github.io/book/writing-your-own-application/generate-project-from-template.html). 52 | 53 | ## Notes 54 | 55 | When building from source, you need a huge amount of memory, about 36 GB. 56 | To create temporary swap, use following commands: 57 | 58 | ``` 59 | $ fallocate -l 36G /tmp/swap; mkswap /tmp/swap; swapon /tmp/swap 60 | ``` 61 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "nixpkgs": { 4 | "locked": { 5 | "lastModified": 1724819573, 6 | "narHash": "sha256-GnR7/ibgIH1vhoy8cYdmXE6iyZqKqFxQSVkFgosBh6w=", 7 | "owner": "NixOS", 8 | "repo": "nixpkgs", 9 | "rev": "71e91c409d1e654808b2621f28a327acfdad8dc2", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "owner": "NixOS", 14 | "ref": "nixos-unstable", 15 | "repo": "nixpkgs", 16 | "type": "github" 17 | } 18 | }, 19 | "root": { 20 | "inputs": { 21 | "nixpkgs": "nixpkgs" 22 | } 23 | } 24 | }, 25 | "root": "root", 26 | "version": 7 27 | } 28 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | inputs = { 3 | nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; 4 | }; 5 | 6 | outputs = { 7 | self, 8 | nixpkgs, 9 | }: let 10 | pkgs = nixpkgs.legacyPackages.x86_64-linux; 11 | esp32 = pkgs.dockerTools.pullImage { 12 | imageName = "espressif/idf-rust"; 13 | imageDigest = "sha256:0e1fba439a7d4cee57c5822feda8cd09ec38d55b40e402273d5c10477e53ddd4"; 14 | sha256 = "rUumcjsV3rKmQeGpMK93srfuQpf4OMXvbqTszJa2BXg="; 15 | finalImageName = "espressif/idf-rust"; 16 | finalImageTag = "all_latest"; 17 | }; 18 | in { 19 | packages.x86_64-linux.esp32 = pkgs.stdenv.mkDerivation { 20 | name = "esp32"; 21 | src = esp32; 22 | unpackPhase = '' 23 | mkdir -p source 24 | tar -C source -xvf $src 25 | ''; 26 | sourceRoot = "source"; 27 | nativeBuildInputs = [ 28 | pkgs.autoPatchelfHook 29 | pkgs.jq 30 | ]; 31 | buildInputs = [ 32 | pkgs.xz 33 | pkgs.zlib 34 | pkgs.libxml2 35 | pkgs.python3 36 | pkgs.libudev-zero 37 | pkgs.stdenv.cc.cc 38 | ]; 39 | buildPhase = '' 40 | jq -r '.[0].Layers | @tsv' < manifest.json > layers 41 | ''; 42 | installPhase = '' 43 | mkdir -p $out 44 | for i in $(< layers); do 45 | tar -C $out -xvf "$i" home/esp/.cargo home/esp/.rustup || true 46 | done 47 | mv -t $out $out/home/esp/{.cargo,.rustup} 48 | rmdir $out/home/esp 49 | rmdir $out/home 50 | export PATH=$out/.rustup/toolchains/esp/bin:$PATH 51 | export PATH=$out/.rustup/toolchains/esp/xtensa-esp-elf-esp-13.2.0_20230928/stensa-esp-elf/bin:$PATH 52 | export RUST_SRC_PATH="$(rustc --print sysroot)/lib/rustlib/src/rust/src" 53 | 54 | # [ -d $out/.cargo ] && [ -d $out/.rustup ] 55 | ''; 56 | }; 57 | }; 58 | } 59 | --------------------------------------------------------------------------------