Convert Figma logo to code with AI

nix-community logonix-on-droid

Nix-enabled environment for your Android device. [maintainers=@t184256,@Gerschtli]

1,248
68
1,248
87

Top Related Projects

Termux - a terminal emulator application for Android OS extendible by variety of packages.

Manage a user environment using Nix [maintainer=@rycee]

12,081

Nix, the purely functional package manager

17,319

Nix Packages collection & NixOS

1,267

Nix User Repository: User contributed nix packages [maintainer=@Mic92]

A fast, persistent use_nix/use_flake implementation for direnv [maintainer=@Mic92 / @bbenne10]

Quick Overview

Nix-on-Droid is a project that brings the Nix package manager and NixOS-style system configuration to Android devices. It allows users to create a customizable Linux environment on Android, leveraging the power of Nix for reproducible and declarative system management.

Pros

  • Provides a full Linux environment on Android devices without rooting
  • Offers declarative and reproducible system configuration using Nix
  • Enables access to a vast ecosystem of packages from the Nixpkgs repository
  • Allows for easy customization and portability of development environments

Cons

  • Requires some familiarity with Nix and Linux systems
  • May have performance limitations compared to native Android apps
  • Limited integration with Android's native features and services
  • Potential for increased battery consumption and storage usage

Getting Started

To get started with Nix-on-Droid:

  1. Install the Nix-on-Droid app from the Google Play Store or F-Droid.
  2. Open the app and follow the initial setup process.
  3. Once setup is complete, you can start using Nix commands in the terminal.

To install a package:

nix-env -iA nixpkgs.package-name

To update your system:

nix-channel --update
nix-env -u

To customize your environment, edit the ~/.config/nixpkgs/home.nix file:

{ pkgs, ... }:

{
  home.packages = with pkgs; [
    git
    vim
    tmux
  ];
}

After making changes, apply them with:

home-manager switch

For more advanced usage and configuration options, refer to the project's documentation on GitHub.

Competitor Comparisons

Termux - a terminal emulator application for Android OS extendible by variety of packages.

Pros of Termux

  • Larger user base and more active community support
  • Wider range of pre-built packages available
  • Easier setup and more intuitive for beginners

Cons of Termux

  • Less flexibility in package management compared to Nix
  • Limited reproducibility and declarative configuration options
  • Potential security concerns due to less strict package isolation

Code Comparison

Termux package installation:

pkg install package-name

Nix-on-Droid package installation:

{ pkgs, ... }:

{
  environment.packages = [ pkgs.package-name ];
}

Termux focuses on simplicity and ease of use, making it more accessible for users new to Linux environments on Android. It offers a wide range of pre-built packages and has a larger community, which can be beneficial for troubleshooting and support.

Nix-on-Droid, on the other hand, leverages the power of the Nix package manager, providing better reproducibility and more advanced package management features. It offers greater flexibility in system configuration and package isolation, which can be advantageous for more experienced users or those requiring a more customizable environment.

The code comparison illustrates the difference in package installation approaches. Termux uses a simple command-line interface, while Nix-on-Droid employs a declarative configuration style, typical of Nix environments.

Manage a user environment using Nix [maintainer=@rycee]

Pros of home-manager

  • Broader scope, managing entire user environments across various systems
  • More extensive documentation and community support
  • Integrates well with NixOS and other Nix-based systems

Cons of home-manager

  • Steeper learning curve for new users
  • May be overkill for simple configurations or single-device setups
  • Not specifically designed for Android environments

Code Comparison

home-manager configuration example:

{ config, pkgs, ... }:
{
  home.packages = [ pkgs.htop pkgs.git ];
  programs.bash.enable = true;
}

nix-on-droid configuration example:

{ pkgs, ... }:
{
  environment.packages = [ pkgs.htop pkgs.git ];
  nix.extraOptions = "experimental-features = nix-command flakes";
}

