···8787 exit 1
8888fi
89899090-9191-# Mount some stuff in the target root directory.
9292-mkdir -m 0755 -p $mountPoint/dev $mountPoint/proc $mountPoint/sys $mountPoint/etc $mountPoint/run $mountPoint/home
9393-mkdir -m 01777 -p $mountPoint/tmp
9494-mkdir -m 0755 -p $mountPoint/tmp/root
9595-mkdir -m 0755 -p $mountPoint/var
9696-mkdir -m 0700 -p $mountPoint/root
9797-mount --rbind /dev $mountPoint/dev
9898-mount --rbind /proc $mountPoint/proc
9999-mount --rbind /sys $mountPoint/sys
100100-mount --rbind / $mountPoint/tmp/root
101101-mount -t tmpfs -o "mode=0755" none $mountPoint/run
102102-rm -rf $mountPoint/var/run
103103-ln -s /run $mountPoint/var/run
104104-for f in /etc/resolv.conf /etc/hosts; do rm -f $mountPoint/$f; [ -f "$f" ] && cp -Lf $f $mountPoint/etc/; done
105105-for f in /etc/passwd /etc/group; do touch $mountPoint/$f; [ -f "$f" ] && mount --rbind -o ro $f $mountPoint/$f; done
106106-107107-cp -Lf "@cacert@" "$mountPoint/tmp/ca-cert.crt"
108108-export SSL_CERT_FILE=/tmp/ca-cert.crt
109109-# For Nix 1.7
110110-export CURL_CA_BUNDLE=/tmp/ca-cert.crt
111111-112112-if [ -n "$runChroot" ]; then
113113- if ! [ -L $mountPoint/nix/var/nix/profiles/system ]; then
114114- echo "$0: installation not finished; cannot chroot into installation directory"
115115- exit 1
116116- fi
117117- ln -s /nix/var/nix/profiles/system $mountPoint/run/current-system
118118- exec chroot $mountPoint "${chrootCommand[@]}"
119119-fi
120120-121121-12290# Get the path of the NixOS configuration file.
12391if test -z "$NIXOS_CONFIG"; then
12492 NIXOS_CONFIG=/etc/nixos/configuration.nix
···13098fi
13199132100133133-# Create the necessary Nix directories on the target device, if they
134134-# don't already exist.
135135-mkdir -m 0755 -p \
136136- $mountPoint/nix/var/nix/gcroots \
137137- $mountPoint/nix/var/nix/temproots \
138138- $mountPoint/nix/var/nix/userpool \
139139- $mountPoint/nix/var/nix/profiles \
140140- $mountPoint/nix/var/nix/db \
141141- $mountPoint/nix/var/log/nix/drvs
142142-143143-mkdir -m 1775 -p $mountPoint/nix/store
144144-chown @root_uid@:@nixbld_gid@ $mountPoint/nix/store
145145-146146-147147-# There is no daemon in the chroot.
148148-unset NIX_REMOTE
149149-150150-151151-# We don't have locale-archive in the chroot, so clear $LANG.
152152-export LANG=
153153-export LC_ALL=
154154-export LC_TIME=
155155-156156-157101# Builds will use users that are members of this group
158102extraBuildFlags+=(--option "build-users-group" "$buildUsersGroup")
159103160160-161104# Inherit binary caches from the host
105105+# TODO: will this still work with Nix 1.12 now that it has no perl? Probably not...
162106binary_caches="$(@perl@/bin/perl -I @nix@/lib/perl5/site_perl/*/* -e 'use Nix::Config; Nix::Config::readConfig; print $Nix::Config::config{"binary-caches"};')"
163107extraBuildFlags+=(--option "binary-caches" "$binary_caches")
164108109109+nixpkgs="$(readlink -f "$(nix-instantiate --find-file nixpkgs)")"
110110+export NIX_PATH="nixpkgs=$nixpkgs:nixos-config=$mountPoint/$NIXOS_CONFIG"
111111+unset NIXOS_CONFIG
165112166166-# Copy Nix to the Nix store on the target device, unless it's already there.
167167-if ! NIX_DB_DIR=$mountPoint/nix/var/nix/db nix-store --check-validity @nix@ 2> /dev/null; then
168168- echo "copying Nix to $mountPoint...."
169169- for i in $(@perl@/bin/perl @pathsFromGraph@ @nixClosure@); do
170170- echo " $i"
171171- chattr -R -i $mountPoint/$i 2> /dev/null || true # clear immutable bit
172172- @rsync@/bin/rsync -a $i $mountPoint/nix/store/
173173- done
113113+# TODO: do I need to set NIX_SUBSTITUTERS here or is the --option binary-caches above enough?
174114175175- # Register the paths in the Nix closure as valid. This is necessary
176176- # to prevent them from being deleted the first time we install
177177- # something. (I.e., Nix will see that, e.g., the glibc path is not
178178- # valid, delete it to get it out of the way, but as a result nothing
179179- # will work anymore.)
180180- chroot $mountPoint @nix@/bin/nix-store --register-validity < @nixClosure@
181181-fi
182115183183-184184-# Create the required /bin/sh symlink; otherwise lots of things
185185-# (notably the system() function) won't work.
186186-mkdir -m 0755 -p $mountPoint/bin
187187-# !!! assuming that @shell@ is in the closure
188188-ln -sf @shell@ $mountPoint/bin/sh
189189-116116+# A place to drop temporary closures
117117+trap "rm -rf $tmpdir" EXIT
118118+tmpdir="$(mktemp -d)"
190119191191-# Build hooks likely won't function correctly in the minimal chroot; just disable them.
192192-unset NIX_BUILD_HOOK
193193-194194-# Make the build below copy paths from the CD if possible. Note that
195195-# /tmp/root in the chroot is the root of the CD.
196196-export NIX_OTHER_STORES=/tmp/root/nix:$NIX_OTHER_STORES
197197-198198-p=@nix@/libexec/nix/substituters
199199-export NIX_SUBSTITUTERS=$p/copy-from-other-stores.pl:$p/download-from-binary-cache.pl
120120+# Build a closure (on the host; we then copy it into the guest)
121121+function closure() {
122122+ nix-build "${extraBuildFlags[@]}" --no-out-link -E "with import <nixpkgs> {}; runCommand \"closure\" { exportReferencesGraph = [ \"x\" (buildEnv { name = \"env\"; paths = [ ($1) stdenv ]; }) ]; } \"cp x \$out\""
123123+}
200124125125+system_closure="$tmpdir/system.closure"
201126202127if [ -z "$closure" ]; then
203203- # Get the absolute path to the NixOS/Nixpkgs sources.
204204- nixpkgs="$(readlink -f $(nix-instantiate --find-file nixpkgs))"
205205-206206- nixEnvAction="-f <nixpkgs/nixos> --set -A system"
128128+ expr="(import <nixpkgs/nixos> {}).system"
129129+ system_root="$(nix-build -E "$expr")"
130130+ system_closure="$(closure "$expr")"
207131else
208208- nixpkgs=""
209209- nixEnvAction="--set $closure"
132132+ system_root=$closure
133133+ # Create a temporary file ending in .closure (so nixos-prepare-root knows to --import it) to transport the store closure
134134+ # to the filesytem we're preparing. Also delete it on exit!
135135+ nix-store --export $(nix-store -qR $closure) > $system_closure
210136fi
211137212212-# Build the specified Nix expression in the target store and install
213213-# it into the system configuration profile.
214214-echo "building the system configuration..."
215215-NIX_PATH="nixpkgs=/tmp/root/$nixpkgs:nixos-config=$NIXOS_CONFIG" NIXOS_CONFIG= \
216216- chroot $mountPoint @nix@/bin/nix-env \
217217- "${extraBuildFlags[@]}" -p /nix/var/nix/profiles/system $nixEnvAction
218218-219219-220220-# Copy the NixOS/Nixpkgs sources to the target as the initial contents
221221-# of the NixOS channel.
222222-mkdir -m 0755 -p $mountPoint/nix/var/nix/profiles
223223-mkdir -m 1777 -p $mountPoint/nix/var/nix/profiles/per-user
224224-mkdir -m 0755 -p $mountPoint/nix/var/nix/profiles/per-user/root
225225-srcs=$(nix-env "${extraBuildFlags[@]}" -p /nix/var/nix/profiles/per-user/root/channels -q nixos --no-name --out-path 2>/dev/null || echo -n "")
226226-if [ -z "$noChannelCopy" ] && [ -n "$srcs" ]; then
227227- echo "copying NixOS/Nixpkgs sources..."
228228- chroot $mountPoint @nix@/bin/nix-env \
229229- "${extraBuildFlags[@]}" -p /nix/var/nix/profiles/per-user/root/channels -i "$srcs" --quiet
230230-fi
231231-mkdir -m 0700 -p $mountPoint/root/.nix-defexpr
232232-ln -sfn /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels
138138+channel_root="$(nix-env -p /nix/var/nix/profiles/per-user/root/channels -q nixos --no-name --out-path 2>/dev/null || echo -n "")"
139139+channel_closure="$tmpdir/channel.closure"
140140+nix-store --export $channel_root > $channel_closure
233141142142+# Populate the target root directory with the basics
143143+@prepare_root@/bin/nixos-prepare-root $mountPoint $channel_root $system_root @nixClosure@ $system_closure $channel_closure
234144235235-# Get rid of the /etc bind mounts.
236236-for f in /etc/passwd /etc/group; do [ -f "$f" ] && umount $mountPoint/$f; done
145145+# nixos-prepare-root doesn't currently do anything with file ownership, so we set it up here instead
146146+chown @root_uid@:@nixbld_gid@ $mountPoint/nix/store
237147148148+mount --rbind /dev $mountPoint/dev
149149+mount --rbind /proc $mountPoint/proc
150150+mount --rbind /sys $mountPoint/sys
238151239152# Grub needs an mtab.
240153ln -sfn /proc/mounts $mountPoint/etc/mtab
241241-242242-243243-# Mark the target as a NixOS installation, otherwise
244244-# switch-to-configuration will chicken out.
245245-touch $mountPoint/etc/NIXOS
246246-247154248155# Switch to the new system configuration. This will install Grub with
249156# a menu default pointing at the kernel/initrd/etc of the new
···11+#! @shell@
22+33+# This script's goal is to perform all "static" setup of a filesystem structure from pre-built store paths. Everything
44+# in here should run in a non-root context and inside a Nix builder. It's designed primarily to be called from image-
55+# building scripts and from nixos-install, but because it makes very few assumptions about the context in which it runs,
66+# it could be useful in other contexts as well.
77+#
88+# Current behavior:
99+# - set up basic filesystem structure
1010+# - make Nix store etc.
1111+# - copy Nix, system, channel, and misceallaneous closures to target Nix store
1212+# - register validity of all paths in the target store
1313+# - set up channel and system profiles
1414+1515+# Ensure a consistent umask.
1616+umask 0022
1717+1818+set -e
1919+2020+mountPoint="$1"
2121+channel="$2"
2222+system="$3"
2323+shift 3
2424+closures="$@"
2525+2626+PATH="@coreutils@/bin:@nix@/bin:@perl@/bin:@utillinux@/bin:@rsync@/bin"
2727+2828+if ! test -e "$mountPoint"; then
2929+ echo "mount point $mountPoint doesn't exist"
3030+ exit 1
3131+fi
3232+3333+# Create a few of the standard directories in the target root directory.
3434+mkdir -m 0755 -p $mountPoint/dev $mountPoint/proc $mountPoint/sys $mountPoint/etc $mountPoint/run $mountPoint/home
3535+mkdir -m 01777 -p $mountPoint/tmp
3636+mkdir -m 0755 -p $mountPoint/tmp/root
3737+mkdir -m 0755 -p $mountPoint/var
3838+mkdir -m 0700 -p $mountPoint/root
3939+4040+ln -s /run $mountPoint/var/run
4141+4242+# Create the necessary Nix directories on the target device
4343+mkdir -m 0755 -p \
4444+ $mountPoint/nix/var/nix/gcroots \
4545+ $mountPoint/nix/var/nix/temproots \
4646+ $mountPoint/nix/var/nix/userpool \
4747+ $mountPoint/nix/var/nix/profiles \
4848+ $mountPoint/nix/var/nix/db \
4949+ $mountPoint/nix/var/log/nix/drvs
5050+5151+mkdir -m 1775 -p $mountPoint/nix/store
5252+5353+# All Nix operations below should operate on our target store, not /nix/store.
5454+# N.B: this relies on Nix 1.12 or higher
5555+export NIX_REMOTE=local?root=$mountPoint
5656+5757+# Copy our closures to the Nix store on the target mount point, unless they're already there.
5858+for i in $closures; do
5959+ # We support closures both in the format produced by `nix-store --export` and by `exportReferencesGraph`,
6060+ # mostly because there doesn't seem to be a single format that can be produced outside of a nix build and
6161+ # inside one. See https://github.com/NixOS/nix/issues/1242 for more discussion.
6262+ if [[ "$i" =~ \.closure$ ]]; then
6363+ echo "importing serialized closure $i to $mountPoint..."
6464+ nix-store --import < $i
6565+ else
6666+ # There has to be a better way to do this, right?
6767+ echo "copying closure $i to $mountPoint..."
6868+ for j in $(perl @pathsFromGraph@ $i); do
6969+ echo " $j... "
7070+ rsync -a $j $mountPoint/nix/store/
7171+ done
7272+7373+ nix-store --register-validity < $i
7474+ fi
7575+done
7676+7777+# Create the required /bin/sh symlink; otherwise lots of things
7878+# (notably the system() function) won't work.
7979+if [ ! -x $mountPoint/@shell@ ]; then
8080+ echo "Error: @shell@ wasn't included in the closure" >&2
8181+ exit 1
8282+fi
8383+mkdir -m 0755 -p $mountPoint/bin
8484+ln -sf @shell@ $mountPoint/bin/sh
8585+8686+echo "setting the system closure to '$system'..."
8787+nix-env "${extraBuildFlags[@]}" -p $mountPoint/nix/var/nix/profiles/system --set "$system"
8888+8989+ln -sfn /nix/var/nix/profiles/system $mountPoint/run/current-system
9090+9191+# Copy the NixOS/Nixpkgs sources to the target as the initial contents of the NixOS channel.
9292+mkdir -m 0755 -p $mountPoint/nix/var/nix/profiles
9393+mkdir -m 1777 -p $mountPoint/nix/var/nix/profiles/per-user
9494+mkdir -m 0755 -p $mountPoint/nix/var/nix/profiles/per-user/root
9595+9696+if [ -z "$noChannelCopy" ] && [ -n "$channel" ]; then
9797+ echo "copying channel..."
9898+ nix-env --option build-use-substitutes false "${extraBuildFlags[@]}" -p $mountPoint/nix/var/nix/profiles/per-user/root/channels --set "$channel" --quiet
9999+fi
100100+mkdir -m 0700 -p $mountPoint/root/.nix-defexpr
101101+ln -sfn /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels
102102+103103+# Mark the target as a NixOS installation, otherwise switch-to-configuration will chicken out.
104104+touch $mountPoint/etc/NIXOS
105105+
···3434 boot.loader.systemd-boot.enable = true;
3535 ''}
36363737+ users.extraUsers.alice = {
3838+ isNormalUser = true;
3939+ home = "/home/alice";
4040+ description = "Alice Foobar";
4141+ };
4242+3743 hardware.enableAllFirmware = lib.mkForce false;
38443945 ${replaceChars ["\n"] ["\n "] extraConfig}
···96102 $machine->shutdown;
9710398104 # Now see if we can boot the installation.
9999- $machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}" });
105105+ $machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}", name => "boot-after-install" });
100106101107 # For example to enter LUKS passphrase.
102108 ${preBootCommands}
···118124 $machine->waitForUnit("swap.target");
119125 $machine->succeed("cat /proc/swaps | grep -q /dev");
120126127127+ # Check that the store is in good shape
128128+ $machine->succeed("nix-store --verify --check-contents >&2");
129129+121130 # Check whether the channel works.
122131 $machine->succeed("nix-env -iA nixos.procps >&2");
123132 $machine->succeed("type -tP ps | tee /dev/stderr") =~ /.nix-profile/
124133 or die "nix-env failed";
134134+135135+ # Check that the daemon works, and that non-root users can run builds (this will build a new profile generation through the daemon)
136136+ $machine->succeed("su alice -l -c 'nix-env -iA nixos.procps' >&2");
125137126138 # We need to a writable nix-store on next boot.
127139 $machine->copyFileFromHost(
···139151 $machine->shutdown;
140152141153 # Check whether a writable store build works
142142- $machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}" });
154154+ $machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}", name => "rebuild-switch" });
143155 ${preBootCommands}
144156 $machine->waitForUnit("multi-user.target");
145157 $machine->copyFileFromHost(
···150162151163 # And just to be sure, check that the machine still boots after
152164 # "nixos-rebuild switch".
153153- $machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}" });
165165+ $machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}", "boot-after-rebuild-switch" });
154166 ${preBootCommands}
155167 $machine->waitForUnit("network.target");
156168 $machine->shutdown;
+3-2
pkgs/tools/package-management/nix/default.nix
···131131 sha256 = "69e0f398affec2a14c47b46fec712906429c85312d5483be43e4c34da4f63f67";
132132 };
133133134134- # 1.11.8 doesn't yet have the patch to work on LLVM 4, so we patch it for now. Take this out once
135135- # we move to a higher version. I'd pull the specific patch from upstream but it doesn't apply cleanly.
134134+ # Until 1.11.9 is released, we do this :)
136135 patchPhase = ''
137136 substituteInPlace src/libexpr/json-to-value.cc \
138137 --replace 'std::less<Symbol>, gc_allocator<Value *>' \
139138 'std::less<Symbol>, gc_allocator<std::pair<const Symbol, Value *> >'
139139+140140+ sed -i '/if (settings.readOnlyMode) {/a curSchema = getSchema();' src/libstore/local-store.cc
140141 '';
141142 }) // { perl-bindings = nixStable; };
142143