#!/usr/bin/env bash dedicateduser=nixos if [ "$(whoami)" != "${dedicateduser}" ] then echo "Please execute this script as user nixos" exit 1 fi cat > "/tmp/$(whoami)_sudo" << EOF $(whoami) ALL = NOPASSWD: /usr/bin/mkdir * /nix $(whoami) ALL = NOPASSWD: /usr/bin/chown $(whoami) /nix $(whoami) ALL = NOPASSWD: /usr/sbin/groupadd -g 30000 nixbld $(whoami) ALL = NOPASSWD: /usr/sbin/groupdel nixbld $(whoami) ALL = NOPASSWD: /usr/bin/useadd -u 30000 -g nixbld -G nixbld nixbld $(whoami) ALL = NOPASSWD: /usr/bin/userdel nixbld $(whoami) ALL = NOPASSWD: /home/$(whoami)/.nix-profile/bin/nixos-generate-config --root /mnt/$(whoami) $(whoami) ALL = NOPASSWD: /usr/sbin/chroot /mnt/$(whoami)* $(whoami) ALL = NOPASSWD: /usr/sbin/debootstrap * /mnt/$(whoami)* EOF add_nixos_user() { echo "# adduser $(whoami)" echo "And add sudo rights" echo "# cp /tmp/$(whoami)_sudo /etc/sudoers.d/$(whoami)" } if [ "$(whoami)" != "${dedicateduser}" ] then echo "Please execute this script as user nixos" grep -q ${dedicateduser} /etc/passwd || add_sid_user exit 1 fi if [ ! -d /nix ] then sudo mkdir -m 0755 /nix sudo chown nixos /nix fi if [ ! -d "${HOME}"/.nix-profile ] then if ! curl -L https://nixos.org/nix/install | sh then echo "curl error" exit 1 fi fi # shellcheck source=/dev/null source "${HOME}"/.nix-profile/etc/profile.d/nix.sh # nixpkgs https://channels.nixos.org/nixpkgs-unstable if nix-channel --list | grep -q unstable then if ! nix-channel --add https://channels.nixos.org/nixos-26.05 nixpkgs then echo "Channel add error" exit 1 fi if ! nix-channel --update then echo "channel update error" exit 1 fi fi if [ ! -x /home/nixos/.nix-profile/bin/nixos-generate-config ] then if ! nix-env -f '' -iA nixos-install-tools then echo "tools installation error" exit 1 fi fi if [ ! -d /mnt/"$(whoami)"/etc/nixos/configuration.nix ] then if ! sudo /home/"$(whoami)"/.nix-profile/bin/nixos-generate-config --root /mnt/"$(whoami)" then echo "generate config error" exit 1 fi fi if ! grep -q nixbld /etc/group then if ! sudo groupadd -g 30000 nixbld then echo "groupadd error" exit 1 fi if ! sudo useradd -u 30000 -g nixbld -G nixbld nixbld then echo "useradd error" exit 1 fi fi # if ! grep "${target} ext" /etc/mtab # then # echo "Error : mount partition under ${target}" # exit 1 # fi # # if ! dpkg -l | grep "^ii debootstrap" # then # echo "Error: install debootstrap" # fi # # if [ ! -d "${target}/debootstrap" ] # then # if ! sudo /usr/sbin/debootstrap sid "${target}" http://deb.debian.org/debian/ # then # echo "Error: please check" # fi # fi # # if [ ! -e mount_chroot.sh ] # then # echo "mount_chroot.sh absent" # echo "link it here" # exit 1 # fi # # # shellcheck source=/dev/null # source mount_chroot.sh "${target}" # # echo "--- Creating the ${target}/etc/ Files" # if ! grep " / " "${target}"/etc/fstab # then # echo "--- Creating /etc/fstab" # FSTAB=${target}/tmp/fstab # if [ -e "${FSTAB}" ] # then # rm "${FSTAB}" # fi # blockdevice="$(mount |grep "${target} type ext4" |cut --delimiter=' ' --fields=1 |cut --delimiter='/' --fields=3)" # for link in /dev/disk/by-uuid/* # do # case $(readlink "${link}") in # *${blockdevice}) # blockuuid=$(basename "${link}") # if [[ ! "${blockuuid}" =~ ^[a-zA-Z0-9-]+$ ]] # then # echo "Can't init /etc/fstab" # echo "Fix it by ourself" # exit 1 # else # cat > "${FSTAB}" << EOSF # UUID=${blockuuid} / ext4 defaults 0 0 # EOSF # fi # ;; # *) # continue # ;; # esac # done # if [ ! -e "${FSTAB}" ] # then # echo "Can't init /etc/fstab" # echo "Fix it by ourself" # exit 1 # fi # swapuuid=$(lsblk --output UUID,MOUNTPOINTS |grep SWAP |head -1 |cut --delimiter=' ' --fields=1) # if [[ "${blockuuid}" =~ ^[a-zA-Z0-9-]+$ ]] # then # cat >> "${FSTAB}" << EOSF # UUID=${swapuuid} none swap sw 0 0 # EOSF # echo "The first swap partition found of the host was written in /etc/fstab" # echo "Fix it of you have/want another swap" # else # echo "Can't init swap /etc/fstab" # echo "Fix it by ourself" # exit 1 # fi # echo "--- Creating /etc/adjtime" # cp --verbose /etc/adjtime "${target}"/tmp/adjtime # echo "--- Creating /etc/network/interfaces.d/lo.conf" # cat > "${target}"/tmp/lo.conf <<"EOSF" # # The loopback network interface # auto lo # iface lo inet loopback # EOSF # echo "--- Creating /etc/network/interfaces.d/eno1.conf" # cat > "${target}"/tmp/eno1.conf <<"EOSF" # auto eno1 # iface eno1 inet dhcp # EOSF # echo "--- Creating /etc/resolv.conf" # cp --verbose /etc/resolv.conf "${target}"/tmp/resolv.conf # echo "--- Creating /etc/hostname" # cp --verbose /etc/hostname "${target}"/tmp/hostname # echo "--- Creating /etc/hosts" # cp --verbose /etc/hosts "${target}"/tmp/hosts # echo "--- Creating /etc/locale.conf" # cp --verbose /etc/locale.conf "${target}"/tmp/locale.conf # echo "--- Creating /etc/default/keyboard" # cp --verbose /etc/default/keyboard "${target}"/tmp/keyboard # echo "--- Creating sudofile executable" # sudofile=/tmp/mnt_$(whoami)_etc_files.sh # cat > "${sudofile}" << "EOSF" # cp --verbose /tmp/fstab /etc/fstab # cp --verbose /tmp/adjtime /etc/adjtime # cp --verbose /tmp/lo.conf /etc/network/interfaces.d/lo.conf # cp --verbose /tmp/eno1.conf /etc/network/interfaces.d/eno1.conf # cp --verbose /tmp/resolv.conf /etc/resolv.conf # cp --verbose /tmp/hostname /etc/hostname # cp --verbose /tmp/hosts /etc/hosts # cp --verbose /tmp/locale.conf /etc/locate.conf # cp --verbose /tmp/keyboard /etc/default/keyboard # apt update # echo "root:$(basename $(ls -1 /etc/s*w))" | chpasswd # apt --yes install linux-image-amd64 ssh # apt --yes install locales console-setup # EOSF # echo "bash mount_chroot.sh ${target} ${sudofile}" # if ! bash mount_chroot.sh "${target}" "${sudofile}" # then # echo "Configuration error" # exit 1 # fi # # # fi