Skip to content

Instantly share code, notes, and snippets.

@paradigm
Last active June 13, 2021 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paradigm/61e736fdb1741ec4fdd593f91b3c467e to your computer and use it in GitHub Desktop.
Save paradigm/61e736fdb1741ec4fdd593f91b3c467e to your computer and use it in GitHub Desktop.
Broken attempt at adding NixOS support to `brl fetch`
#!/bedrock/libexec/busybox sh
#
# NixOS bootstrap support
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# Copyright (c) 2016-2018 Daniel Thau <danthau@bedrocklinux.org>
#
# errors trying to set up sandbox on nixos-install. Could not find a way to disable it.
. /bedrock/share/common-code
trap 'fetch_abort "Unexpected error occurred."' EXIT
check_supported() {
false
}
check_experimental() {
false
}
distro_arch_name() {
fetch_abort "brl-fetch does not support distro/architecture pair"
}
list_mirrors() {
# Could not find a list of package mirrors. This is the one found in-use by default on a fresh install.
#
echo "https://nixos.org/releases/"
}
speed_test_url() {
echo "nix/latest/install"
}
list_releases() {
echo "rolling"
}
default_release() {
echo "rolling"
}
fetch() {
step_init 99
if [ -z "${target_dir:-}" ]; then
fetch_abort "Essential variable not set"
fi
step "Determining name"
name="${flag_name:-${distro}}"
notice "Using ${color_strat}${name}${color_norm}"
step "Determining CPU architecture"
arch="$(distro_arch_name)"
notice "Using ${color_file}${arch}${color_norm}"
step "Determining release"
release="${flag_release:-$(default_release)}"
notice "Using ${color_file}${release}${color_norm}"
step "Determining mirror"
mirror="$(pick_mirror "$(speed_test_url)")"
notice "Using ${color_link}${mirror}${color_norm}"
step "Making directory structure"
bootstrap_dir="${target_dir}/brl-bootstrap"
mkdir -p "${target_dir}/etc/nix" "${bootstrap_dir}/target-root"
step "Downloading bootstrap software"
url="$(find_link "${mirror}/nix/latest/" "^nix-.*-${arch}-linux.tar.bz2")"
wget -O "${bootstrap_dir}/nix.tar.bz2" "${url}"
step "Extracting bootstrap software"
tar xf "${bootstrap_dir}/nix.tar.bz2" -C "${bootstrap_dir}"
step "Installing bootstrap software"
for dir in bin sbin usr/bin usr/sbin etc dev nix root/.config/nix/ tmp proc etc/nix; do
mkdir -p "${bootstrap_dir}/${dir}"
done
for file in etc/group etc/passwd; do
touch "${bootstrap_dir}/${file}"
done
cp /bedrock/libexec/busybox "${bootstrap_dir}"
chroot "${bootstrap_dir}" /busybox --install
for cfg in "${target_dir}/etc/nix/nix.conf" "${bootstrap_dir}/etc/nix/nix.conf" "${bootstrap_dir}/root/.config/nix/nix.conf"; do
(
echo "build-users-group ="
echo "sandbox = false"
echo "build-use-sandbox = false"
echo "trusted-users = root"
) > "${cfg}"
done
setup_chroot "${bootstrap_dir}"
LC_ALL=C chroot "${bootstrap_dir}" /busybox addgroup nixbld
LC_ALL=C chroot "${bootstrap_dir}" /busybox adduser -s /bin/sh -D -G nixbld -u 0 root
LC_ALL=C chroot "${bootstrap_dir}" /bin/sh -c "/nix-*-${arch}-linux/install"
LC_ALL=C chroot "${bootstrap_dir}" /bin/sh -c '. /root/.nix-profile/etc/profile.d/nix.sh ; nix-channel --add https://nixos.org/channels/nixos-19.03 nixpkgs' # TODO: look up proper channel
LC_ALL=C chroot "${bootstrap_dir}" /bin/sh -c '. /root/.nix-profile/etc/profile.d/nix.sh ; nix-channel --update' # TODO: look up proper channel
LC_ALL=C chroot "${bootstrap_dir}" /bin/sh -c '. /root/.nix-profile/etc/profile.d/nix.sh ; nix-env --option sandbox false -iE "_: with import <nixpkgs/nixos> { configuration = {}; }; with config.system.build; [ nixos-generate-config nixos-install ]"'
LC_ALL=C chroot "${bootstrap_dir}" /bin/sh -c '. /root/.nix-profile/etc/profile.d/nix.sh ; nix-env --option sandbox false --install coreutils'
step "Running bootstrap software"
mount --bind "${target_dir}" "${bootstrap_dir}/target-root"
LC_ALL=C chroot "${bootstrap_dir}" /bin/sh -c '. /root/.nix-profile/etc/profile.d/nix.sh ; nixos-generate-config --root /target-root'
sed 's/boot.loader.grub.enable =.*/boot.loader.grub.enable = false;/g' "${target_dir}/etc/nixos/configuration.nix" > "${target_dir}/etc/nixos/configuration.nix-new"
mv "${target_dir}/etc/nixos/configuration.nix-new" "${target_dir}/etc/nixos/configuration.nix"
echo PAUSE; read PAUSE
LC_ALL=C chroot "${bootstrap_dir}" /bin/sh -c '. /root/.nix-profile/etc/profile.d/nix.sh ; nixos-install --option sandbox false --root /target-root --no-root-passwd --no-bootloader'
# . /root/.nix-profile/etc/profile.d/nix.sh
# echo PAUSE; read PAUSE
# chroot "${bootstrap_dir}" /bin/sh -c "/nix-*-${arch}-linux/install"
# sudo PATH="$PATH" NIX_PATH="$NIX_PATH" "$(which nixos-install)" --root "/target-root"
echo END-PAUSE; read PAUSE
}
Copy link

ghost commented Jun 12, 2021

:(

@paradigm
Copy link
Author

If it makes you feel any better, while I've had to switch focus elsewhere the effort to get this to work hasn't died. @unrooted is currently working on it.

@unrooted
Copy link

Yes, that's right, I was trying my best and I still do from time to time. You, @xosefulk, or anyone reading this, can see the progress and what I've done in here. Sadly, I didn't have time to research this further, but feel free to comment there about your own effort if you try doing the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment