Add nix-bundle.sh.

This commit is contained in:
Matthew Bauer 2016-11-26 01:05:43 -06:00
parent 6dc15dff24
commit 21b6115ddf
10 changed files with 118 additions and 98 deletions

View File

@ -3,5 +3,5 @@
to run:
```sh
nix-build
./nix-bundle.sh TARGET EXECUTABLE
```

22
arx.nix
View File

@ -1,15 +1,11 @@
{ stdenv, arx }:
{ name, archive, startup}:
{ name
, archive
, startup
}:
stdenv.mkDerivation {
inherit name;
nativeBuildInputs = [ arx ];
buildCommand = ''
${arx}/bin/arx tmpx ${archive} -o $out // ${startup}
chmod +x $out
'';
}
stdenv.mkDerivation {
inherit name;
nativeBuildInputs = [ arx ];
buildCommand = ''
${arx}/bin/arx tmpx ${archive} -o $out // ${startup}
chmod +x $out
'';
}

View File

@ -2,7 +2,7 @@
with nixpkgs;
let
rec {
arx = callPackage ./arx.nix {
inherit (haskellPackages) arx;
@ -22,35 +22,4 @@ let
inherit nix-user-chroot makebootstrap;
};
in {
hello = nix-bootstrap {
name = "hello";
target = hello;
run = "/bin/hello";
};
firefox = nix-bootstrap {
name = "firefox";
target = firefox;
run = "/bin/firefox";
};
nano = nix-bootstrap {
name = "nano";
target = nano;
run = "/bin/nano";
};
emacs = nix-bootstrap {
name = "emacs";
target = emacs;
run = "/bin/emacs";
};
nixInstaller = makebootstrap {
name = "nix-installer.sh";
targets = [ nix-installer ];
startup = ".${nix-installer}/install";
};
}

View File

@ -1,9 +1,9 @@
{ arx, maketar }:
{ name, targets, startup }:
{ name, targets, startup }:
arx {
inherit name startup;
archive = maketar {
inherit name targets;
};
}
arx {
inherit name startup;
archive = maketar {
inherit name targets;
};
}

View File

@ -1,17 +1,29 @@
{ stdenv, perl, pathsFromGraph }:
{ stdenv, gcc, perl, pathsFromGraph }:
{ name, targets }:
stdenv.mkDerivation {
inherit name;
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
nativeBuildInputs = [ perl ];
buildCommand = ''
storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*)
# printRegistration=1 ${perl}/bin/perl ${pathsFromGraph} ./closure-* > .reginfo
tar cvfj $out \
--owner=0 --group=0 --mode=u+rw,uga+r \
--hard-dereference \
$storePaths
'';
}
stdenv.mkDerivation {
inherit name;
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
nativeBuildInputs = [ perl ];
buildCommand = ''
storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*)
# remove "unused" stdenv store paths
# these need to be adjusted and made more intelligent
# this should create a "runtime stdenv"
storePaths=$(echo $storePaths | tr ' ' '\n' | \
grep -Ev '/nix/store/[a-z0-9]+-linux-headers-[0-9.]+' | \
grep -v ${stdenv.cc.libc.dev} | \
grep -v ${stdenv.cc.libc.bin} | \
grep -v ${stdenv.cc.cc} | \
grep -v ${stdenv.cc.cc.lib} | \
tr '\n' ' ')
# grep -Ev '/nix/store/[a-z0-9]+-zlib-[0-9.]+' | \
# printRegistration=1 ${perl}/bin/perl ${pathsFromGraph} ./closure-* > .reginfo
tar cfj $out \
--owner=0 --group=0 --mode=u+rw,uga+r \
--hard-dereference \
$storePaths
'';
}

View File

@ -1,9 +1,8 @@
{ stdenv, writeText, nix-user-chroot, makebootstrap }:
{ name, target, run }:
makebootstrap {
inherit name;
startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}";
targets = [ nix-user-chroot target ];
}
makebootstrap {
inherit name;
startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}";
targets = [ nix-user-chroot target ];
}

29
nix-bundle.sh Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env sh
if [ "$#" -lt 2 ]; then
cat <<EOF
Usage: $0 TARGET EXECUTABLE
Create a single-file bundle from the nixpkgs attribute "TARGET".
EXECUTABLE should be relative to the TARGET's output path.
For example:
$ $0 hello /bin/hello
$ ./hello
Hello, world!
EOF
exit 1
fi
target="$1"
exec="$2"
expr="with import <nixpkgs> {}; with import ./. {}; nix-bootstrap { name = \"$target\"; target = $target; run = \"$exec\"; }"
out=$(nix-store -r $(nix-instantiate -E "$expr"))
cp -f $out $target

View File

@ -1,15 +1,15 @@
{ stdenv, fetchFromGitHub, writeText, nix, cacert }:
stdenv.mkDerivation {
name = "nix-installer";
stdenv.mkDerivation {
name = "nix-installer";
propagatedBuildInputs = [ nix.out cacert ];
propagatedBuildInputs = [ nix.out cacert ];
buildCommand = ''
mkdir -p $out/bin/
substitute ${./install-nix-from-closure.sh} $out/install \
--subst-var-by nix ${nix.out} \
--subst-var-by cacert ${cacert}
chmod +x $out/install
'';
}
buildCommand = ''
mkdir -p $out/bin/
substitute ${./install-nix-from-closure.sh} $out/install \
--subst-var-by nix ${nix.out} \
--subst-var-by cacert ${cacert}
chmod +x $out/install
'';
}

View File

@ -1,3 +1,10 @@
/*
* This file is based on @lethalman's nix-user-chroot. This file has
* diverged from it though.
*
* Usage: nix-user-chroot <nixpath> <command>
*/
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>

View File

@ -1,14 +1,22 @@
{ stdenv, fetchFromGitHub }:
{ stdenv, fetchFromGitHub, patchelf }:
stdenv.mkDerivation {
name = "nix-user-chroot";
phases = [ "buildPhase" "installPhase" "fixupPhase" ];
buildPhase = ''
cp ${./nix-user-chroot.c} nix-user-chroot.c
$CC nix-user-chroot.c -o nix-user-chroot
'';
installPhase = ''
mkdir -p $out/bin/
cp nix-user-chroot $out/bin/nix-user-chroot
'';
}
stdenv.mkDerivation {
name = "nix-user-chroot";
phases = [ "buildPhase" "fixupPhase" "installPhase" ];
buildPhase = ''
cp ${./nix-user-chroot.c} nix-user-chroot.c
$CC nix-user-chroot.c -o nix-user-chroot
'';
# setup local libc interpreter
fixupPhase = ''
patchelf --set-interpreter .$(patchelf --print-interpreter nix-user-chroot) nix-user-chroot
patchelf --set-rpath $(patchelf --print-rpath nix-user-chroot | sed 's|/nix/store/|./nix/store/|g') nix-user-chroot
'';
installPhase = ''
mkdir -p $out/bin/
cp nix-user-chroot $out/bin/nix-user-chroot
'';
}