aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/nixos/modules/installer/tools/nixos-install.sh
diff options
context:
space:
mode:
Diffstat (limited to 'nixpkgs/nixos/modules/installer/tools/nixos-install.sh')
-rw-r--r--nixpkgs/nixos/modules/installer/tools/nixos-install.sh86
1 files changed, 70 insertions, 16 deletions
diff --git a/nixpkgs/nixos/modules/installer/tools/nixos-install.sh b/nixpkgs/nixos/modules/installer/tools/nixos-install.sh
index 1bccbbfaf24..a180d1bc4c1 100644
--- a/nixpkgs/nixos/modules/installer/tools/nixos-install.sh
+++ b/nixpkgs/nixos/modules/installer/tools/nixos-install.sh
@@ -1,4 +1,4 @@
-#! @shell@
+#! @runtimeShell@
set -e
shopt -s nullglob
@@ -10,6 +10,7 @@ umask 0022
# Parse the command line for the -I flag
extraBuildFlags=()
+flakeFlags=()
mountPoint=/mnt
channelPath=
@@ -34,6 +35,23 @@ while [ "$#" -gt 0 ]; do
--system|--closure)
system="$1"; shift 1
;;
+ --flake)
+ flake="$1"
+ flakeFlags=(--experimental-features 'nix-command flakes')
+ shift 1
+ ;;
+ --recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file)
+ lockFlags+=("$i")
+ ;;
+ --update-input)
+ j="$1"; shift 1
+ lockFlags+=("$i" "$j")
+ ;;
+ --override-input)
+ j="$1"; shift 1
+ k="$1"; shift 1
+ lockFlags+=("$i" "$j" "$k")
+ ;;
--channel)
channelPath="$1"; shift 1
;;
@@ -71,6 +89,17 @@ if ! test -e "$mountPoint"; then
exit 1
fi
+# Verify permissions are okay-enough
+checkPath="$(realpath "$mountPoint")"
+while [[ "$checkPath" != "/" ]]; do
+ mode="$(stat -c '%a' "$checkPath")"
+ if [[ "${mode: -1}" -lt "5" ]]; then
+ echo "path $checkPath should have permissions 755, but had permissions $mode. Consider running 'chmod o+rx $checkPath'."
+ exit 1
+ fi
+ checkPath="$(dirname "$checkPath")"
+done
+
# Get the path of the NixOS configuration file.
if [[ -z $NIXOS_CONFIG ]]; then
NIXOS_CONFIG=$mountPoint/etc/nixos/configuration.nix
@@ -81,14 +110,32 @@ if [[ ${NIXOS_CONFIG:0:1} != / ]]; then
exit 1
fi
-if [[ ! -e $NIXOS_CONFIG && -z $system ]]; then
+if [[ -n $flake ]]; then
+ if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
+ flake="${BASH_REMATCH[1]}"
+ flakeAttr="${BASH_REMATCH[2]}"
+ fi
+ if [[ -z "$flakeAttr" ]]; then
+ echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri."
+ echo "For example, to use the output nixosConfigurations.foo from the flake.nix, append \"#foo\" to the flake-uri."
+ exit 1
+ fi
+ flakeAttr="nixosConfigurations.\"$flakeAttr\""
+fi
+
+# Resolve the flake.
+if [[ -n $flake ]]; then
+ flake=$(nix "${flakeFlags[@]}" flake info --json "${extraBuildFlags[@]}" "${lockFlags[@]}" -- "$flake" | jq -r .url)
+fi
+
+if [[ ! -e $NIXOS_CONFIG && -z $system && -z $flake ]]; then
echo "configuration file $NIXOS_CONFIG doesn't exist"
exit 1
fi
# A place to drop temporary stuff.
-tmpdir="$(mktemp -d -p $mountPoint)"
-trap "rm -rf $tmpdir" EXIT
+tmpdir="$(mktemp -d -p "$mountPoint")"
+trap 'rm -rf $tmpdir' EXIT
# store temporary files on target filesystem by default
export TMPDIR=${TMPDIR:-$tmpdir}
@@ -97,12 +144,19 @@ sub="auto?trusted=1"
# Build the system configuration in the target filesystem.
if [[ -z $system ]]; then
- echo "building the configuration in $NIXOS_CONFIG..."
outLink="$tmpdir/system"
- nix-build --out-link "$outLink" --store "$mountPoint" "${extraBuildFlags[@]}" \
- --extra-substituters "$sub" \
- '<nixpkgs/nixos>' -A system -I "nixos-config=$NIXOS_CONFIG" ${verbosity[@]}
- system=$(readlink -f $outLink)
+ if [[ -z $flake ]]; then
+ echo "building the configuration in $NIXOS_CONFIG..."
+ nix-build --out-link "$outLink" --store "$mountPoint" "${extraBuildFlags[@]}" \
+ --extra-substituters "$sub" \
+ '<nixpkgs/nixos>' -A system -I "nixos-config=$NIXOS_CONFIG" "${verbosity[@]}"
+ else
+ echo "building the flake in $flake..."
+ nix "${flakeFlags[@]}" build "$flake#$flakeAttr.config.system.build.toplevel" \
+ --extra-substituters "$sub" "${verbosity[@]}" \
+ "${extraBuildFlags[@]}" "${lockFlags[@]}" --out-link "$outLink"
+ fi
+ system=$(readlink -f "$outLink")
fi
# Set the system profile to point to the configuration. TODO: combine
@@ -110,7 +164,7 @@ fi
# a progress bar.
nix-env --store "$mountPoint" "${extraBuildFlags[@]}" \
--extra-substituters "$sub" \
- -p $mountPoint/nix/var/nix/profiles/system --set "$system" ${verbosity[@]}
+ -p "$mountPoint"/nix/var/nix/profiles/system --set "$system" "${verbosity[@]}"
# Copy the NixOS/Nixpkgs sources to the target as the initial contents
# of the NixOS channel.
@@ -120,12 +174,12 @@ if [[ -z $noChannelCopy ]]; then
fi
if [[ -n $channelPath ]]; then
echo "copying channel..."
- mkdir -p $mountPoint/nix/var/nix/profiles/per-user/root
+ mkdir -p "$mountPoint"/nix/var/nix/profiles/per-user/root
nix-env --store "$mountPoint" "${extraBuildFlags[@]}" --extra-substituters "$sub" \
- -p $mountPoint/nix/var/nix/profiles/per-user/root/channels --set "$channelPath" --quiet \
- ${verbosity[@]}
- install -m 0700 -d $mountPoint/root/.nix-defexpr
- ln -sfn /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels
+ -p "$mountPoint"/nix/var/nix/profiles/per-user/root/channels --set "$channelPath" --quiet \
+ "${verbosity[@]}"
+ install -m 0700 -d "$mountPoint"/root/.nix-defexpr
+ ln -sfn /nix/var/nix/profiles/per-user/root/channels "$mountPoint"/root/.nix-defexpr/channels
fi
fi
@@ -139,7 +193,7 @@ touch "$mountPoint/etc/NIXOS"
if [[ -z $noBootLoader ]]; then
echo "installing the boot loader..."
# Grub needs an mtab.
- ln -sfn /proc/mounts $mountPoint/etc/mtab
+ ln -sfn /proc/mounts "$mountPoint"/etc/mtab
NIXOS_INSTALL_BOOTLOADER=1 nixos-enter --root "$mountPoint" -- /run/current-system/bin/switch-to-configuration boot
fi