Skip to content

Commit

Permalink
feat: provide installation from local ISO
Browse files Browse the repository at this point in the history
  • Loading branch information
Swarsel committed Dec 21, 2024
1 parent 57135bd commit adb241f
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 27 deletions.
172 changes: 149 additions & 23 deletions SwarselSystems.org
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,11 @@ In this section I am creating some attributes that define general concepts of my
type = "app";
program = "${self.packages.${system}.swarsel-install}/bin/swarsel-install";
};

rebuild = {
type = "app";
program = "${self.packages.${system}.swarsel-rebuild}/bin/swarsel-rebuild";
};
});
devShells = forAllSystems (
system:
Expand Down Expand Up @@ -1975,7 +1980,7 @@ This is the "reference implementation" of a setup that runs without NixOS, only

This is just a demo host. It applies all the configuration found in the common parts of the flake, but disables all secrets-related features (as they would not work without the proper SSH keys).

I also set the =WLR_ALLOW_SOFTWARE_RENDERER=1= to allow this configuration to run in a virtualized environment. I also enable =qemuGuest= for a smoother experience when testing on QEMU.
I also set the =WLR_RENDERER_ALLOW_SOFTWARE=1= to allow this configuration to run in a virtualized environment. I also enable =qemuGuest= for a smoother experience when testing on QEMU.


#+begin_src nix :tangle hosts/nixos/chaostheatre/default.nix
Expand Down Expand Up @@ -2006,7 +2011,7 @@ I also set the =WLR_ALLOW_SOFTWARE_RENDERER=1= to allow this configuration to ru
};

environment.variables = {
WLR_ALLOW_SOFTWARE_RENDERER = 1;
WLR_RENDERER_ALLOW_SOFTWARE = 1;
};

services.qemuGuest.enable = true;
Expand Down Expand Up @@ -2984,11 +2989,11 @@ This program sets up a new NixOS host.
}
#+end_src

**** swarsel-install
**** swarsel-rebuild

This program sets up a new NixOS host.

#+begin_src shell :tangle scripts/swarsel-install.sh
#+begin_src shell :tangle scripts/swarsel-rebuild.sh
set -eo pipefail

target_flake="chaostheatre"
Expand Down Expand Up @@ -3074,6 +3079,126 @@ This program sets up a new NixOS host.



#+begin_src nix :tangle pkgs/swarsel-rebuild/default.nix
{ writeShellApplication, git }:

writeShellApplication {
name = "swarsel-rebuild";
runtimeInputs = [ git ];
text = builtins.readFile ../../scripts/swarsel-rebuild.sh;
}
#+end_src

**** swarsel-install

This program sets up a new NixOS host.

#+begin_src shell :tangle scripts/swarsel-install.sh
set -eo pipefail

target_flake="chaostheatre"
target_user="swarsel"
fs_type="ext4"
disk=""

function help_and_exit() {
echo
echo "Remotely installs NixOS on a target machine using this nix-config."
echo
echo "USAGE: $0 -d <disk> [OPTIONS]"
echo
echo "ARGS:"
echo " -d <disk> specify disk to install on."
echo " -f <target_flake> specify flake to deploy the nixos config of."
echo " Default: chaostheatre"
echo " -u <target_user> specify user to deploy for."
echo " Default: swarsel"
echo " -t <fs_type> specify file system type to deploy for."
echo " Default: ext4"
echo " -h | --help Print this help."
exit 0
}

function green() {
echo -e "\x1B[32m[+] $1 \x1B[0m"
if [ -n "${2-}" ]; then
echo -e "\x1B[32m[+] $($2) \x1B[0m"
fi
}
function yellow() {
echo -e "\x1B[33m[*] $1 \x1B[0m"
if [ -n "${2-}" ]; then
echo -e "\x1B[33m[*] $($2) \x1B[0m"
fi
}

while [[ $# -gt 0 ]]; do
case "$1" in
-f)
shift
target_flake=$1
;;
-u)
shift
target_user=$1
;;
-t)
shift
fs_type=$1
;;
-d)
shift
disk=$1
;;
-h | --help) help_and_exit ;;
,*)
echo "Invalid option detected."
help_and_exit
;;
esac
shift
done

cd /home/"$target_user"

if [ ! -d /home/"$target_user"/.dotfiles ]; then
green "Cloning repository from GitHub"
git clone https://github.com/Swarsel/.dotfiles.git
fi

local_keys=$(ssh-add -L || true)
pub_key=$(cat /home/"$target_user"/.dotfiles/secrets/keys/ssh/nbl-imba-2.pub)
read -ra pub_arr <<< "$pub_key"

cd .dotfiles
if [[ $local_keys != *"${pub_arr[1]}"* ]]; then
yellow "The ssh key for this configuration is not available."
green "Adjusting flake.nix so that the configuration is buildable"
sed -i '/nix-secrets = {/,/^[[:space:]]*};/d' flake.nix
git add flake.nix
fi
sudo parted "$disk" mklabel gpt mkpart "EFI system partition" fat32 1MiB 1025MiB set 1 esp on mkpart "root partition" "$fs_type" 1025MiB 100% type 2 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709

