2018-07-11 23:10:34 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
2019-09-21 22:26:01 +03:00
|
|
|
# FIXME: Add hook for mounting, right now it's hardcoded to only mount "/".
|
|
|
|
# (This'll allow complex schemes like LVM)
|
|
|
|
|
|
|
|
let
|
|
|
|
rootfs = config.fileSystems."/".device;
|
2019-09-23 22:45:18 +03:00
|
|
|
inherit (lib) mkMerge mkOrder;
|
2019-09-21 22:26:01 +03:00
|
|
|
in
|
2018-07-11 23:10:34 +03:00
|
|
|
with import ./initrd-order.nix;
|
|
|
|
{
|
2019-09-23 22:45:18 +03:00
|
|
|
config = mkMerge [
|
|
|
|
{
|
|
|
|
mobile.boot.stage-1.init = mkOrder SWITCH_ROOT_INIT ''
|
|
|
|
targetRoot=/mnt
|
2018-07-11 23:10:34 +03:00
|
|
|
|
2019-10-13 00:50:37 +03:00
|
|
|
_mount_root() {
|
|
|
|
mkdir -p $targetRoot || return 1
|
|
|
|
mount "${rootfs}" $targetRoot || return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
_find_init_path() {
|
2019-09-23 22:45:18 +03:00
|
|
|
local _system=""
|
2019-09-21 22:26:01 +03:00
|
|
|
|
2019-09-23 22:45:18 +03:00
|
|
|
# Using -L is required, as the link chain is most likely dangling.
|
|
|
|
if [ -L "$targetRoot/nix/var/nix/profiles/system" ]; then
|
|
|
|
# There is a system symlink, use it.
|
|
|
|
# What's that strange dance? We're canonicalizing one level deep of an
|
|
|
|
# absolute symlink that we can't easily canonicalize otherwise.
|
|
|
|
_system=$(cd $targetRoot/nix/var/nix/profiles/; readlink $(readlink system))
|
|
|
|
elif [ -e "$targetRoot/nix-path-registration" ]; then
|
|
|
|
# Otherwise, try to find one in nix-path-registration.
|
|
|
|
_system="$(grep '^/nix/store/[a-z0-9]\+-nixos-system-' $targetRoot/nix-path-registration | head -1)"
|
|
|
|
else
|
2019-10-13 00:50:37 +03:00
|
|
|
init_fail FF00FF init_not_found "Could not find init path for stage-2"
|
2019-09-23 22:45:18 +03:00
|
|
|
fi
|
2018-07-11 23:10:34 +03:00
|
|
|
|
2019-09-23 22:45:18 +03:00
|
|
|
echo "$_system/init"
|
|
|
|
}
|
2019-09-21 22:23:50 +03:00
|
|
|
|
2019-10-13 00:50:37 +03:00
|
|
|
_mount_root || init_fail FFFF00 root_mount_failure "Could not mount root filesystem"
|
2019-09-23 22:45:18 +03:00
|
|
|
|
|
|
|
echo ""
|
|
|
|
echo "***"
|
|
|
|
echo ""
|
2019-10-13 00:50:37 +03:00
|
|
|
echo "Swiching root to $(_find_init_path)"
|
2018-07-11 23:10:34 +03:00
|
|
|
echo ""
|
2019-09-23 22:45:18 +03:00
|
|
|
echo "***"
|
2018-07-11 23:10:34 +03:00
|
|
|
echo ""
|
|
|
|
|
2019-09-23 22:45:18 +03:00
|
|
|
for mp in /proc /sys /dev /run; do
|
|
|
|
mkdir -m 0755 -p $targetRoot/$mp
|
|
|
|
mount --move $mp $targetRoot/$mp
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
|
|
|
mobile.boot.stage-1.init = mkOrder SWITCH_ROOT_HAPPENING ''
|
2019-10-13 00:50:37 +03:00
|
|
|
exec env -i $(type -P switch_root) $targetRoot $(_find_init_path)
|
|
|
|
init_fail FF0000 init_exec_failure "Could not exec stage-2 init"
|
2019-09-23 22:45:18 +03:00
|
|
|
'';
|
2018-07-11 23:10:34 +03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|