Merge pull request #95194 from ju1m/nixos-install

nixos-install: add support for flakes
This commit is contained in:
WORLDofPEACE 2020-09-05 15:31:14 -04:00 committed by GitHub
commit d0972c9637
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 21 deletions

View File

@ -45,6 +45,10 @@
<replaceable>path</replaceable> <replaceable>path</replaceable>
</arg> </arg>
<arg>
<option>--flake</option> <replaceable>flake-uri</replaceable>
</arg>
<arg> <arg>
<arg choice='plain'> <arg choice='plain'>
<option>--channel</option> <option>--channel</option>
@ -199,6 +203,18 @@
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>
<option>--flake</option> <replaceable>flake-uri</replaceable>#<replaceable>name</replaceable>
</term>
<listitem>
<para>
Build the NixOS system from the specified flake.
The flake must contain an output named
<literal>nixosConfigurations.<replaceable>name</replaceable></literal>.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term> <term>
<option>--channel</option> <option>--channel</option>

View File

@ -521,7 +521,7 @@
<varlistentry> <varlistentry>
<term> <term>
<option>--flake</option> <replaceable>flake-uri</replaceable>[<replaceable>name</replaceable>] <option>--flake</option> <replaceable>flake-uri</replaceable><optional>#<replaceable>name</replaceable></optional>
</term> </term>
<listitem> <listitem>
<para> <para>

View File

@ -10,6 +10,7 @@ umask 0022
# Parse the command line for the -I flag # Parse the command line for the -I flag
extraBuildFlags=() extraBuildFlags=()
flakeFlags=()
mountPoint=/mnt mountPoint=/mnt
channelPath= channelPath=
@ -34,6 +35,23 @@ while [ "$#" -gt 0 ]; do
--system|--closure) --system|--closure)
system="$1"; shift 1 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) --channel)
channelPath="$1"; shift 1 channelPath="$1"; shift 1
;; ;;
@ -92,14 +110,32 @@ if [[ ${NIXOS_CONFIG:0:1} != / ]]; then
exit 1 exit 1
fi 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" echo "configuration file $NIXOS_CONFIG doesn't exist"
exit 1 exit 1
fi fi
# A place to drop temporary stuff. # A place to drop temporary stuff.
tmpdir="$(mktemp -d -p $mountPoint)" tmpdir="$(mktemp -d -p "$mountPoint")"
trap "rm -rf $tmpdir" EXIT trap 'rm -rf $tmpdir' EXIT
# store temporary files on target filesystem by default # store temporary files on target filesystem by default
export TMPDIR=${TMPDIR:-$tmpdir} export TMPDIR=${TMPDIR:-$tmpdir}
@ -108,12 +144,19 @@ sub="auto?trusted=1"
# Build the system configuration in the target filesystem. # Build the system configuration in the target filesystem.
if [[ -z $system ]]; then if [[ -z $system ]]; then
echo "building the configuration in $NIXOS_CONFIG..."
outLink="$tmpdir/system" outLink="$tmpdir/system"
nix-build --out-link "$outLink" --store "$mountPoint" "${extraBuildFlags[@]}" \ if [[ -z $flake ]]; then
--extra-substituters "$sub" \ echo "building the configuration in $NIXOS_CONFIG..."
'<nixpkgs/nixos>' -A system -I "nixos-config=$NIXOS_CONFIG" ${verbosity[@]} nix-build --out-link "$outLink" --store "$mountPoint" "${extraBuildFlags[@]}" \
system=$(readlink -f $outLink) --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 fi
# Set the system profile to point to the configuration. TODO: combine # Set the system profile to point to the configuration. TODO: combine
@ -121,7 +164,7 @@ fi
# a progress bar. # a progress bar.
nix-env --store "$mountPoint" "${extraBuildFlags[@]}" \ nix-env --store "$mountPoint" "${extraBuildFlags[@]}" \
--extra-substituters "$sub" \ --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 # Copy the NixOS/Nixpkgs sources to the target as the initial contents
# of the NixOS channel. # of the NixOS channel.
@ -131,12 +174,12 @@ if [[ -z $noChannelCopy ]]; then
fi fi
if [[ -n $channelPath ]]; then if [[ -n $channelPath ]]; then
echo "copying channel..." 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" \ nix-env --store "$mountPoint" "${extraBuildFlags[@]}" --extra-substituters "$sub" \
-p $mountPoint/nix/var/nix/profiles/per-user/root/channels --set "$channelPath" --quiet \ -p "$mountPoint"/nix/var/nix/profiles/per-user/root/channels --set "$channelPath" --quiet \
${verbosity[@]} "${verbosity[@]}"
install -m 0700 -d $mountPoint/root/.nix-defexpr install -m 0700 -d "$mountPoint"/root/.nix-defexpr
ln -sfn /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels ln -sfn /nix/var/nix/profiles/per-user/root/channels "$mountPoint"/root/.nix-defexpr/channels
fi fi
fi fi
@ -150,7 +193,7 @@ touch "$mountPoint/etc/NIXOS"
if [[ -z $noBootLoader ]]; then if [[ -z $noBootLoader ]]; then
echo "installing the boot loader..." echo "installing the boot loader..."
# Grub needs an mtab. # 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 NIXOS_INSTALL_BOOTLOADER=1 nixos-enter --root "$mountPoint" -- /run/current-system/bin/switch-to-configuration boot
fi fi