sudo mount "$disk"1 /mnt/boot
sudo mount "$disk"2 /mnt

sudo nixos-generate-config --root /mnt --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/
git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
# sudo rm -rf /root/.nix-defexpr/channels
# sudo rm -rf /nix/var/nix/profiles/per-user/channels
sudo mkdir -p /root/.local/share/nix/
printf '{\"extra-substituters\":{\"https://nix-community.cachix.org\":true,\"https://nix-community.cachix.org https://cache.ngi0.nixos.org/\":true},\"extra-trusted-public-keys\":{\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=\":true,\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= cache.ngi0.nixos.org-1:KqH5CBLNSyX184S9BKZJo1LxrxJ9ltnY2uAs5c/f1MA=\":true}}' > /root/.local/share/nix/trusted-settings.json
green "Installing flake $target_flake"
sudo nixos-install --flake .#"$target_flake"
yellow "Please keep in mind that this is only a demo of the configuration. Things might break unexpectedly."
git restore --staged /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
git restore /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
git restore --staged /home/"$target_user"/.dotfiles/flake.nix
git restore /home/"$target_user"/.dotfiles/flake.nix
#+end_src



#+begin_src nix :tangle pkgs/swarsel-install/default.nix
{ writeShellApplication, git }:

Expand Down Expand Up @@ -5368,28 +5493,29 @@ Used for storing sessions in e.g. Nextcloud. Using this on a system level keeps
This is used to better integrate Sway into the system on NixOS hosts. On the home-manager side, the =package= attribute will be =null= for such an host, using the systems derivation instead.

#+begin_src nix :tangle profiles/common/nixos/sway.nix
{ pkgs, ... }:
{
{ pkgs, ... }:
{

programs.sway = {
enable = true;
package = pkgs.swayfx;
wrapperFeatures = {
base = true;
gtk = true;
programs.sway = {
enable = true;
package = pkgs.swayfx;
wrapperFeatures = {
base = true;
gtk = true;
};

extraSessionCommands = ''
export XDG_SESSION_DESKTOP=sway
export SDL_VIDEODRIVER=wayland
export QT_QPA_PLATFORM=wayland-egl
export QT_WAYLAND_DISABLE_WINDOWDECORATION=1
export QT_QPA_PLATFORM_PLUGIN_PATH="${pkgs.libsForQt5.qt5.qtbase.bin}/lib/qt-${pkgs.libsForQt5.qt5.qtbase.version}/plugins";
export MOZ_ENABLE_WAYLAND=1
export MOZ_DISABLE_RDD_SANDBOX=1
'';
};

extraSessionCommands = ''
export XDG_SESSION_DESKTOP=sway
export SDL_VIDEODRIVER=wayland
export QT_QPA_PLATFORM=wayland-egl
export QT_WAYLAND_DISABLE_WINDOWDECORATION=1
export MOZ_ENABLE_WAYLAND=1
export MOZ_DISABLE_RDD_SANDBOX=1
'';
};

}
}
#+end_src

**** xdg-portal
Expand Down
5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@
type = "app";
program = "${self.packages.${system}.swarsel-install}/bin/swarsel-install";
};

rebuild = {
type = "app";
program = "${self.packages.${system}.swarsel-rebuild}/bin/swarsel-rebuild";
};
});
devShells = forAllSystems (
system:
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/chaostheatre/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ in
};

environment.variables = {
WLR_ALLOW_SOFTWARE_RENDERER = 1;
WLR_RENDERER_ALLOW_SOFTWARE = 1;
};

services.qemuGuest.enable = true;
Expand Down
7 changes: 7 additions & 0 deletions pkgs/swarsel-rebuild/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ writeShellApplication, git }:

writeShellApplication {
name = "swarsel-rebuild";
runtimeInputs = [ git ];
text = builtins.readFile ../../scripts/swarsel-rebuild.sh;
}
26 changes: 23 additions & 3 deletions scripts/swarsel-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ set -eo pipefail

target_flake="chaostheatre"
target_user="swarsel"
fs_type="ext4"
disk=""

