1#! @shell@
2
3set -e
4shopt -s nullglob
5
6export PATH=@path@:$PATH
7
8# Ensure a consistent umask.
9umask 0022
10
11# Parse the command line for the -I flag
12extraBuildFlags=()
13
14mountPoint=/mnt
15channelPath=
16
17while [ "$#" -gt 0 ]; do
18 i="$1"; shift 1
19 case "$i" in
20 --max-jobs|-j|--cores|-I|--substituters)
21 j="$1"; shift 1
22 extraBuildFlags+=("$i" "$j")
23 ;;
24 --option)
25 j="$1"; shift 1
26 k="$1"; shift 1
27 extraBuildFlags+=("$i" "$j" "$k")
28 ;;
29 --root)
30 mountPoint="$1"; shift 1
31 ;;
32 --system|--closure)
33 system="$1"; shift 1
34 ;;
35 --channel)
36 channelPath="$1"; shift 1
37 ;;
38 --no-channel-copy)
39 noChannelCopy=1
40 ;;
41 --no-root-passwd)
42 noRootPasswd=1
43 ;;
44 --no-bootloader)
45 noBootLoader=1
46 ;;
47 --show-trace)
48 extraBuildFlags+=("$i")
49 ;;
50 --help)
51 exec man nixos-install
52 exit 1
53 ;;
54 --debug)
55 set -x
56 ;;
57 *)
58 echo "$0: unknown option \`$i'"
59 exit 1
60 ;;
61 esac
62done
63
64if ! test -e "$mountPoint"; then
65 echo "mount point $mountPoint doesn't exist"
66 exit 1
67fi
68
69# Get the path of the NixOS configuration file.
70if [[ -z $NIXOS_CONFIG ]]; then
71 NIXOS_CONFIG=$mountPoint/etc/nixos/configuration.nix
72fi
73
74if [[ ${NIXOS_CONFIG:0:1} != / ]]; then
75 echo "$0: \$NIXOS_CONFIG is not an absolute path"
76 exit 1
77fi
78
79if [[ ! -e $NIXOS_CONFIG && -z $system ]]; then
80 echo "configuration file $NIXOS_CONFIG doesn't exist"
81 exit 1
82fi
83
84# A place to drop temporary stuff.
85trap "rm -rf $tmpdir" EXIT
86tmpdir="$(mktemp -d)"
87
88sub="auto?trusted=1"
89
90# Build the system configuration in the target filesystem.
91if [[ -z $system ]]; then
92 echo "building the configuration in $NIXOS_CONFIG..."
93 outLink="$tmpdir/system"
94 nix build --out-link "$outLink" --store "$mountPoint" "${extraBuildFlags[@]}" \
95 --extra-substituters "$sub" \
96 -f '<nixpkgs/nixos>' system -I "nixos-config=$NIXOS_CONFIG"
97 system=$(readlink -f $outLink)
98fi
99
100# Set the system profile to point to the configuration. TODO: combine
101# this with the previous step once we have a nix-env replacement with
102# a progress bar.
103nix-env --store "$mountPoint" "${extraBuildFlags[@]}" \
104 --extra-substituters "$sub" \
105 -p $mountPoint/nix/var/nix/profiles/system --set "$system"
106
107# Copy the NixOS/Nixpkgs sources to the target as the initial contents
108# of the NixOS channel.
109if [[ -z $noChannelCopy ]]; then
110 if [[ -z $channelPath ]]; then
111 channelPath="$(nix-env -p /nix/var/nix/profiles/per-user/root/channels -q nixos --no-name --out-path 2>/dev/null || echo -n "")"
112 fi
113 if [[ -n $channelPath ]]; then
114 echo "copying channel..."
115 mkdir -p $mountPoint/nix/var/nix/profiles/per-user/root
116 nix-env --store "$mountPoint" "${extraBuildFlags[@]}" --extra-substituters "$sub" \
117 -p $mountPoint/nix/var/nix/profiles/per-user/root/channels --set "$channelPath" --quiet
118 install -m 0700 -d $mountPoint/root/.nix-defexpr
119 ln -sfn /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels
120 fi
121fi
122
123# Mark the target as a NixOS installation, otherwise switch-to-configuration will chicken out.
124mkdir -m 0755 -p "$mountPoint/etc"
125touch "$mountPoint/etc/NIXOS"
126
127# Switch to the new system configuration. This will install Grub with
128# a menu default pointing at the kernel/initrd/etc of the new
129# configuration.
130if [[ -z $noBootLoader ]]; then
131 echo "installing the boot loader..."
132 # Grub needs an mtab.
133 ln -sfn /proc/mounts $mountPoint/etc/mtab
134 NIXOS_INSTALL_BOOTLOADER=1 nixos-enter --root "$mountPoint" -- /run/current-system/bin/switch-to-configuration boot
135fi
136
137# Ask the user to set a root password, but only if the passwd command
138# exists (i.e. when mutable user accounts are enabled).
139if [[ -z $noRootPasswd ]] && [ -t 0 ]; then
140 nixos-enter --root "$mountPoint" -c '[[ -e /nix/var/nix/profiles/system/sw/bin/passwd ]] && echo "setting root password..." && /nix/var/nix/profiles/system/sw/bin/passwd'
141fi
142
143echo "installation finished!"