Both projects use Nix for configuration management, but home-manager focuses on user-specific configurations across systems, while nix-on-droid is tailored for Android environments. home-manager offers more flexibility and broader application, making it suitable for managing complex user environments. nix-on-droid, on the other hand, provides a more specialized solution for running Nix on Android devices, with optimizations and features specific to the mobile platform.

12,081

Nix, the purely functional package manager

Pros of nix

  • More mature and widely adopted project with a larger community
  • Supports multiple operating systems, not limited to Android
  • Offers a more comprehensive package management and system configuration solution

Cons of nix

  • Steeper learning curve for new users
  • Requires more system resources and storage space
  • Not specifically optimized for mobile devices or Android environments

Code comparison

nix:

{ pkgs ? import <nixpkgs> {} }:
pkgs.stdenv.mkDerivation {
  name = "example";
  buildInputs = [ pkgs.hello ];
}

nix-on-droid:

{ pkgs, ... }:

{
  environment.packages = [ pkgs.hello ];
}

The nix example shows a typical derivation for building a package, while the nix-on-droid example demonstrates how to add packages to the environment. nix-on-droid simplifies the process for Android-specific use cases, making it more accessible for mobile environments.

17,319

Nix Packages collection & NixOS

Pros of nixpkgs

  • Larger ecosystem with a vast collection of packages and configurations
  • More comprehensive documentation and community support
  • Broader scope, covering entire system configurations beyond just Android

Cons of nixpkgs

  • Not specifically tailored for Android environments
  • Potentially more complex to set up and use on Android devices
  • May include unnecessary components for Android-specific use cases

Code Comparison

nixpkgs:

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [ pkgs.hello pkgs.cowsay ];
}

nix-on-droid:

{ pkgs, ... }:

{
  environment.packages = [ pkgs.hello pkgs.cowsay ];
}

The code snippets demonstrate how both projects use Nix expressions to define package installations. nixpkgs uses a more generic approach with mkShell, while nix-on-droid focuses on the environment.packages attribute specific to the Android environment.

nix-on-droid is designed specifically for running Nix on Android devices, offering a more streamlined experience for Android users. It provides a tailored solution for managing packages and configurations on Android, but with a more limited scope compared to the broader nixpkgs ecosystem.

nixpkgs, on the other hand, offers a comprehensive package management solution for various platforms, including but not limited to Android. It provides a wider range of packages and configuration options, making it more versatile but potentially more complex for Android-specific use cases.

1,267

Nix User Repository: User contributed nix packages [maintainer=@Mic92]

Pros of NUR

  • Larger community-driven repository with a wide variety of packages
  • Easier to contribute and maintain individual packages
  • Provides a centralized hub for Nix users to share and discover custom packages

Cons of NUR

  • Less focused on a specific use case or platform
  • May require more effort to integrate into existing Nix setups
  • Potential for package conflicts or inconsistencies due to diverse contributors

Code Comparison

NUR:

{ pkgs ? import <nixpkgs> {} }:

{
  example-package = pkgs.callPackage ./example-package {};
}

nix-on-droid:

{ pkgs ? import <nixpkgs> {} }:

{
  environment.packages = with pkgs; [
    # Android-specific packages
  ];
}

While both repositories use Nix, NUR focuses on providing a collection of user repositories, whereas nix-on-droid is tailored specifically for running Nix on Android devices. The code examples reflect this difference, with NUR showcasing a typical package definition and nix-on-droid demonstrating Android-specific configuration.

A fast, persistent use_nix/use_flake implementation for direnv [maintainer=@Mic92 / @bbenne10]

Pros of nix-direnv

  • Works on any Unix-like system, not limited to Android
  • Integrates with existing shell environments and workflows
  • Lighter weight and less intrusive than a full Android environment

Cons of nix-direnv

  • Requires existing Nix installation and shell setup
  • Limited to development environment management, not a full system solution
  • May require more manual configuration for complex setups

Code Comparison

nix-direnv:

use_nix() {
  local path="$(nix-instantiate --find-file nixpkgs)"
  if [ -f "${path}/.version-suffix" ]; then
    local version="$(< $path/.version-suffix)"
  elif [ -f "${path}/.git" ]; then
    local version="$(cd $path && git describe --always --dirty)"
  fi
}

