aboutsummaryrefslogtreecommitdiffstats
path: root/modules/system/nix-update
diff options
context:
space:
mode:
Diffstat (limited to 'modules/system/nix-update')
-rw-r--r--modules/system/nix-update/default.nix24
-rw-r--r--modules/system/nix-update/nix-update.sh122
-rw-r--r--modules/system/nix-update/nix-update.zsh22
3 files changed, 168 insertions, 0 deletions
diff --git a/modules/system/nix-update/default.nix b/modules/system/nix-update/default.nix
new file mode 100644
index 0000000..13c15f6
--- /dev/null
+++ b/modules/system/nix-update/default.nix
@@ -0,0 +1,24 @@
+# Update NixOS and/or Home Manager flake configuration
+{
+ config,
+ lib,
+ pkgs,
+ ...
+}:
+let
+ inherit ((import ../../lib/script.nix { inherit lib pkgs; })) mkScriptModule;
+in
+mkScriptModule {
+ scope = [ "system" ];
+ name = "nix-update";
+ path = ./nix-update.sh;
+ description = "Update NixOS and/or Home Manager flake configuration\nRuns nix flake update and rebuilds the system";
+ deps = [
+ pkgs.git
+ pkgs.nix
+ config.programs.home-manager.package
+ pkgs.bash
+ ];
+ completion.zsh = builtins.readFile ./nix-update.zsh;
+ inherit config;
+}
diff --git a/modules/system/nix-update/nix-update.sh b/modules/system/nix-update/nix-update.sh
new file mode 100644
index 0000000..0f1c0c1
--- /dev/null
+++ b/modules/system/nix-update/nix-update.sh
@@ -0,0 +1,122 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+HM_DIR="$HOME/.config/home-manager"
+NIXOS_DIR="/etc/nixos"
+ONLY_FLAKE=false
+ONLY_SWITCH=false
+TARGET=""
+
+usage() {
+ echo "Usage: nix-update <home-manager|nixos|both> [--only-flake|--only-switch]"
+ 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 " --only-flake Only run nix flake update, skip switch commands"
+ echo " --only-switch Only run switch commands, skip nix flake update"
+ 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
+ ;;
+ --only-flake)
+ ONLY_FLAKE=true
+ shift
+ ;;
+ --only-switch)
+ ONLY_SWITCH=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
+
+ if [[ "$ONLY_SWITCH" == false ]]; then
+ echo " Running nix flake update..."
+ nix flake update
+ fi
+
+ if [[ "$ONLY_FLAKE" == false ]]; then
+ echo " Running home-manager switch..."
+ home-manager switch --flake "$HM_DIR#arpit"
+ fi
+
+ echo " Unstaging modules/private..."
+ for f in modules/private/*.nix; do
+ [[ "$f" == *.example.nix ]] || git rm --cached "$f"
+ done
+
+ 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
+
+ if [[ "$ONLY_SWITCH" == false ]]; then
+ echo " Running nix flake update..."
+ nix flake update
+ fi
+
+ if [[ "$ONLY_FLAKE" == 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
diff --git a/modules/system/nix-update/nix-update.zsh b/modules/system/nix-update/nix-update.zsh
new file mode 100644
index 0000000..a9500e6
--- /dev/null
+++ b/modules/system/nix-update/nix-update.zsh
@@ -0,0 +1,22 @@
+#compdef nix-update
+
+_arguments \
+ '1: :->target' \
+ '*:: :->flags'
+
+case $state in
+ target)
+ local -a commands
+ commands=(
+ 'home-manager:Update and switch home-manager configuration'
+ 'nixos:Update and switch NixOS configuration'
+ 'both:Update and switch both configurations'
+ )
+ _describe 'command' commands
+ ;;
+ flags)
+ _arguments \
+ '--only-flake[Only run nix flake update, skip switch commands]' \
+ '--only-switch[Only run switch commands, skip nix flake update]'
+ ;;
+esac