refactors update_nixos_flake and update_home_manager_flake functions for improved configuration handling and validation

This commit is contained in:
2024-11-08 14:30:26 +01:00
parent 0b8a5a150d
commit b13cb3ff0e

View File

@ -136,7 +136,6 @@ EOF
git -C "$DOTFILES_DIR" status git -C "$DOTFILES_DIR" status
echo echo
} }
# Update these functions in the previous script:
update_nixos_flake() { update_nixos_flake() {
local hostname="$1" local hostname="$1"
@ -150,15 +149,8 @@ update_nixos_flake() {
common_module="./common/server.nix" common_module="./common/server.nix"
fi fi
# Create temporary file # Create new configuration entry
local temp_file=$(mktemp) local new_config=" \"$hostname\" = nixpkgs.lib.nixosSystem {
# Read file up to nixosConfigurations
awk '/nixosConfigurations = {/,/{/{if($0 ~ /nixosConfigurations = {/) {p=NR}} p==NR{print}' "$flake_file" > "$temp_file"
# Add new configuration
cat >> "$temp_file" << EOF
"$hostname" = nixpkgs.lib.nixosSystem {
inherit system; inherit system;
modules = [ modules = [
./hardware/$hostname.nix ./hardware/$hostname.nix
@ -170,13 +162,27 @@ update_nixos_flake() {
isWorkstation = $isWorkstation; isWorkstation = $isWorkstation;
isServer = $isServer; isServer = $isServer;
}; };
}; };"
EOF # Create temporary file
local temp_file=$(mktemp)
# Find the line number where nixosConfigurations = { appears
local config_line=$(grep -n "nixosConfigurations = {" "$flake_file" | cut -d: -f1)
if [ -z "$config_line" ]; then
rm "$temp_file"
die "Could not find nixosConfigurations in flake.nix"
fi
# Add the rest of the original file # Copy the file up to the line after nixosConfigurations = {
awk 'BEGIN{p=0}/nixosConfigurations = {/{p=1;next}p==1{print}' "$flake_file" | \ head -n "$config_line" "$flake_file" > "$temp_file"
tail -n +2 >> "$temp_file"
# Add the new configuration
echo "$new_config" >> "$temp_file"
# Add the rest of the file starting from the line after nixosConfigurations = {
tail -n +"$((config_line + 1))" "$flake_file" >> "$temp_file"
# Validate the new file # Validate the new file
if ! nix-shell -p nixfmt --run "nixfmt $temp_file"; then if ! nix-shell -p nixfmt --run "nixfmt $temp_file"; then
@ -194,29 +200,36 @@ update_home_manager_flake() {
local isServer="$2" local isServer="$2"
local flake_file="$DOTFILES_DIR/config/home-manager/flake.nix" local flake_file="$DOTFILES_DIR/config/home-manager/flake.nix"
# Create temporary file # Create new configuration entry
local temp_file=$(mktemp) local new_config=" \"$hostname\" = home-manager.lib.homeManagerConfiguration {
# Read file up to homeConfigurations
awk '/homeConfigurations = {/,/{/{if($0 ~ /homeConfigurations = {/) {p=NR}} p==NR{print}' "$flake_file" > "$temp_file"
# Add new configuration
cat >> "$temp_file" << EOF
"$hostname" = home-manager.lib.homeManagerConfiguration {
inherit pkgs; inherit pkgs;
modules = [ ./home.nix ]; modules = [ ./home.nix ];
extraSpecialArgs = { extraSpecialArgs = {
inherit pkgs pkgs-unstable; inherit pkgs pkgs-unstable;
isServer = $isServer; isServer = $isServer;
hostname = "$hostname"; hostname = \"$hostname\";
}; };
}; };"
EOF # Create temporary file
local temp_file=$(mktemp)
# Find the line number where homeConfigurations = { appears
local config_line=$(grep -n "homeConfigurations = {" "$flake_file" | cut -d: -f1)
if [ -z "$config_line" ]; then
rm "$temp_file"
die "Could not find homeConfigurations in flake.nix"
fi
# Add the rest of the original file # Copy the file up to the line after homeConfigurations = {
awk 'BEGIN{p=0}/homeConfigurations = {/{p=1;next}p==1{print}' "$flake_file" | \ head -n "$config_line" "$flake_file" > "$temp_file"
tail -n +2 >> "$temp_file"
# Add the new configuration
echo "$new_config" >> "$temp_file"
# Add the rest of the file starting from the line after homeConfigurations = {
tail -n +"$((config_line + 1))" "$flake_file" >> "$temp_file"
# Validate the new file # Validate the new file
if ! nix-shell -p nixfmt --run "nixfmt $temp_file"; then if ! nix-shell -p nixfmt --run "nixfmt $temp_file"; then