diff options
| author | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-08-29 02:02:23 +0530 |
|---|---|---|
| committer | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-08-29 03:50:32 +0530 |
| commit | 41bdeb88009dd232808edee9fc0e4ae94358d8d6 (patch) | |
| tree | c1a41f54e086064dac178ebed24d36ab34087c32 /modules/system | |
| parent | 530299d97926d45939f831dc3ea5e444d2839d08 (diff) | |
| download | home-manager-config-41bdeb88009dd232808edee9fc0e4ae94358d8d6.tar.gz home-manager-config-41bdeb88009dd232808edee9fc0e4ae94358d8d6.zip | |
refactor: moving script into modules that makes sense
- instead of collecting all scripts into modules/scripts we are moving
them to modules that makes sense (yazi-file-chooser.sh into yazi)
Diffstat (limited to 'modules/system')
| -rw-r--r-- | modules/system/deep-clean/deep-clean.sh | 35 | ||||
| -rw-r--r-- | modules/system/deep-clean/default.nix | 18 | ||||
| -rw-r--r-- | modules/system/default.nix | 3 | ||||
| -rw-r--r-- | modules/system/nix-update/default.nix | 24 | ||||
| -rw-r--r-- | modules/system/nix-update/nix-update.sh | 122 | ||||
| -rw-r--r-- | modules/system/nix-update/nix-update.zsh | 22 | ||||
| -rw-r--r-- | modules/system/system-monitor/assertions.nix | 19 | ||||
| -rw-r--r-- | modules/system/system-monitor/default.nix | 41 | ||||
| -rw-r--r-- | modules/system/system-monitor/system-monitor.sh | 26 |
9 files changed, 310 insertions, 0 deletions
diff --git a/modules/system/deep-clean/deep-clean.sh b/modules/system/deep-clean/deep-clean.sh new file mode 100644 index 0000000..bb28182 --- /dev/null +++ b/modules/system/deep-clean/deep-clean.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +set -euo pipefail + +echo "--- Starting Nix Deep Clean (keeping only current generations) ---" + +if [[ $EUID -eq 0 ]]; then + echo "Error: Do not run this script with sudo." + echo "Run it as your normal user; it will ask for sudo when required." + exit 1 +fi + +echo "Removing old NixOS system generations..." +sudo nix-env -p /nix/var/nix/profiles/system --delete-generations +1 + +echo "Removing old Home Manager generations..." +if command -v home-manager &> /dev/null; then + home-manager generations | \ + awk 'NR>1 {print $1}' | \ + xargs -r home-manager remove-generations +fi + +echo "Removing old user profile generations..." +nix-env --delete-generations +1 + +echo "Removing result symlinks..." +find . -name "result" -type l -delete + +echo "Running garbage collector..." +sudo nix-collect-garbage -d +nix-collect-garbage -d + +echo "Optimising Nix store..." +nix store optimise + +echo "--- Cleanup Complete ---" diff --git a/modules/system/deep-clean/default.nix b/modules/system/deep-clean/default.nix new file mode 100644 index 0000000..af75382 --- /dev/null +++ b/modules/system/deep-clean/default.nix @@ -0,0 +1,18 @@ +# Deep clean script for Nix systems +{ + config, + lib, + pkgs, + ... +}: +let + inherit ((import ../../lib/script.nix { inherit lib pkgs; })) mkScriptModule; +in +mkScriptModule { + scope = [ "system" ]; + name = "deep-clean"; + path = ./deep-clean.sh; + description = "Deep clean script for Nix systems\nRemoves old generations, garbage, and optimizes store\nWARNING: Do NOT run with sudo - run as normal user"; + deps = [ pkgs.bash ]; + inherit config; +} diff --git a/modules/system/default.nix b/modules/system/default.nix index 591cd10..9bd3a01 100644 --- a/modules/system/default.nix +++ b/modules/system/default.nix @@ -4,8 +4,11 @@ imports = [ ./bottom ./brightnessctl + ./deep-clean ./htop + ./nix-update ./nvtop + ./system-monitor ./systemctl-tui ]; } 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 diff --git a/modules/system/system-monitor/assertions.nix b/modules/system/system-monitor/assertions.nix new file mode 100644 index 0000000..08b9f7c --- /dev/null +++ b/modules/system/system-monitor/assertions.nix @@ -0,0 +1,19 @@ +{ config, ... }: +[ + { + assertion = !config.system.system-monitor.enable || config.system.bottom.enable; + message = "system.system-monitor is enabled but requires `system.bottom.enable`."; + } + { + assertion = !config.system.system-monitor.enable || config.system.nvtop.enable; + message = "system.system-monitor is enabled but requires `system.nvtop.enable`."; + } + { + assertion = !config.system.system-monitor.enable || config.terminal.tmux.enable; + message = "system.system-monitor is enabled but requires `terminal.tmux.enable`."; + } + { + assertion = !config.system.system-monitor.enable || config.terminal.kitty.enable; + message = "system.system-monitor is enabled but requires `terminal.kitty.enable`."; + } +] diff --git a/modules/system/system-monitor/default.nix b/modules/system/system-monitor/default.nix new file mode 100644 index 0000000..e103ed3 --- /dev/null +++ b/modules/system/system-monitor/default.nix @@ -0,0 +1,41 @@ +# System monitor script using tmux to run bottom and nvtop side-by-side +{ + config, + lib, + pkgs, + ... +}: +let + inherit ((import ../../lib/script.nix { inherit lib pkgs; })) mkScriptModule; + script = mkScriptModule { + scope = [ "system" ]; + name = "system-monitor"; + path = ./system-monitor.sh; + description = "System monitor script using tmux to run bottom and nvtop side-by-side\nOpens a tmux session with:\n - Left pane: bottom (system/process monitor)\n - Right pane: nvtop (GPU monitor)"; + deps = [ + config.system.bottom.package + config.system.nvtop.package + config.terminal.tmux.package + config.terminal.kitty.package + pkgs.bash + ]; + desktop = { + enable = true; + displayName = "System Monitor"; + icon = "bottom-system-monitor"; + }; + inherit config; + }; +in +{ + options = script.options; + config = lib.mkMerge [ + script.config + { + assertions = import ./assertions.nix { inherit config lib; }; + home.file.".local/share/icons/hicolor/scalable/apps/bottom-system-monitor.svg" = { + source = ../../../assets/icons/bottom-system-monitor.svg; + }; + } + ]; +} diff --git a/modules/system/system-monitor/system-monitor.sh b/modules/system/system-monitor/system-monitor.sh new file mode 100644 index 0000000..7a9e7ea --- /dev/null +++ b/modules/system/system-monitor/system-monitor.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +SESSION="system-monitor" + +kitty @ set-font-size -- -5 + +cleanup() { + kitty @ set-font-size -- +5 +} +trap cleanup EXIT INT TERM + +# Kill any existing session +tmux kill-session -t "$SESSION" 2>/dev/null + +# Start new tmux session with btm wrapped to kill session on exit +tmux new-session -d -s "$SESSION" -c "$HOME" "bash -c 'btm; tmux kill-session -t $SESSION'" + +# Split horizontally: right pane runs nvtop wrapped similarly +tmux split-window -h -t "$SESSION:0" -c "$HOME" "bash -c 'nvtop; tmux kill-session -t $SESSION'" + +tmux set -g status off +tmux set -g mouse on +tmux set -g focus-events on + +# Select main pane and attach +tmux select-pane -t "$SESSION:0.0" +tmux attach-session -t "$SESSION" |
