You can run the kvm nixos installation test by: nix-build --no-out-link tests/test-nixos-install-from-cd.nix It boots the installed system. It still fails sshd isn't started (yet) adding nixos-bootstrapping-archive: You can install NixOS easily using any live cd now. See README-BOOTSTRAP-NIXOS svn path=/nixos/trunk/; revision=18950wip/yesman
parent
fe490f2edc
commit
401fd84ae1
@ -0,0 +1,134 @@ |
||||
#! @shell@ |
||||
# runs in chroot |
||||
# creates mandatory directories such as /var |
||||
# finally runs switch-to-configuration optionally registering grub |
||||
set -e |
||||
|
||||
usage(){ |
||||
cat << EOF |
||||
script --install [--fast] [--no-grub] |
||||
|
||||
--no-pull: don't do a nix-pull to get the latest Nixpkgs |
||||
channel manifest |
||||
|
||||
--no-grub: don't install grub ( you don't want this ..) |
||||
|
||||
--debug: set -x |
||||
|
||||
-j n : will be passed to nix-env = number of build tasks done simultaniously |
||||
|
||||
Description: |
||||
This scripts installs NixOS and should be run within the target chroot. |
||||
|
||||
The minimal nix system must have been installed previously. |
||||
Eg nixos-prepare-install.sh does this for you. |
||||
|
||||
Probably this script is distributed along with the minimal nix closure used |
||||
for bootstrapping |
||||
EOF |
||||
exit 1 |
||||
} |
||||
|
||||
die(){ echo "!>> " $@; exit 1; } |
||||
|
||||
check(){ |
||||
[ -e "$1" ] || die "$1 not found $2" |
||||
} |
||||
WARN(){ echo "WARNING: $@"; } |
||||
INFO(){ echo "INFO: " $@; } |
||||
|
||||
# == configuration == |
||||
|
||||
HOME=${HOME:-/root} |
||||
NIXOS=${NIXOS:-/etc/nixos/nixos} |
||||
NIXPKGS=${NIXPKGS:-/etc/nixos/nixpkgs} |
||||
NIXOS_CONFIG=${NIXOS_CONFIG:-/etc/nixos/configuration.nix} |
||||
NIXOS_PULL=${NIXOS_PULL:-1} |
||||
NIXOS_INSTALL_GRUB=${NIXOS_INSTALL_GRUB:-1} |
||||
|
||||
ps="with-prepared-chroot.sh" |
||||
|
||||
check "$NIXOS_CONFIG" |
||||
check "$NIXOS/modules" "nixos repo not found" |
||||
check "$NIXPKGS/pkgs/top-level/all-packages.nix" "nixpgks repo not found" |
||||
for d in /dev /sys /proc; do |
||||
check "$d" "It should have been mounted by $ps" |
||||
done |
||||
|
||||
# try reusing binaries from host system (which is most likely an installation CD) |
||||
if [ -d /host-system/nix ]; then |
||||
export NIX_OTHER_STORES=/host-system/nix${NIX_OTHER_STORES:+:}$NIX_OTHER_STORES |
||||
else |
||||
WARN "/host-system/nix not found. It should have been --bind mounted by $ps. |
||||
I won't be able to take binaries from the installation media. |
||||
" |
||||
fi |
||||
|
||||
# == configuration end == |
||||
|
||||
INSTALL= |
||||
|
||||
for arg in $@; do |
||||
case $arg in |
||||
--no-pull) NIXOS_PULL=0;; |
||||
--install) INSTALL=1;; |
||||
--no-grub) NIXOS_INSTALL_GRUB=;; |
||||
--debug) set -x;; |
||||
-j*) NIX_ENV_ARGS="$NIX_ENV_ARGS $arg";; |
||||
*) usage; |
||||
esac |
||||
done |
||||
|
||||
if [ "$INSTALL" != 1 ]; then |
||||
usage |
||||
fi |
||||
|
||||
|
||||
# We don't have locale-archive in the chroot, so clear $LANG. |
||||
export LANG= |
||||
|
||||
export PATH=@coreutils@/bin |
||||
|
||||
mkdir -m 01777 -p /tmp |
||||
mkdir -m 0755 -p /var |
||||
|
||||
# Create the necessary Nix directories on the target device, if they |
||||
# don't already exist. |
||||
mkdir -m 0755 -p \ |
||||
/nix/var/nix/gcroots \ |
||||
/nix/var/nix/temproots \ |
||||
/nix/var/nix/manifests \ |
||||
/nix/var/nix/userpool \ |
||||
/nix/var/nix/profiles \ |
||||
/nix/var/nix/db \ |
||||
/nix/var/log/nix/drvs |
||||
|
||||
# Do a nix-pull to speed up building. |
||||
if test -n "@nixpkgsURL@" -a ${NIXOS_PULL} != 0; then |
||||
@nix@/bin/nix-pull @nixpkgsURL@/MANIFEST || true |
||||
fi |
||||
|
||||
# Build the specified Nix expression in the target store and install |
||||
# it into the system configuration profile. |
||||
INFO "building the system configuration..." |
||||
@nix@/bin/nix-env \ |
||||
-p /nix/var/nix/profiles/system \ |
||||
-f "$NIXOS" \ |
||||
--arg configuration "import $NIXOS_CONFIG" \ |
||||
--set -A system \ |
||||
$NIX_ENV_ARGS |
||||
|
||||
# Mark the target as a NixOS installation, otherwise |
||||
# switch-to-configuration will chicken out. |
||||
touch /etc/NIXOS |
||||
|
||||
# Grub needs an mtab. |
||||
ln -sfn /proc/mounts /etc/mtab |
||||
|
||||
|
||||
# Switch to the new system configuration. This will install Grub with |
||||
# a menu default pointing at the kernel/initrd/etc of the new |
||||
# configuration. |
||||
INFO "finalising the installation..." |
||||
export NIXOS_INSTALL_GRUB |
||||
/nix/var/nix/profiles/system/bin/switch-to-configuration boot |
@ -0,0 +1,227 @@ |
||||
#!/bin/sh |
||||
|
||||
# prepare installation by putting in place unless present: |
||||
# /etc/nixos/nixpkgs |
||||
# /etc/nixos/nixos |
||||
# /etc/nixos/configuration.nix |
||||
|
||||
|
||||
set -e |
||||
|
||||
usage(){ |
||||
cat << EOF |
||||
script options [list of actions] |
||||
|
||||
actions: |
||||
|
||||
guess-config: run nixos-hardware-scan > T/configuration.nix |
||||
copy-nix: copy minimal nix system which can bootstrap the whole system |
||||
This creates the $mountPoint/bin/sh symlink |
||||
If you're bootstrapping from an archive store paths are registered valid only |
||||
copy-nixos-bootstrap: |
||||
copy the nixos-bootstrap script to /nix/store where it will be |
||||
garbage collected later |
||||
copy-sources: copy repos into T/ |
||||
checkout-sources: svn checkout official repos into T/ |
||||
|
||||
|
||||
where T=$T |
||||
and repos = $ALL_REPOS |
||||
|
||||
options: |
||||
--force: If targets exist no action will taken unless you use --force |
||||
in which case target is renamed before action is run |
||||
--dir-ok: allow installing into directory (omits is mount point check) |
||||
|
||||
--debug: set -x |
||||
|
||||
|
||||
default list of actions: $DEFAULTS |
||||
EOF |
||||
exit 1 |
||||
} |
||||
|
||||
|
||||
INFO(){ echo "INFO: " $@; } |
||||
|
||||
# = configuration = |
||||
|
||||
FORCE_ACTIONS=${FORCE_ACTIONS:-} |
||||
ALL_REPOS="nixpkgs nixos" |
||||
mountPoint=${mountPoint:-/mnt} |
||||
|
||||
if [ -e $mountPoint/README-BOOTSTRAP-NIXOS ]; then |
||||
INFO "$mountPoint/README-BOOTSTRAP-NIXOS found, assuming your're bootstrapping from an archive. Nix files should be in place" |
||||
FROM_ARCHIVE=1 |
||||
DEFAULTS="guess-config copy-nix" |
||||
else |
||||
FROM_ARCHIVE=0 |
||||
DEFAULTS="guess-config copy-nixos-bootstrap copy-nix copy-sources" |
||||
fi |
||||
|
||||
backupTimestamp=$(date "+%Y%m%d%H%M%S") |
||||
SRC_BASE=${SRC_BASE:-"/etc/nixos"} |
||||
SVN_BASE="https://svn.nixos.org/repos/nix" |
||||
MUST_BE_MOUNTPOINT=${MUST_BE_MOUNTPOINT:-1} |
||||
T="$mountPoint/etc/nixos" |
||||
|
||||
NIX_CLOSURE=${NIX_CLOSURE:-@nixClosure@} |
||||
|
||||
die(){ echo "!>> " $@; exit 1; } |
||||
|
||||
## = read options = |
||||
# actions is used by main loop at the end |
||||
ACTIONS="" |
||||
# check and handle options: |
||||
for a in $@; do |
||||
case "$a" in |
||||
copy-nix|copy-nixos-bootstrap|guess-config|copy-sources|checkout-sources) |
||||
ACTIONS="$ACTIONS $a" |
||||
;; |
||||
--dir-ok) |
||||
MUST_BE_MOUNTPOINT=false |
||||
;; |
||||
--force) |
||||
FORCE_ACTIONS=1 |
||||
;; |
||||
--debug) |
||||
set -x |
||||
;; |
||||
*) |
||||
echo "unkown option: $a" |
||||
usage |
||||
;; |
||||
esac |
||||
done |
||||
[ -n "$ACTIONS" ] || ACTIONS="$DEFAULTS" |
||||
|
||||
|
||||
if ! grep -F -q " $mountPoint " /proc/mounts && [ "$MUST_BE_MOUNTPOINT" = 1 ]; then |
||||
die "$mountPoint doesn't appear to be a mount point" |
||||
fi |
||||
|
||||
# Get the store paths to copy from the references graph. |
||||
storePaths=$(@perl@/bin/perl @pathsFromGraph@ @nixClosure@) |
||||
# = utils = |
||||
|
||||
backup(){ |
||||
local dst="$(dirname "$1")/$(basename "$1")-$backupTimestamp" |
||||
INFO "backing up: $1 -> $dst" |
||||
mv "$1" "$dst" |
||||
} |
||||
|
||||
# = implementation = |
||||
|
||||
# exit status = 0: target exists |
||||
# exti status != 0: target must be rebuild either because --force was given or |
||||
# because it didn't exist yet |
||||
target_realised(){ |
||||
if [ -e "$1" ] && [ "$FORCE_ACTIONS" = 1 ]; then |
||||
backup "$1" |
||||
fi |
||||
|
||||
[ -e "$1" ] && { |
||||
INFO "not realsing $1. Target already exists. Use --force to force." |
||||
} |
||||
} |
||||
|
||||
createDirs(){ |
||||
mkdir -m 0755 -p $mountPoint/etc/nixos |
||||
mkdir -m 1777 -p $mountPoint/nix/store |
||||
} |
||||
|
||||
|
||||
realise_repo(){ |
||||
local action=$1 |
||||
local repo=$2 |
||||
|
||||
createDirs |
||||
|
||||
if [ "$action" == "copy-sources" ]; then |
||||
|
||||
local repo_sources="${repo}_SOURCES" |
||||
rsync -a -r "${SRC_BASE}/$repo" "$T" |
||||
|
||||
else |
||||
|
||||
INFO "checking out $repo" |
||||
svn co "$SVN_BASE/$repo/trunk" "$T/$repo" |
||||
|
||||
fi |
||||
|
||||
} |
||||
|
||||
# = run actions: = |
||||
for a in $ACTIONS; do |
||||
case "$a" in |
||||
|
||||
guess_config) |
||||
local config="$T/configuration.nix" |
||||
target_realised "$config" || { |
||||
INFO "creating simple configuration file" |
||||
nixos-hardware-scan > "$config" |
||||
echo |
||||
INFO "Note: you can start customizing $config while remaining actions will are being executed" |
||||
echo |
||||
} |
||||
;; |
||||
|
||||
copy-nixos-bootstrap) |
||||
createDirs |
||||
# this script will be garbage collected somewhen: |
||||
cp @nixosBootstrap@/bin/nixos-bootstrap $mountPoint/nix/store/ |
||||
;; |
||||
|
||||
copy-nix) |
||||
if [ -n "$FROM_ARCHIVE" ]; then |
||||
NIX_CLOSURE=${mountPoint}@nixClosure@ |
||||
else |
||||
INFO "Copy Nix to the Nix store on the target device." |
||||
createDirs |
||||
echo "copying Nix to $mountPoint...." |
||||
for i in $storePaths; do |
||||
echo " $i" |
||||
rsync -a $i $mountPoint/nix/store/ |
||||
done |
||||
|
||||
fi |
||||
|
||||
[ -e "$NIX_CLOSURE" ] || die "Couldn't find nixClosure $NIX_CLOSURE anywhere. Can't register inital store paths valid. Exiting" |
||||
|
||||
# Create the required /bin/sh symlink; otherwise lots of things |
||||
# (notably the system() function) won't work. |
||||
mkdir -m 0755 -p $mountPoint/bin |
||||
ln -sf @shell@ $mountPoint/bin/sh |
||||
|
||||
INFO "registering bootstrapping store paths as valid so that they won't be rebuild" |
||||
# Register the paths in the Nix closure as valid. This is necessary |
||||
# to prevent them from being deleted the first time we install |
||||
# something. (I.e., Nix will see that, e.g., the glibc path is not |
||||
# valid, delete it to get it out of the way, but as a result nothing |
||||
# will work anymore.) |
||||
# TODO: check permissions so that paths can't be changed later? |
||||
chroot "$mountPoint" @nix@/bin/nix-store --register-validity < $NIX_CLOSURE |
||||
|
||||
;; |
||||
|
||||
copy-sources|checkout-sources) |
||||
|
||||
for repo in $ALL_REPOS; do |
||||
target_realised "$T/$repo" || realise_repo $a $repo |
||||
done |
||||
|
||||
;; |
||||
esac |
||||
done |
||||
|
||||
if [ -e "$T/nixos" ] && [ -e "$T/nixpkgs" ] && [ -e "$T/configuration.nix" ]; then |
||||
cat << EOF |
||||
To realise your NixOS installtion execute: |
||||
|
||||
run-in-chroot "/nix/store/nixos-bootstrap --install" |
||||
EOF |
||||
else |
||||
for t in "$T/configuration.nix" "$T/nixpkgs" "$T/configuration.nix"; do |
||||
INFO "you can't start because $t is missing" |
||||
done |
||||
fi |
@ -0,0 +1,93 @@ |
||||
#!/bin/sh |
||||
# helper script preparing chroot by mounting /dev /proc /sys etc |
||||
|
||||
set -e |
||||
|
||||
usage(){ |
||||
cat << EOF |
||||
$SCRIPT OPTS or cmd |
||||
OPTS: |
||||
--prepare : only mount |
||||
--unprepare : only unmount |
||||
--debug : set -x |
||||
|
||||
requisites: perl |
||||
|
||||
$SCRIPT --prepare: prepare chroot only |
||||
$SCRIPT --unprepare: unprepare chroot only |
||||
$SCRIPT command: run /bin/sh command in chroot |
||||
|
||||
usage example run a shell in chroot: |
||||
$SCRIPT path-to-sh |
||||
|
||||
EOF |
||||
exit 1 |
||||
} |
||||
|
||||
die(){ echo "!>> " $@; exit 1; } |
||||
INFO(){ echo "INFO: " $@; } |
||||
|
||||
# this is no longer needed because umount -l unmounts rescursive --rbound mounts as well |
||||
#umountUnder() { |
||||
# local dir="$1" |
||||
# for i in $(grep -F " $dir" /proc/mounts \ |
||||
# | perl -e 'while (<>) { /^\S+\s+(\S+)\s+/; print "$1\n"; }' \ |
||||
# | sort -r); |
||||
# do |
||||
# echo umount $i || true |
||||
# done |
||||
#} |
||||
|
||||
|
||||
prepare(){ |
||||
INFO "Enable networking: copying /etc/resolv.conf" |
||||
mkdir -m 0755 -p $mountPoint/etc |
||||
touch /etc/resolv.conf |
||||
cp /etc/resolv.conf $mountPoint/etc/ |
||||
|
||||
INFO "mounting /proc /sys /dev and / to /host-system" |
||||
mkdir -m 0755 -p $mountPoint/{dev,proc,sys,host-system} |
||||
mount --bind /dev $mountPoint/dev |
||||
mount --bind /proc $mountPoint/proc |
||||
mount --bind /sys $mountPoint/sys |
||||
mount --rbind / $mountPoint/host-system |
||||
} |
||||
|
||||
unprepare(){ |
||||
INFO "unmounting /proc /sys /dev and removing /host-system if empty" |
||||
for d in $mountPoint/{host-system,dev,proc,sys}; do |
||||
umount -l "$d" |
||||
done |
||||
# no -fr !! |
||||
rmdir $mountPoint/host-system |
||||
} |
||||
|
||||
run_cmd(){ |
||||
prepare |
||||
trap "unprepare" EXIT |
||||
chroot $mountPoint $@ |
||||
} |
||||
|
||||
|
||||
SCRIPT="$(basename "$0")" |
||||
mountPoint=${mountPoint:-/mnt} |
||||
|
||||
[ -d $mountPoint ] || die "$mountPoint is not a directory" |
||||
|
||||
while [ "$#" > 0 ]; do |
||||
case $1 in |
||||
|
||||
--debug) shift; set -x;; |
||||
|
||||
-h|--help) shift; usage;; |
||||
|
||||
--prepare) shift; prepare;; |
||||
|
||||
--unprepare) shift; unprepare;; |
||||
|
||||
*) |
||||
run_cmd "$@" |
||||
exit 0 |
||||
;; |
||||
esac |
||||
done |
@ -0,0 +1,16 @@ |
||||
HOWTO install NixOS from a bootstrap archive |
||||
============================================= |
||||
|
||||
export mountPoint=$target # defaults to /mnt |
||||
tar xfj $ARCHIV -C $mountPoint |
||||
|
||||
bash $mountPoint/nix/store/nixos-prepare-install # see --help |
||||
|
||||
$EDITOR $mountPoint/etc/nixos/configuration.nix |
||||
|
||||
# you can replace --install by --help to get more info |
||||
bash $mountPoint/nix/store/run-in-chroot "/nix/store/nixos-bootstrap --install" |
||||
|
||||
|
||||
This archive is create by |
||||
nix-build $NIXOS -A config.system.build.minimalInstaller |
@ -0,0 +1,20 @@ |
||||
args: with args; |
||||
|
||||
# TODO add revision number or such |
||||
runCommand "nixos-bootstrap-archive" { } '' |
||||
PATH=${perl}/bin:${coreutils}/bin:${gnutar}/bin:${bzip2}/bin |
||||
storePaths=$(perl ${pathsFromGraph} ${nixClosure}) |
||||
|
||||
s(){ echo -C $(dirname $1) $(basename $1); } |
||||
|
||||
mkdir $out |
||||
tar cf tmp.tar ${nixClosure} $storePaths |
||||
cp ${./README-BOOTSTRAP-NIXOS} README-BOOTSTRAP-NIXOS |
||||
tar --append -f tmp.tar README-BOOTSTRAP-NIXOS |
||||
tar --append -f tmp.tar --transform 's@^@/nix/store/@' \ |
||||
$(s ${nixosPrepareInstall}/bin/nixos-prepare-install ) \ |
||||
$(s ${runInChroot}/bin/run-in-chroot ) \ |
||||
$(s ${nixosBootstrap}/bin/nixos-bootstrap ) |
||||
|
||||
cat tmp.tar | bzip2 > $out/nixos-install-archive.tar.bz2 |
||||
'' |
@ -0,0 +1,71 @@ |
||||
# A test script is created testing the installer2 scripts |
||||
# This can't be a build job because chroot is required - sudo is used |
||||
# |
||||
# # build test script: |
||||
# nix-build tests/test-nixos-installer2.nix -A test |
||||
# |
||||
# # run: |
||||
# sudo result/bin/test |
||||
|
||||
{ nixos ? ./.. |
||||
, nixpkgs ? ../../nixpkgs |
||||
, services ? ../../nixos/services |
||||
, system ? builtins.currentSystem |
||||
, configPath ? ./test-nixos-install-from-cd.nix |
||||
}: |
||||
|
||||
let |
||||
|
||||
isos = (import ../release.nix) { inherit nixpkgs; }; |
||||
|
||||
# using same configuration as ased by kvm test. |
||||
configuration = ../modules/installer/cd-dvd/test-nixos-install-from-cd-config.nix; |
||||
|
||||
eval = import ../lib/eval-config.nix { |
||||
inherit system nixpkgs; |
||||
modules = [ configuration ]; |
||||
}; |
||||
|
||||
|
||||
inherit (eval) pkgs config; |
||||
|
||||
inherit (pkgs) qemu_kvm; |
||||
|
||||
# prebuild system which will be installed for two reasons: |
||||
# build derivations are in store and can be reused |
||||
# the iso is only build when this suceeds (?) |
||||
systemDerivation = builtins.addErrorContext "while building system" config.system.build.toplevel; |
||||
|
||||
|
||||
tools = config.system.build; |
||||
|
||||
in |
||||
|
||||
rec { |
||||
|
||||
test = pkgs.writeScriptBin "test" '' |
||||
#!/bin/sh -e |
||||
# DEBUG can be set to --debug to force set -x in scripts |
||||
|
||||
export mountPoint=''${mountPoint:-`pwd`/mountPoint} |
||||
if [ -e "$mountPoint" ]; then |
||||
echo "mountPoint $mountPoint exists, delete? [y] | other key: continue" |
||||
read -n1 delete |
||||
if [ "$delete" == "y" ]; then |
||||
rm -fr $mountPoint |
||||
fi |
||||
fi |
||||
mkdir -p $mountPoint |
||||
|
||||
set -x |
||||
|
||||
${tools.nixosPrepareInstall}/bin/nixos-prepare-install $DEBUG --dir-ok copy-nixos-bootstrap copy-nix copy-sources |
||||
|
||||
cp ${configuration} $mountPoint/etc/nixos/configuration.nix |
||||
|
||||
# at least one of those files is referenced. So copy all - they don't hurt |
||||
cp -r ${builtins.dirOf (builtins.toString configuration)}/* $mountPoint/etc/nixos |
||||
|
||||
${tools.runInChroot}/bin/run-in-chroot $DEBUG "/nix/store/nixos-bootstrap $DEBUG --install --no-grub" |
||||
''; |
||||
} |
Loading…
Reference in new issue