2020-08-13 20:54:51 +01:00
#! @runtimeShell@
2006-11-08 02:34:14 +00:00
2018-02-05 18:50:36 +00:00
set -e
shopt -s nullglob
export PATH = @path@:$PATH
2006-11-08 02:34:14 +00:00
2014-09-03 22:10:40 +01:00
# Ensure a consistent umask.
umask 0022
2013-09-29 15:38:57 +01:00
# Parse the command line for the -I flag
extraBuildFlags = ( )
2020-08-11 17:15:28 +01:00
flakeFlags = ( )
2018-02-05 18:50:36 +00:00
mountPoint = /mnt
2018-02-27 18:58:23 +00:00
channelPath =
2018-10-14 19:12:08 +01:00
system =
2019-12-22 21:03:14 +00:00
verbosity = ( )
2013-09-29 15:38:57 +01:00
while [ " $# " -gt 0 ] ; do
i = " $1 " ; shift 1
case " $i " in
2018-02-07 18:33:22 +00:00
--max-jobs| -j| --cores| -I| --substituters)
2015-03-11 16:44:31 +00:00
j = " $1 " ; shift 1
extraBuildFlags += ( " $i " " $j " )
; ;
--option)
j = " $1 " ; shift 1
k = " $1 " ; shift 1
extraBuildFlags += ( " $i " " $j " " $k " )
2013-10-11 13:12:36 +01:00
; ;
2014-08-24 14:57:00 +01:00
--root)
mountPoint = " $1 " ; shift 1
; ;
2018-02-27 18:58:23 +00:00
--system| --closure)
2018-02-05 18:50:36 +00:00
system = " $1 " ; shift 1
2016-07-24 14:45:24 +01:00
; ;
2020-08-11 17:15:28 +01:00
--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 " )
; ;
2018-02-27 18:58:23 +00:00
--channel)
channelPath = " $1 " ; shift 1
; ;
2016-07-24 14:45:24 +01:00
--no-channel-copy)
noChannelCopy = 1
; ;
--no-root-passwd)
noRootPasswd = 1
; ;
--no-bootloader)
2016-08-16 01:15:27 +01:00
noBootLoader = 1
2016-07-24 14:45:24 +01:00
; ;
2020-10-14 21:45:22 +01:00
--show-trace| --impure| --keep-going)
2014-05-08 20:04:58 +01:00
extraBuildFlags += ( " $i " )
; ;
2013-10-11 13:12:36 +01:00
--help)
exec man nixos-install
exit 1
; ;
2018-02-05 18:50:36 +00:00
--debug)
set -x
; ;
2019-12-22 21:03:14 +00:00
-v*| --verbose)
verbosity += ( " $i " )
; ;
2013-10-11 13:12:36 +01:00
*)
echo " $0 : unknown option \` $i ' "
exit 1
; ;
2013-09-29 15:38:57 +01:00
esac
done
2007-01-11 00:06:46 +00:00
if ! test -e " $mountPoint " ; then
echo " mount point $mountPoint doesn't exist "
exit 1
fi
2007-02-05 15:52:55 +00:00
2020-06-15 04:04:41 +01:00
# 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
2014-05-08 23:25:05 +01:00
# Get the path of the NixOS configuration file.
2018-02-05 18:50:36 +00:00
if [ [ -z $NIXOS_CONFIG ] ] ; then
NIXOS_CONFIG = $mountPoint /etc/nixos/configuration.nix
2014-05-08 23:25:05 +01:00
fi
2018-02-05 18:50:36 +00:00
if [ [ ${ NIXOS_CONFIG : 0 : 1 } != / ] ] ; then
echo " $0 : \$NIXOS_CONFIG is not an absolute path "
2014-05-08 23:25:05 +01:00
exit 1
fi
2020-08-11 17:15:28 +01:00
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
2018-02-05 18:50:36 +00:00
echo " configuration file $NIXOS_CONFIG doesn't exist "
exit 1
2017-08-31 02:57:05 +01:00
fi
2006-11-11 17:59:08 +00:00
2018-02-05 18:50:36 +00:00
# A place to drop temporary stuff.
2020-08-11 17:15:28 +01:00
tmpdir = " $( mktemp -d -p " $mountPoint " ) "
trap 'rm -rf $tmpdir' EXIT
2020-04-25 11:00:05 +01:00
# store temporary files on target filesystem by default
export TMPDIR = ${ TMPDIR :- $tmpdir }
2015-09-30 20:43:06 +01:00
2018-02-07 18:33:22 +00:00
sub = "auto?trusted=1"
2012-05-15 14:50:36 +01:00
2018-02-05 18:50:36 +00:00
# Build the system configuration in the target filesystem.
if [ [ -z $system ] ] ; then
outLink = " $tmpdir /system "
2020-08-11 17:15:28 +01:00
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 " \
2020-10-03 18:03:55 +01:00
--store " $mountPoint " --extra-substituters " $sub " " ${ verbosity [@] } " \
2020-08-11 17:15:28 +01:00
" ${ extraBuildFlags [@] } " " ${ lockFlags [@] } " --out-link " $outLink "
fi
system = $( readlink -f " $outLink " )
2018-02-05 18:50:36 +00:00
fi
2017-08-31 02:57:05 +01:00
2018-02-05 18:50:36 +00:00
# Set the system profile to point to the configuration. TODO: combine
# this with the previous step once we have a nix-env replacement with
# a progress bar.
nix-env --store " $mountPoint " " ${ extraBuildFlags [@] } " \
2018-02-07 18:33:22 +00:00
--extra-substituters " $sub " \
2020-08-11 17:15:28 +01:00
-p " $mountPoint " /nix/var/nix/profiles/system --set " $system " " ${ verbosity [@] } "
2018-02-05 18:50:36 +00:00
# Copy the NixOS/Nixpkgs sources to the target as the initial contents
# of the NixOS channel.
if [ [ -z $noChannelCopy ] ] ; then
2018-02-27 18:58:23 +00:00
if [ [ -z $channelPath ] ] ; then
channelPath = " $( nix-env -p /nix/var/nix/profiles/per-user/root/channels -q nixos --no-name --out-path 2>/dev/null || echo -n "" ) "
fi
2018-02-05 18:50:36 +00:00
if [ [ -n $channelPath ] ] ; then
echo "copying channel..."
2020-08-11 17:15:28 +01:00
mkdir -p " $mountPoint " /nix/var/nix/profiles/per-user/root
2018-02-07 18:33:22 +00:00
nix-env --store " $mountPoint " " ${ extraBuildFlags [@] } " --extra-substituters " $sub " \
2020-08-11 17:15:28 +01:00
-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
2018-02-05 18:50:36 +00:00
fi
fi
2012-05-15 14:50:36 +01:00
2018-02-05 18:50:36 +00:00
# Mark the target as a NixOS installation, otherwise switch-to-configuration will chicken out.
2018-02-07 18:47:03 +00:00
mkdir -m 0755 -p " $mountPoint /etc "
2018-02-05 18:50:36 +00:00
touch " $mountPoint /etc/NIXOS "
2008-05-09 11:08:02 +01:00
2006-11-12 23:30:03 +00:00
# Switch to the new system configuration. This will install Grub with
# a menu default pointing at the kernel/initrd/etc of the new
# configuration.
2018-02-05 18:50:36 +00:00
if [ [ -z $noBootLoader ] ] ; then
echo "installing the boot loader..."
# Grub needs an mtab.
2020-08-11 17:15:28 +01:00
ln -sfn /proc/mounts " $mountPoint " /etc/mtab
2019-10-07 19:44:42 +01:00
NIXOS_INSTALL_BOOTLOADER = 1 nixos-enter --root " $mountPoint " -- /run/current-system/bin/switch-to-configuration boot
2016-08-16 01:15:27 +01:00
fi
2014-05-08 23:04:48 +01:00
2018-02-05 18:50:36 +00:00
# Ask the user to set a root password, but only if the passwd command
# exists (i.e. when mutable user accounts are enabled).
if [ [ -z $noRootPasswd ] ] && [ -t 0 ] ; then
2019-03-03 20:07:14 +00:00
if nixos-enter --root " $mountPoint " -c 'test -e /nix/var/nix/profiles/system/sw/bin/passwd' ; then
set +e
nixos-enter --root " $mountPoint " -c 'echo "setting root password..." && /nix/var/nix/profiles/system/sw/bin/passwd'
exit_code = $?
set -e
if [ [ $exit_code != 0 ] ] ; then
echo "Setting a root password failed with the above printed error."
echo " You can set the root password manually by executing \`nixos-enter --root ${ mountPoint @Q } \` and then running \`passwd\` in the shell of the new system. "
exit $exit_code
fi
fi
2014-05-08 23:04:48 +01:00
fi
echo "installation finished!"