Bundle Nix derivations to run anywhere!
Go to file
Artturin 4f6330b207
Merge pull request #113 from Artturin/nixbundleapi
flake: Use new nix bundle api
2024-10-16 19:12:21 +03:00
nix-user-chroot User proper compiler for nix-user-chroot 2023-03-01 15:26:15 +03:00
.gitignore Mod .gitignore. 2016-06-15 18:53:25 +00:00
appdir.nix remove usage of stdenv.lib 2021-07-19 12:00:00 +00:00
appdir.sh Fix appimage stuff 2017-04-30 02:32:58 -05:00
appimage-bundle.nix Simplify appimage generation 2019-06-26 17:32:46 -04:00
appimage-top.nix Fix with latest nixpkgs, use musl-native pkgs 2018-03-03 17:46:12 -06:00
appimage.nix try to fix appimage generation 2017-11-07 22:29:52 -06:00
appimagetool.nix Get dynamic-linker path correctly 2024-10-15 00:35:41 +03:00
AppRun.c symlink /bin to /usr/bin 2020-07-21 11:25:44 +02:00
default.nix Add the drvToBundle pname to the bundle 2024-10-16 00:19:36 +03:00
flake.lock flake.lock: Update 2024-10-15 01:43:48 +03:00
flake.nix Add the drvToBundle pname to the bundle 2024-10-16 00:19:36 +03:00
install-nix-from-closure.sh Add nix installer. 2016-06-15 18:53:47 +00:00
LICENSE Create LICENSE 2017-02-07 00:43:47 -06:00
Makefile Fixup release.nix 2018-03-03 15:16:18 -06:00
nix2appimage.sh Fix appimage stuff 2017-04-30 02:32:58 -05:00
nix-bootstrap.sh nix-bootstrap.sh: fix shellcheck warnings 2020-10-22 15:43:48 +02:00
nix-bundle.sh nix-bundle: print the output path instead 2020-12-02 15:46:49 +01:00
nix-installer.nix Add nix-bundle.sh. 2016-11-26 01:05:43 -06:00
nix-run.sh nix-run.sh: fix shellcheck warnings 2020-10-22 15:43:55 +02:00
nix-strace.sh Add nix-strace.sh (wip) 2017-04-29 19:54:09 -05:00
proot-x86_64 Add nix-bootstrap.sh script. 2017-04-30 15:48:04 -05:00
README.md Add unstable warning 2024-10-15 18:39:25 +03:00
release.nix Release 0.4.0 2020-05-20 11:06:15 -05:00
test-appimage.nix Get dynamic-linker path correctly 2024-10-15 00:35:41 +03:00
test.sh fixes #68 2020-11-29 13:29:02 +01:00

UNSTABLE, Breaking changes may be done without warning.

nix-bundle

nix-bundle is a way to package Nix attributes into single-file executables.

Benefits

  • Single-file output
  • Can be run by non-root users
  • No runtime
  • Distro agnostic
  • No installation

Getting started

Make sure you have installed Nix already. See http://nixos.org/nix/ for more details.

Once you have a working Nix install, you can run:

$ ./nix-bundle.sh hello /bin/hello

hello indicates the Nix derivation from NixPkgs that you want to use, while /bin/hello indicates the path of the executable relative to hello that you want to run. This will create the file "hello". Running it:

$ ./hello
Hello, world!

This is a standalone file that is completely portable! As long as you are running the same architecture Linux kernel and have a shell interpreter available it will run.

Some others to try:

./nix-bundle.sh nano /bin/nano
./nix-bundle.sh emacs /bin/emacs

Or if you want to try graphical applications:

# Simple X game. Very few dependencies. Quick to build and load. ~13MB
./nix-bundle.sh xskat /bin/xskat
./nix-bundle.sh firefox /bin/firefox
# SDL-based game. ~228MB
./nix-bundle.sh ivan /bin/ivan

Self-bundling (meta)

Starting with v0.1.3, you can bundle nix-bundle! To do this, just use nix-bundle normally:

NIX_PATH="nixpkgs=https://github.com/matthewbauer/nixpkgs/archive/nix-bundle.tar.gz" ./nix-bundle.sh nix-bundle /bin/nix-bundle

[Experimental] Create AppImage executables from Nix expressions

"nix-bundle.sh" tends to create fairly large outputs. This is largely because nix-bundle.sh "extracts" its payload up front. AppImage uses a different method where extraction only takes place when the file is accessed (through FUSE and SquashFS). You can now create a compliant "AppImage" using the "nix2appimage.sh" script:

./nix2appimage.sh emacs

This will create a file at Emacs-x86_64.AppImage which you can execute.

Notice that there is only one argument for nix2appimage.sh. This is because the target executable will be detected from the .desktop file in /share/applications/*.desktop. As a side-effect, AppImage requires your package to have a .desktop file, so packages like "hello", "coreutils", etc. will not work.

Some other examples to try:

./nix2appimage.sh firefox
./nix2appimage.sh vlc
./nix2appimage.sh 0ad
./nix2appimage.sh wireshark-gtk

These may take a while because of the large closure size.

Note that these do not currently work out of the box with NixOS. Other Linux distros should work.

Comparison with AppImage, FlatPak, Snap

Name Distro-agnostic Runtime required Root required Storage
nix-bundle yes no no Arx tarball
AppImage yes no no Squashfs w/ lzma compression
FlatPak yes yes no ?
Snap yes yes no squashFS

How it works

Nix-bundle glues together four different projects to work correctly:

  • Arx - an archive execution tool
  • Creates single-file archive executable that can unpack themselves and then run some command. nix-bundle calls nix-user-chroot to bootstrap the Nix environment. It outputs a "./nix" folder.
  • nix-user-chroot - a small bootstrap that uses Linux namespaces to call chroot
    • This will create sub namespace and bind mount the "./nix" to "/nix" so that the Nix references function properly.
  • Nix - a functional package manager
    • Used to build runtime closures that are self-contained.
  • nixpkgs
    • Provides lots of different packages to choose from.

Drawbacks

Nix-bundle has some drawbacks that need to be worked on:

  • Slow startup
  • Large files (Firefox 150MB)
  • Only compatible Linux
  • Outputs built on x86-64 will not run on i386
  • Requires Linux kernel with CAP_SYS_USER_NS on and permissions setup correctly