github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/flake.nix (about)

     1  # A Nix flake file, mainly intended for developing TinyGo.
     2  # You can download Nix here, for use on your Linux or macOS system:
     3  #   https://nixos.org/download.html
     4  # After you have installed Nix, you can enter the development environment as
     5  # follows:
     6  #
     7  #   nix develop
     8  #
     9  # This drops you into a bash shell, where you can install TinyGo simply using
    10  # the following command:
    11  #
    12  #   go install
    13  #
    14  # That's all! Assuming you've set up your $PATH correctly, you can now use the
    15  # tinygo command as usual:
    16  #
    17  #   tinygo version
    18  #
    19  # But you'll need a bit more to make TinyGo actually able to compile code:
    20  #
    21  #   make llvm-source            # fetch compiler-rt
    22  #   git submodule update --init # fetch lots of other libraries and SVD files
    23  #   make gen-device -j4         # build src/device/*/*.go files
    24  #   make wasi-libc              # build support for wasi/wasm
    25  #
    26  # With this, you should have an environment that can compile anything - except
    27  # for the Xtensa architecture (ESP8266/ESP32) because support for that lives in
    28  # a separate LLVM fork.
    29  #
    30  # You can also do many other things from this environment. Building and flashing
    31  # should work as you're used to: it's not a VM or container so there are no
    32  # access restrictions and you're running in the same host environment - just
    33  # with a slightly different set of tools available.
    34  {
    35    inputs = {
    36      # Use a recent stable release, but fix the version to make it reproducible.
    37      # This version should be updated from time to time.
    38      nixpkgs.url = "nixpkgs/nixos-23.11";
    39      flake-utils.url = "github:numtide/flake-utils";
    40    };
    41    outputs = { self, nixpkgs, flake-utils }:
    42      flake-utils.lib.eachDefaultSystem (system:
    43        let
    44          pkgs = nixpkgs.legacyPackages.${system};
    45        in
    46        with pkgs;
    47        {
    48          devShells.default = mkShell {
    49            buildInputs = [
    50              # These dependencies are required for building tinygo (go install).
    51              go
    52              llvmPackages_17.llvm
    53              llvmPackages_17.libclang
    54              llvmPackages_17.libcxx
    55              # Additional dependencies needed at runtime, for building and/or
    56              # flashing.
    57              llvmPackages_17.lld
    58              avrdude
    59              binaryen
    60              # Additional dependencies needed for on-chip debugging.
    61              # These tools are rather big (especially GDB) and not frequently
    62              # used, so are commented out. On-chip debugging is still possible if
    63              # these tools are available in the host environment.
    64              #gdb
    65              #openocd
    66            ];
    67            shellHook= ''
    68              # Configure CLANG, LLVM_AR, and LLVM_NM for `make wasi-libc`.
    69              # Without setting these explicitly, Homebrew versions might be used
    70              # or the default `ar` and `nm` tools might be used (which don't
    71              # support wasi).
    72              export CLANG="clang-17 -resource-dir ${llvmPackages_17.clang.cc.lib}/lib/clang/17"
    73              export LLVM_AR=llvm-ar
    74              export LLVM_NM=llvm-nm
    75  
    76              # Make `make smoketest` work (the default is `md5`, while Nix only
    77              # has `md5sum`).
    78              export MD5SUM=md5sum
    79  
    80              # Ugly hack to make the Clang resources directory available.
    81              export GOFLAGS="\"-ldflags=-X github.com/tinygo-org/tinygo/goenv.clangResourceDir=${llvmPackages_17.clang.cc.lib}/lib/clang/17"\"
    82            '';
    83          };
    84        }
    85      );
    86  }