function help_and_exit() {
echo
echo "Remotely installs NixOS on a target machine using this nix-config."
echo
echo "USAGE: $0 [OPTIONS]"
echo "USAGE: $0 -d <disk> [OPTIONS]"
echo
echo "ARGS:"
echo " -d <disk> specify disk to install on."
echo " -f <target_flake> specify flake to deploy the nixos config of."
echo " Default: chaostheatre"
echo " -u <target_user> specify user to deploy for."
echo " Default: swarsel"
echo " -t <fs_type> specify file system type to deploy for."
echo " Default: ext4"
echo " -h | --help Print this help."
exit 0
}
Expand Down Expand Up @@ -41,6 +46,14 @@ while [[ $# -gt 0 ]]; do
shift
target_user=$1
;;
-t)
shift
fs_type=$1
;;
-d)
shift
disk=$1
;;
-h | --help) help_and_exit ;;
*)
echo "Invalid option detected."
Expand Down Expand Up @@ -68,12 +81,19 @@ if [[ $local_keys != *"${pub_arr[1]}"* ]]; then
sed -i '/nix-secrets = {/,/^[[:space:]]*};/d' flake.nix
git add flake.nix
fi
sudo nixos-generate-config --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/
sudo parted "$disk" mklabel gpt mkpart "EFI system partition" fat32 1MiB 1025MiB set 1 esp on mkpart "root partition" "$fs_type" 1025MiB 100% type 2 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709

sudo mount "$disk"1 /mnt/boot
sudo mount "$disk"2 /mnt

sudo nixos-generate-config --root /mnt --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/
git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
# sudo rm -rf /root/.nix-defexpr/channels
# sudo rm -rf /nix/var/nix/profiles/per-user/channels
sudo mkdir -p /root/.local/share/nix/
printf '{\"extra-substituters\":{\"https://nix-community.cachix.org\":true,\"https://nix-community.cachix.org https://cache.ngi0.nixos.org/\":true},\"extra-trusted-public-keys\":{\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=\":true,\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= cache.ngi0.nixos.org-1:KqH5CBLNSyX184S9BKZJo1LxrxJ9ltnY2uAs5c/f1MA=\":true}}' > /root/.local/share/nix/trusted-settings.json
green "Installing flake $target_flake"
sudo nixos-rebuild --show-trace --flake .#"$target_flake" boot
sudo nixos-install --flake .#"$target_flake"
yellow "Please keep in mind that this is only a demo of the configuration. Things might break unexpectedly."
git restore --staged /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
git restore /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
Expand Down
81 changes: 81 additions & 0 deletions scripts/swarsel-rebuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
set -eo pipefail

target_flake="chaostheatre"
target_user="swarsel"

function help_and_exit() {
echo
echo "Remotely installs NixOS on a target machine using this nix-config."
echo
echo "USAGE: $0 [OPTIONS]"
echo
echo "ARGS:"
echo " -f <target_flake> specify flake to deploy the nixos config of."
echo " Default: chaostheatre"
echo " -u <target_user> specify user to deploy for."
echo " Default: swarsel"
echo " -h | --help Print this help."
exit 0
}

function green() {
echo -e "\x1B[32m[+] $1 \x1B[0m"
if [ -n "${2-}" ]; then
echo -e "\x1B[32m[+] $($2) \x1B[0m"
fi
}
function yellow() {
echo -e "\x1B[33m[*] $1 \x1B[0m"
if [ -n "${2-}" ]; then
echo -e "\x1B[33m[*] $($2) \x1B[0m"
fi
}

while [[ $# -gt 0 ]]; do
case "$1" in
-f)
shift
target_flake=$1
;;
-u)
shift
target_user=$1
;;
-h | --help) help_and_exit ;;
*)
echo "Invalid option detected."
help_and_exit
;;
esac
shift
done

cd /home/"$target_user"

if [ ! -d /home/"$target_user"/.dotfiles ]; then
green "Cloning repository from GitHub"
git clone https://github.com/Swarsel/.dotfiles.git
fi

local_keys=$(ssh-add -L || true)
pub_key=$(cat /home/"$target_user"/.dotfiles/secrets/keys/ssh/nbl-imba-2.pub)
read -ra pub_arr <<< "$pub_key"

cd .dotfiles
if [[ $local_keys != *"${pub_arr[1]}"* ]]; then
yellow "The ssh key for this configuration is not available."
green "Adjusting flake.nix so that the configuration is buildable"
sed -i '/nix-secrets = {/,/^[[:space:]]*};/d' flake.nix
git add flake.nix
fi
sudo nixos-generate-config --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/
git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
sudo mkdir -p /root/.local/share/nix/
printf '{\"extra-substituters\":{\"https://nix-community.cachix.org\":true,\"https://nix-community.cachix.org https://cache.ngi0.nixos.org/\":true},\"extra-trusted-public-keys\":{\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=\":true,\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= cache.ngi0.nixos.org-1:KqH5CBLNSyX184S9BKZJo1LxrxJ9ltnY2uAs5c/f1MA=\":true}}' > /root/.local/share/nix/trusted-settings.json
green "Installing flake $target_flake"
sudo nixos-rebuild --show-trace --flake .#"$target_flake" boot
yellow "Please keep in mind that this is only a demo of the configuration. Things might break unexpectedly."
git restore --staged /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
git restore /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix
git restore --staged /home/"$target_user"/.dotfiles/flake.nix
git restore /home/"$target_user"/.dotfiles/flake.nix

0 comments on commit adb241f

Please sign in to comment.