Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add store root for nixos install #114

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ clap = "3.0.0-beta.2"
flexi_logger = "0.16"
fork = "0.1"
futures-util = "0.3.6"
linked_hash_set = "0.1.4"
log = "0.4"
merge = "0.1.0"
notify = "5.0.0-pre.3"
Expand Down
44 changes: 44 additions & 0 deletions chroot-wrapper.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
set -e

# Re-exec ourselves in a private mount namespace so that our bind
# mounts get cleaned up automatically.
if [ -z "$NIXOS_ENTER_REEXEC" ]; then
export NIXOS_ENTER_REEXEC=1
if [ "$(id -u)" != 0 ]; then
extraFlags="-r"
fi
exec unshare --fork --mount --uts --mount-proc --pid $extraFlags -- "$0" "$@"
else
mount --make-rprivate /
fi

mountPoint=/mnt

while [ "$#" -gt 0 ]; do
i="$1"; shift 1
case "$i" in
--root)
mountPoint="$1"; shift 1
;;
--)
command=("$@")
break
;;
*)
echo "$0: unknown option \`$i'"
exit 1
;;
esac
done

mkdir -p "$mountPoint/dev" "$mountPoint/sys" "$mountPoint/tmp" "$mountPoint/etc" "$mountPoint/proc"
chmod 0755 "$mountPoint/dev" "$mountPoint/sys" "$mountPoint/tmp" "$mountPoint/etc" "$mountPoint/proc"
mount --rbind /dev "$mountPoint/dev"
mount --rbind /sys "$mountPoint/sys"
mount --rbind /proc "$mountPoint/proc"

touch "$mountPoint/etc/mtab" || true # might be already a working system
(cd "$mountPoint" && ln -snf "../proc/self/mounts" "etc/mtab") # Grub needs an mtab.

export CHROOTED=1
exec chroot "$mountPoint" "${command[@]}"
50 changes: 48 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,22 @@
executable = true;
destination = "/deploy-rs-activate";
})
(final.writeTextFile {
name = base.name + "-activate-chroot-wrapper";
text = ''
# no interpreter: absolute storepaths are not resolvable outside chroot
# run with bash <>, instead
${nixpkgs.lib.fileContents ./chroot-wrapper.bash};
'';
executable = true;
destination = "/deploy-rs-chroot-wrapper";
})
(final.writeTextFile {
name = base.name + "-activate-rs";
text = ''
#!${final.runtimeShell}
# required by bin/activate
export PATH="${nixpkgs.lib.makeBinPath [final.nixUnstable final.coreutils]}"
exec ${self.defaultPackage.${system}}/bin/activate "$@"
'';
executable = true;
Expand All @@ -84,15 +96,49 @@
};

nixos = base: (custom // { dryActivate = "$PROFILE/bin/switch-to-configuration dry-activate"; }) base.config.system.build.toplevel ''
export PATH="${nixpkgs.lib.makeBinPath [final.coreutils]}"
# work around https://github.com/NixOS/nixpkgs/issues/73404
cd /tmp

$PROFILE/bin/switch-to-configuration switch
_SYSTEM="/nix/var/nix/profiles/system"
_SWITCH_COMMAND="$PROFILE/bin/switch-to-configuration"

# Set $LOCALE_ARCHIVE to supress some Perl locale warnings.
export LOCALE_ARCHIVE="$PROFILE/sw/lib/locale/locale-archive"

_already_on_nixos() { [[ -f "/etc/NIXOS" ]]; }
_switch_cmd() {
"$_SWITCH_COMMAND" "$1"
}

_become_a_nixos() { touch /etc/NIXOS; }
_if_chrooted() { [[ "''${CHROOTED:-}" == "1" ]] && echo $@; }
_activate_system() {
# Run the activation script.
"$PROFILE/activate" 1>&2 || true
# Cooperatively respect our chrooted environment
${final.systemd}/bin/systemd-tmpfiles --create --remove $(_if_chrooted "--exclude-prefix=/dev") 1>&2 || true;
}

if _already_on_nixos
then
_switch_cmd switch
else
echo "-> Become a NixOS ..."
_become_a_nixos
echo "-> Activate the system ..."
_activate_system
echo "-> Install the boot loader ..."
NIXOS_INSTALL_BOOTLOADER=1 _switch_cmd boot
echo "-> Reboot ..."
$(_if_chrooted sync && echo b |tee /proc/sysrq-trigger)
${final.systemd}/bin/reboot --reboot
fi

# https://github.com/serokell/deploy-rs/issues/31
${with base.config.boot.loader;
final.lib.optionalString systemd-boot.enable
"sed -i '/^default /d' ${efi.efiSysMountPoint}/loader/loader.conf"}
"${final.gnused}/bin/sed -i '/^default /d' ${efi.efiSysMountPoint}/loader/loader.conf"}
'';

home-manager = base: custom base.activationPackage "$PROFILE/activate";
Expand Down
Loading