nix-on-droid:

{ pkgs, ... }:

{
  environment.packages = with pkgs; [
    vim
    git
    tmux
  ];
  
  home-manager.users.nix-on-droid = { ... }: {
    home.stateVersion = "22.05";
  };
}

The code snippets demonstrate the different focus of each project. nix-direnv is centered around environment activation, while nix-on-droid provides a full system configuration approach for Android devices.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Nix-on-Droid

Get it on F-Droid

Nix package manager on Android, in a single-click installable package. This is not full NixOS running inside Android, but you get easy access to nixpkgs' vast collection of (precompiled!) software and the best package manager under the sun. It's prototype-grade quality as of now, but hey, it works!

It does not require root, user namespaces support or disabling SELinux, but it relies on proot and other hacks instead. It uses a fork of Termux-the-terminal-emulator app, but has no relation to Termux-the-distro. Please do not pester Termux folks about Nix-on-Droid.

This repository contains:

  1. Nix expressions that generate a bootstrap zipball, which is then used to install Nix package manager on Android along with the nix-on-droid executable.
  2. A module system for configuring the local Nix-on-Droid installation directly on the device.

It is only tested with aarch64 (64-bit ARM devices). It also used to compile for i686 devices, but the developers don't own any and nobody has reported whether it actually worked or not, so it's no longer built unless a user shows up. Sorry, it would not work on 32-bit ARM devices and it's not an easy feat to pull off.

Try it out

Install it from F-Droid, launch the app, press OK, expect many hundreds megabytes of downloads to happen.

Nix-on-Droid and the module system

Config file

The Nix-on-Droid system can be managed through a custom config file in ~/.config/nixpkgs/nix-on-droid.nix as generated on first build, for example:

{ pkgs, ... }:

{
  environment.packages = [ pkgs.vim ];
  system.stateVersion = "24.05";
}

An alternative location is ~/.config/nixpkgs/config.nix with the key nix-on-droid, for example:

{
  nix-on-droid =
    { pkgs, ... }:

    {
      environment.packages = [ pkgs.vim ];
      system.stateVersion = "24.05";
    };
}

See https://nix-community.github.io/nix-on-droid/ for list of all available options.

home-manager integration

To enable home-manager you simply need to follow the instructions already provided in the example nix-on-droid.nix:

  1. Add home-manager channel:
    nix-channel --add https://github.com/nix-community/home-manager/archive/release-24.05.tar.gz home-manager
    nix-channel --update
    
  2. Configure home-manager:
    { pkgs, ... }:
    
    {
      # Read Nix-on-Droid changelog before changing this value
      system.stateVersion = "24.05";
    
      # insert Nix-on-Droid config
    
      home-manager.config =
        { pkgs, ... }:
        {
          # Read home-manager changelog before changing this value
          home.stateVersion = "24.05";
    
          # insert home-manager config
        };
    
      # or if you have a separate home.nix already present:
      home-manager.config = ./home.nix;
    }
    

nix-on-droid executable

This executable is responsible for activating new configurations: Use nix-on-droid switch to activate the current configuration and nix-on-droid rollback to rollback to the latest build.

For more information, please run nix-on-droid help.

Build Nix-on-Droid on your own

The terminal emulator part is probably not interesting for you, just download and use a prebuilt one. If you really want to rebuild it, you can just use Android Studio for that.

The zipball generation is probably what you are after. Get an x86_64 computer with flake-enabled Nix.

tl;dr: Use the deploy app like the following which executes all steps mentioned below:

nix run ".#deploy" -- <public_url> <rsync_target>
# or run the following for explanation of this script
nix run ".#deploy"

Run

nix build ".#bootstrapZip-aarch64" --impure

Put the zip file from result on some HTTP server and specify the parent directory URL during the installation. To re-trigger the installation, you can use 'clear data' on the Android app (after backing stuff up, obviously).

