Skip to content

AndroidAppsUsedByMyself/nix-termux

 
 

Repository files navigation

Nix for Termux (Multi-architecture)

Note: this project is generated by LLM Claude

Run the Nix package manager on Android through Termux without root access. Uses a custom prefix at /data/data/com.termux/files/nix instead of /nix.

Supports multiple architectures: aarch64 (ARM64), armv7l (ARM 32-bit), x86_64 (Intel/AMD 64-bit), i686 (Intel/AMD 32-bit)

Quick Install

curl -fsSL https://raw.githubusercontent.com/mio-19/nix-termux/main/install-nix-termux.sh | bash

Requirements: Android device with Termux installed, 2-3 GB free space.

After installation: source ~/.bashrc (or restart Termux)

How It Works

  • Custom Store Prefix: Uses /data/data/com.termux/files/nix instead of /nix (no root required)
  • Multi-architecture Support: Pre-built packages for aarch64, armv7l, x86_64, and i686
  • Patchelf: ELF interpreter paths are rewritten at build time to point to the custom prefix
  • Complete Bootstrap: Includes Nix 2.31.2, GCC toolchain, and essential utilities

Requirements

  • Android device with Termux installed
  • At least 2-3 GB free storage
  • Internet connection (for future package builds)
  • Supported architecture: aarch64, armv7l, x86_64, or i686

Building from Source

Note: Requires a Linux system with Nix installed.

# Clone this repository
git clone https://github.com/mio-19/nix-termux.git
cd nix-termux

# Build for specific architecture (takes several hours)
./build.sh -a aarch64

# Build for all architectures
./build.sh -A

# See all options
./build.sh --help

The output will be in: result/nix-termux-<arch>.tar.gz

Pre-built releases are available on the releases page.

Installation on Termux

Option 1: One-Line Install (Recommended)

The easiest way to install is using the automated installer script:

curl -fsSL https://raw.githubusercontent.com/mio-19/nix-termux/main/install-nix-termux.sh | bash

Or with wget:

wget -qO- https://raw.githubusercontent.com/mio-19/nix-termux/main/install-nix-termux.sh | bash

The script will automatically detect your architecture and download the appropriate package.

Option 2: Manual Installation

Download from releases page for your architecture:

# Download and extract (example for aarch64)
curl -LO https://github.com/mio-19/nix-termux/releases/latest/download/nix-termux-aarch64-*.tar.gz
tar -xzf nix-termux-aarch64-*.tar.gz
cd tarball

# Run installer
./install.sh

# Reload shell
source ~/.bashrc

Usage

Installing Packages

Since we're using a custom store path, the official binary cache won't work. All packages must be built from source:

# Install a package (will build from source)
nix-env -iA nixpkgs.hello

# Search for packages
nix-env -qaP | grep python

# Update all packages
nix-env -u '*'

Using with nixpkgs

Clone nixpkgs for local package builds:

cd ~
git clone https://github.com/NixOS/nixpkgs.git --depth 1
cd nixpkgs

# Install from local nixpkgs
nix-env -f . -iA hello

Garbage Collection

Free up space by removing unused packages:

# List old generations
nix-env --list-generations

# Delete old generations
nix-env --delete-generations old

# Run garbage collector
nix-collect-garbage

# Aggressive cleanup (remove everything not currently in use)
nix-collect-garbage -d

Configuration

Edit /data/data/com.termux/files/nix/etc/nix/nix.conf to customize:

  • Build settings (max-jobs, cores)
  • Storage optimizations
  • Custom binary caches (if you set up your own)

Technical Details

  • Custom Prefix: Termux cannot access /nix/store without root, so we use /data/data/com.termux/files/nix
  • ELF Patching: Interpreter paths are rewritten using patchelf to point to the custom store
  • Cross Compilation: Built using Nix's cross-compilation support for all architectures
  • No Binary Cache: Must build packages from source due to custom paths

Limitations

  • No Binary Cache: Must build all packages from source
  • Slow Builds: Compiling on mobile hardware takes time
  • Architecture Support: Limited to architectures supported by Termux
  • Android Environment: Some packages may not work due to Android's unique environment

Troubleshooting

"nix-env: command not found"

Ensure you've sourced the environment setup:

source ~/termux-nix-env.sh

Database Errors

Re-initialize the database:

nix-store --init
nix-store --load-db < /data/data/com.termux/files/nix/var/nix/db/db.sqlite

Build Failures

Check logs:

# View build logs
nix-store --read-log /nix/store/...-package-name

Out of Space

Run garbage collection:

nix-collect-garbage -d

License

MIT License. Nix itself is licensed under LGPL 2.1.

Related Projects

Acknowledgments

Based on dramforever's bootstrap approach. Built with Nix.

About

is it hard to bootstrap nix with prefix /data/data/com.termux/files/nix

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 58.1%
  • Nix 41.9%