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: to run:
```sh ```sh
nix-build ./nix-bundle.sh TARGET EXECUTABLE
``` ```

22
arx.nix
View File

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

View File

@ -2,7 +2,7 @@
with nixpkgs; with nixpkgs;
let rec {
arx = callPackage ./arx.nix { arx = callPackage ./arx.nix {
inherit (haskellPackages) arx; inherit (haskellPackages) arx;
@ -22,35 +22,4 @@ let
inherit nix-user-chroot makebootstrap; 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 }: { arx, maketar }:
{ name, targets, startup }:
{ name, targets, startup }: arx {
arx { inherit name startup;
inherit name startup; archive = maketar {
archive = maketar { inherit name targets;
inherit name targets; };
}; }
}

View File

@ -1,17 +1,29 @@
{ stdenv, perl, pathsFromGraph }: { stdenv, gcc, perl, pathsFromGraph }:
{ name, targets }: { name, targets }:
stdenv.mkDerivation { stdenv.mkDerivation {
inherit name; inherit name;
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets; exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
nativeBuildInputs = [ perl ]; nativeBuildInputs = [ perl ];
buildCommand = '' buildCommand = ''
storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*) storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*)
# printRegistration=1 ${perl}/bin/perl ${pathsFromGraph} ./closure-* > .reginfo
tar cvfj $out \ # remove "unused" stdenv store paths
--owner=0 --group=0 --mode=u+rw,uga+r \ # these need to be adjusted and made more intelligent
--hard-dereference \ # this should create a "runtime stdenv"
$storePaths 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 }: { stdenv, writeText, nix-user-chroot, makebootstrap }:
{ name, target, run }: { name, target, run }:
makebootstrap { makebootstrap {
inherit name; inherit name;
startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}"; startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}";
targets = [ nix-user-chroot target ]; 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, fetchFromGitHub, writeText, nix, cacert }:
stdenv.mkDerivation { stdenv.mkDerivation {
name = "nix-installer"; name = "nix-installer";
propagatedBuildInputs = [ nix.out cacert ]; propagatedBuildInputs = [ nix.out cacert ];
buildCommand = '' buildCommand = ''
mkdir -p $out/bin/ mkdir -p $out/bin/
substitute ${./install-nix-from-closure.sh} $out/install \ substitute ${./install-nix-from-closure.sh} $out/install \
--subst-var-by nix ${nix.out} \ --subst-var-by nix ${nix.out} \
--subst-var-by cacert ${cacert} --subst-var-by cacert ${cacert}
chmod +x $out/install 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 #define _GNU_SOURCE
#include <sched.h> #include <sched.h>
#include <unistd.h> #include <unistd.h>

View File

@ -1,14 +1,22 @@
{ stdenv, fetchFromGitHub }: { stdenv, fetchFromGitHub, patchelf }:
stdenv.mkDerivation { stdenv.mkDerivation {
name = "nix-user-chroot"; name = "nix-user-chroot";
phases = [ "buildPhase" "installPhase" "fixupPhase" ]; phases = [ "buildPhase" "fixupPhase" "installPhase" ];
buildPhase = ''
cp ${./nix-user-chroot.c} nix-user-chroot.c buildPhase = ''
$CC nix-user-chroot.c -o nix-user-chroot 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 # 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
'';
}