|
| 1 | +# React Native Development Nix Environment Setup |
| 2 | + |
| 3 | +This document outlines how to set up your React Native development environment using Nix flakes for reproducibility and isolation. |
| 4 | + |
| 5 | +## Disclaimer |
| 6 | + |
| 7 | +This was tested on an Arch Linux system with AMD CPU and GPU. There are likely problems with NVIDIA GPUs. |
| 8 | + |
| 9 | +Compatibility with Microsoft WSL2 is not guaranteed, but should work withoud major issues, except for the emulator. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +* **Nix installed**: [https://nixos.org/download.html](https://nixos.org/download.html) |
| 14 | +* **Git**: Required for cloning the repository. |
| 15 | +* **KVM (for emulator, if enabled)**: Ensure KVM is enabled and your user is in the `kvm` group for optimal emulator performance: |
| 16 | + ```bash |
| 17 | + sudo usermod -aG kvm $USER && newgrp kvm # (or log out/in) |
| 18 | + kvm-ok # Verify KVM acceleration |
| 19 | + ``` |
| 20 | +* **GPU Drivers (for emulator, if enabled)**: For hardware-accelerated emulator (AMD/Intel), ensure host Vulkan drivers are working: |
| 21 | + ```bash |
| 22 | + vulkaninfo | grep -i "device name" # Should show your GPU |
| 23 | + ``` |
| 24 | + If not, install `vulkan-radeon` (AMD) or `vulkan-intel` (Intel) on your host system and reboot. |
| 25 | + |
| 26 | + |
| 27 | +## Getting Started |
| 28 | + |
| 29 | +1. **Clone the Project:** |
| 30 | + ```bash |
| 31 | + git clone <your-fork-url> |
| 32 | + cd <your-fork-name> |
| 33 | + ``` |
| 34 | + |
| 35 | +2. **Enter the Development Shell:** |
| 36 | + |
| 37 | + If you have `npm` or `yarn` installed, use the defined scripts: |
| 38 | + * **For Physical Device Only (default):** |
| 39 | + ```bash |
| 40 | + npm run nix # or yarn nix |
| 41 | + ``` |
| 42 | + * **For Emulator Development (explicitly enable):** |
| 43 | + ```bash |
| 44 | + npm run nix-emulator # or yarn nix-emulator |
| 45 | + ``` |
| 46 | + |
| 47 | + If you do not have `npm`/`yarn` installed, execute the commands manually: |
| 48 | + * **For Physical Device Only (default):** |
| 49 | + ```bash |
| 50 | + nix develop --extra-experimental-features nix-command --extra-experimental-features flakes |
| 51 | + ``` |
| 52 | + * **For Emulator Development (explicitly enable):** |
| 53 | + ``` |
| 54 | + nix develop .#emulator --extra-experimental-features nix-command --extra-experimental-features flakes |
| 55 | + ``` |
| 56 | + |
| 57 | + The first time, this will download and set up all required tools, which can take considerable time. Subsequent entries will be much faster. You will also see a message about "Setting up writable Android SDK..." which only happens once. |
| 58 | + |
| 59 | +## Development Workflow |
| 60 | + |
| 61 | +Once inside the `nix develop` shell, all tools are available. |
| 62 | + |
| 63 | +### 1. Start Metro Bundler |
| 64 | + |
| 65 | +Open a new terminal or run in the background: |
| 66 | +```bash |
| 67 | +npm start |
| 68 | +``` |
| 69 | + |
| 70 | +### 2. Physical Device Development |
| 71 | + |
| 72 | +1. **Connect Device**: Enable USB debugging on your Android device and connect it via USB. |
| 73 | +2. **Verify Device**: |
| 74 | + ```bash |
| 75 | + adb devices |
| 76 | + ``` |
| 77 | +3. **Mirror Screen (Optional)**: |
| 78 | + ```bash |
| 79 | + scrcpy # Mirrors screen, provides control |
| 80 | + scrcpy -s <device_serial> # If multiple devices connected |
| 81 | + ``` |
| 82 | +4. **Run App**: |
| 83 | + ```bash |
| 84 | + npx run android |
| 85 | + ``` |
| 86 | + |
| 87 | +### 3. Android Emulator Development (if enabled) |
| 88 | + |
| 89 | +1. **Launch Emulator**: |
| 90 | + ```bash |
| 91 | + emulator -avd ReactNative_API35 -gpu host -no-metrics -no-audio |
| 92 | + ``` |
| 93 | + * Use `-gpu swiftshader_indirect` if hardware acceleration (`-gpu host`) fails. |
| 94 | + |
| 95 | +2. **Verify Emulator**: |
| 96 | + ```bash |
| 97 | + adb devices |
| 98 | + ``` |
| 99 | +3. **Run App**: |
| 100 | + Launches the the emulater automatically, if it is not already running. |
| 101 | + ```bash |
| 102 | + npx run android |
| 103 | + ``` |
0 commit comments