Skip to content

mio-19/nix-termux

Repository files navigation

Nix for Termux (aarch64-linux) - Working in progress/incomplete

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.

aarch64 (ARM64) devices only.

Quick Install

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

Requirements: aarch64 Android device, 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)
  • Native aarch64 Build: Built natively on ARM64 to ensure correct paths
  • 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 aarch64 (ARM64) architecture
  • Termux app installed
  • At least 2-3 GB free storage
  • Internet connection (for future package builds)

Building from Source

Note: Requires an aarch64-linux system (ARM64 Linux or NixOS).

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

# Build (takes several hours)
./build.sh

The output will be: result/nix-termux-aarch64.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 handle everything automatically, including downloading the latest release, verifying your system, and configuring your environment.

Option 2: Manual Installation

Download from releases page:

# Download and extract
curl -LO <release-url>
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
  • Native Build: Built on aarch64-linux to ensure all paths are correct
  • 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: Only aarch64-linux supported
  • 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

 
 
 

Contributors