From 9096fc6607a196f48df1da1dbe9b3f2b0908d76d Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Fri, 21 Aug 2026 03:59:06 +0530 Subject: scripts: added nix-update script - nix-update script can be used to automatically update both home-manager and nixos, either just update the flake inputs or recompute/rebuild the entire configuration with the updated derivations - fixed fzf unknown option error that appeared in the newer versions --- modules/scripts/nix-update/script.sh | 110 +++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 modules/scripts/nix-update/script.sh (limited to 'modules/scripts/nix-update/script.sh') diff --git a/modules/scripts/nix-update/script.sh b/modules/scripts/nix-update/script.sh new file mode 100644 index 0000000..7f2b6d0 --- /dev/null +++ b/modules/scripts/nix-update/script.sh @@ -0,0 +1,110 @@ +#!/usr/bin/env bash + +set -euo pipefail + +HM_DIR="$HOME/.config/home-manager" +NIXOS_DIR="/etc/nixos" +FLAKE_ONLY=false +TARGET="" + +usage() { + echo "Usage: nix-update [--flake-only]" + echo "" + echo "Options:" + echo " home-manager Update and switch home-manager configuration" + echo " nixos Update and switch NixOS configuration" + echo " both Update and switch both configurations" + echo "" + echo "Flags:" + echo " --flake-only Only run nix flake update, skip switch commands" + exit 1 +} + +parse_args() { + while [[ $# -gt 0 ]]; do + case "$1" in + home-manager | nixos | both) + if [[ -n "$TARGET" ]]; then + echo "Error: only one target allowed" + usage + fi + TARGET="$1" + shift + ;; + --flake-only) + FLAKE_ONLY=true + shift + ;; + -h | --help) + usage + ;; + *) + echo "Error: unknown option '$1'" + usage + ;; + esac + done + + if [[ -z "$TARGET" ]]; then + echo "Error: no target specified" + usage + fi +} + +update_home_manager() { + echo "==> Updating home-manager..." + cd "$HM_DIR" + + echo " Staging modules/private..." + git add modules/private -f + + echo " Running nix flake update..." + nix flake update + + if [[ "$FLAKE_ONLY" == false ]]; then + echo " Running home-manager switch..." + home-manager switch --flake "$HM_DIR#arpit" + fi + + echo " Unstaging modules/private..." + git rm --cached -r modules/private + + echo "==> home-manager update complete" +} + +update_nixos() { + echo "==> Updating NixOS..." + sudo -v + cd "$NIXOS_DIR" + + echo " Staging hardware-configuration.nix..." + git add hardware-configuration.nix -f + + echo " Running nix flake update..." + nix flake update + + if [[ "$FLAKE_ONLY" == false ]]; then + echo " Running nixos-rebuild switch..." + sudo nixos-rebuild switch + fi + + echo " Unstaging hardware-configuration.nix..." + git rm --cached hardware-configuration.nix + + echo "==> NixOS update complete" +} + +parse_args "$@" + +case "$TARGET" in + home-manager) + update_home_manager + ;; + nixos) + update_nixos + ;; + both) + update_nixos + update_home_manager + ;; +esac -- cgit v1.2.3