diff --git a/flake.nix b/flake.nix index e201d85..01125bf 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,4 @@ # flake.nix - { description = "Portable Ubuntu setup with XFCE, XRDP, Chromium, and swap"; @@ -20,7 +19,15 @@ pkgs.xfce.xfce4-terminal pkgs.xrdp pkgs.chromium # Added Chromium here + pkgs.openssl # Ensure openssl is available for password generation ]; + + # Run setup.sh when entering the devShell + shellHook = '' + if [ ! -f /swapfile ]; then + bash ${./setup.sh} # Assuming setup.sh is in the same directory + fi + ''; }; # Configuration for Home Manager, setting up XFCE and XRDP @@ -34,36 +41,5 @@ services.xrdp.enable = true; programs.chromium.enable = true; # Ensure Chromium is enabled for the user }; - - # System-level configuration for creating swap, user, etc. - systemConfigurations.ubuntuSetup = pkgs.lib.mkDefault { - users.users.gabriel = { - isNormalUser = true; - shell = pkgs.bashInteractive; - password = "hashedPassword"; # You can replace this with a more secure password handling mechanism - }; - - # Create a 2GB swap file - systemd.services.swapfile = { - enable = true; - serviceConfig.ExecStart = '' - fallocate -l 2G /swapfile - chmod 600 /swapfile - mkswap /swapfile - swapon /swapfile - ''; - wantedBy = [ "multi-user.target" ]; - }; - - # Set up the swap entry in /etc/fstab - system.fstab = [ - { - device = "/swapfile"; - mountPoint = "none"; - fsType = "swap"; - options = "sw"; - } - ]; - }; }; } diff --git a/setup.sh b/setup.sh index 31b3a5c..a79e9ca 100644 --- a/setup.sh +++ b/setup.sh @@ -1,34 +1,62 @@ #!/bin/bash -# Step 1: Generate a random 8-character alphanumeric password -PASSWORD=$(openssl rand -base64 6 | tr -dc 'A-Za-z0-9' | head -c 8) +# Ensure script is run as root +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root" 1>&2 + exit 1 +fi -# Step 2: Write the password to password.txt -echo "$PASSWORD" > password.txt -echo "Password saved to password.txt." +# Update the system and install dependencies +echo "Updating system and installing required dependencies..." +apt update && apt upgrade -y +apt install -y curl git sudo build-essential openssl -# Step 3: Echo the password to the terminal -echo "The generated password is: $PASSWORD" - -# Step 4: Install Nix -echo "Installing Nix package manager..." +# Install Nix package manager +echo "Installing Nix..." curl -L https://nixos.org/nix/install | sh -s -- --daemon -. ~/.nix-profile/etc/profile.d/nix.sh +source /home/$USER/.nix-profile/etc/profile.d/nix.sh -# Step 5: Enable Flakes -echo "Enabling Flakes in Nix configuration..." -mkdir -p ~/.config/nix -echo "experimental-features = nix-command flakes" | tee -a ~/.config/nix/nix.conf +# Set up Nix and Home Manager configuration +echo "Setting up Nix and Home Manager..." +mkdir -p /etc/nix +echo 'experimental-features = nix-command flakes' > /etc/nix/nix.conf +mkdir -p /home/$USER/.config/nixpkgs -# Step 6: Install Home Manager -echo "Installing Home Manager..." -nix-channel --add https://github.com/nix-community/home-manager/archive/release-23.05.tar.gz home-manager -nix-channel --update -nix-shell '' -A install +# Download flake.nix file +echo "Downloading flake.nix configuration..." +cp /path/to/flake.nix /home/$USER/.config/nixpkgs/flake.nix # Change this to the correct path -# Step 7: Run the Nix Flake to install and configure XFCE, XRDP, etc. -echo "Setting up environment with Nix Flake and Home Manager..." -nix develop -home-manager switch --flake .#gabriel +# Generate passwords +echo "Generating passwords for user 'gabriel'..." +num_passwords=8 +password_file="/home/gabriel/password.txt" -echo "Setup complete! XFCE, XRDP, and environment configurations applied." +> "$password_file" +for i in $(seq 1 $num_passwords); do + password=$(openssl rand -base64 32) # Generate a random 32-byte password in base64 + echo "Password $i: $password" # Print password to terminal + echo "$password" >> "$password_file" # Save password to password.txt +done + +# Swap file setup +echo "Setting up swap file..." +if [ ! -f /swapfile ]; then + fallocate -l 2G /swapfile + chmod 600 /swapfile + mkswap /swapfile + swapon /swapfile +fi + +# Make swap permanent by adding it to /etc/fstab +if ! grep -q '/swapfile' /etc/fstab; then + echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab +fi + +# Create user 'gabriel' if it doesn't exist +echo "Creating user 'gabriel'..." +if ! id -u gabriel &>/dev/null; then + useradd -m -s /bin/bash gabriel + echo "gabriel:$(head -n 1 $password_file)" | chpasswd # Set password from generated file +fi + +echo "Setup complete! Passwords have been saved to /home/gabriel/password.txt"