View File

@ -17,6 +17,7 @@ showSyntax() {
origArgs=("$@") origArgs=("$@")
extraBuildFlags=() extraBuildFlags=()
lockFlags=() lockFlags=()
flakeFlags=()
action= action=
buildNix=1 buildNix=1
fast= fast=
@ -99,6 +100,7 @@ while [ "$#" -gt 0 ]; do
;; ;;
--flake) --flake)
flake="$1" flake="$1"
flakeFlags=(--experimental-features 'nix-command flakes')
shift 1 shift 1
;; ;;
--recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file) --recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file)
@ -281,7 +283,7 @@ fi
# Resolve the flake. # Resolve the flake.
if [[ -n $flake ]]; then if [[ -n $flake ]]; then
flake=$(nix flake info --json "${extraBuildFlags[@]}" "${lockFlags[@]}" -- "$flake" | jq -r .url) flake=$(nix "${flakeFlags[@]}" flake info --json "${extraBuildFlags[@]}" "${lockFlags[@]}" -- "$flake" | jq -r .url)
fi fi
# Find configuration.nix and open editor instead of building. # Find configuration.nix and open editor instead of building.
@ -293,7 +295,7 @@ if [ "$action" = edit ]; then
fi fi
exec ${EDITOR:-nano} "$NIXOS_CONFIG" exec ${EDITOR:-nano} "$NIXOS_CONFIG"
else else
exec nix edit "${lockFlags[@]}" -- "$flake#$flakeAttr" exec nix "${flakeFlags[@]}" edit "${lockFlags[@]}" -- "$flake#$flakeAttr"
fi fi
exit 1 exit 1
fi fi
@ -419,7 +421,7 @@ if [ -z "$rollback" ]; then
pathToConfig="$(nixBuild '<nixpkgs/nixos>' --no-out-link -A system "${extraBuildFlags[@]}")" pathToConfig="$(nixBuild '<nixpkgs/nixos>' --no-out-link -A system "${extraBuildFlags[@]}")"
else else
outLink=$tmpDir/result outLink=$tmpDir/result
nix build "$flake#$flakeAttr.config.system.build.toplevel" \ nix "${flakeFlags[@]}" build "$flake#$flakeAttr.config.system.build.toplevel" \
"${extraBuildFlags[@]}" "${lockFlags[@]}" --out-link $outLink "${extraBuildFlags[@]}" "${lockFlags[@]}" --out-link $outLink
pathToConfig="$(readlink -f $outLink)" pathToConfig="$(readlink -f $outLink)"
fi fi
@ -429,7 +431,7 @@ if [ -z "$rollback" ]; then
if [[ -z $flake ]]; then if [[ -z $flake ]]; then
pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")" pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")"
else else
nix build "$flake#$flakeAttr.config.system.build.toplevel" "${extraBuildFlags[@]}" "${lockFlags[@]}" nix "${flakeFlags[@]}" build "$flake#$flakeAttr.config.system.build.toplevel" "${extraBuildFlags[@]}" "${lockFlags[@]}"
pathToConfig="$(readlink -f ./result)" pathToConfig="$(readlink -f ./result)"
fi fi
elif [ "$action" = build-vm ]; then elif [ "$action" = build-vm ]; then

View File

@ -22,7 +22,7 @@ let
src = ./nixos-install.sh; src = ./nixos-install.sh;
inherit (pkgs) runtimeShell; inherit (pkgs) runtimeShell;
nix = config.nix.package.out; nix = config.nix.package.out;
path = makeBinPath [ nixos-enter ]; path = makeBinPath [ pkgs.nixUnstable nixos-enter ];
}; };
nixos-rebuild = nixos-rebuild =