If you want to change the Nix-on-Droid channel to your custom one, you can do that either with nix-channel after the installation, or by setting the environment variable NIX_ON_DROID_CHANNEL_URL. Other environment variables are NIXPKGS_CHANNEL_URL an NIX_ON_DROID_FLAKE_URL.

Note: The proot binary is not built on the android device (NDK is required for building it, and it's not available on mobile platforms). The way we work around it is to push proot derivation to cachix. The current workaround is to hardcode the path to the wanted proot nix store path in modules/environment/login/default.nix. During evaluation time on the android device this store path will be downloaded from the binary cache (https://nix-on-droid.cachix.org/). This in return means the proot derivation has to be present there or in any other binary cache configured in the nix.conf on the device.

Obviously it's an annoyance if one wants to fork this repo and test something. To minimize the hassle with this scenario, proot derivation is also bundled with the bootstrap zipball. This way you only need your own binary cache if you are planning to maintain a long-term fork that users can update from. In case you only care about updates through wiping the data, or are forking to submit a one-off pull request, you shouldn't need a binary cache for that.

Nix flakes

Note: Nix flake support is still experimental at the moment and subject to change.

Examples / templates

A minimal example could look like the following:

{
  description = "Minimal example of Nix-on-Droid system config.";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";

    nix-on-droid = {
      url = "github:nix-community/nix-on-droid/release-24.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, nix-on-droid }: {

    nixOnDroidConfigurations.default = nix-on-droid.lib.nixOnDroidConfiguration {
      pkgs = import nixpkgs { system = "aarch64-linux"; };
      modules = [ ./nix-on-droid.nix ];
    };

  };
}

For more examples and nix flake templates, see templates directory or explore with:

nix flake init --template github:nix-community/nix-on-droid#advanced

Usage with nix-on-droid

Use nix-on-droid switch --flake path/to/flake#device to build and activate your configuration (path/to/flake#device will expand to .#nixOnDroidConfigurations.device). If you run nix-on-droid switch --flake path/to/flake, the default configuration will be used.

Note: Currently, Nix-on-Droid can not be built with an pure flake build because of hardcoded store paths for proot. Therefore, every evaluation of a flake configuration will be executed with --impure flag. (This behaviour will be dropped as soon as the default setup does not require it anymore.)

Testing

In the ./tests/on-device directory, there is small set of bats tests that can be executed on a real or emulated android device.

To run the tests, execute

nix-on-droid on-device-test

Note: This currently requires a channel setup and should only be executed on clean, disposable installations.

Tips

  • To grant the app access to the storage, use the toggle in the app settings (reachable from Android settings).
  • If the terminal freezes, use 'Acquire wakelock' button in the notification and/or tone down your device's aggressive power saving measures.
  • We have a wiki with tips and success stories, you're encouraged to add yours as well.

Technical overview

OK, real brief.

Developer's device:

  1. proot for the target platform is cross-compiled against bionic, (to fake file paths like /nix/store; think 'userspace chroot')
  2. Target nix is taken from the original release tarball
  3. Target nix database is initialized
  4. Support scripts and config files are built with nix and the Nix-on-Droid module system
  5. From these, a bootstrap zipball is built and published on an HTTP server

User's device:

  1. Android app is installed and launched, bootstrap URL is entered
  2. Bootstrap zipball gets downloaded and unpacked
  3. 'First boot' begins, Nix builds the environment (or, possibly, pulls it from Cachix)
  4. Nix installs the environment (login scripts, config files, etc.)

You can refer to a NixCon 2019 presentation talk for a more extensive overview of the subject.

Licensing and credits

Licensed under MIT License, see LICENSE. Copyright (c) 2019-2021 Alexander Sosedkin and other contributors, see AUTHORS.

Two rewrites ago it was based off the official Nix install script (https://nixos.org/nix/install), presumably written by Eelco Dolstra.

Is deployed and used with a fork of Termux-the-terminal-emulator app, but has no relation to Termux-the-distro.

Previous project that did use Termux-the-distro: https://github.com/t184256/nix-in-termux