diff --git a/overlays.nix b/overlays.nix index 3bc87b1..a5ba76c 100644 --- a/overlays.nix +++ b/overlays.nix @@ -3,8 +3,8 @@ { flake.overlays = { default = final: prev: { - # plasticity = inputs.self.packages.${final.system}.plasticity; toggleaudiosink = inputs.self.packages.${final.system}.toggleaudiosink; + hm-cli = inputs.self.packages.${final.system}.hm-cli; }; }; } diff --git a/packages.nix b/packages.nix index 4ae6a5e..e83ad26 100644 --- a/packages.nix +++ b/packages.nix @@ -6,6 +6,7 @@ { packages = { toggleaudiosink = pkgs.callPackage ./packages/toggleaudiosink.nix { }; + hm-cli = pkgs.callPackage ./packages/hm-cli.nix { }; }; }; } diff --git a/packages/hm-cli.nix b/packages/hm-cli.nix new file mode 100644 index 0000000..715a1ad --- /dev/null +++ b/packages/hm-cli.nix @@ -0,0 +1,101 @@ +{ + pkgs ? import { }, +}: + +pkgs.writeShellScriptBin "hm" '' + set -e + + HM="${pkgs.lib.getExe pkgs.home-manager}" + FLAKE_PATH="''${HM_PATH:-$HOME/.config/home-manager}" + FLAKE_OUTPUT="''${HM_USER:-$(whoami)@$(hostname)}" + + show_usage() { + cat < [args] + + Commands: + apply Switch to a new generation + generation list List all generations + generation delete ID... Delete specified generation(s) + generation rollback Rollback to the previous generation + generation switch ID Switch to the specified generation + generation cleanup Delete all but the current generation + + Environment Variables: + HM_PATH Override default flake path (~/.config/home-manager) + Currently set to "$(echo $HM_PATH)" + HM_USER Override default user output ("$(whoami)@$(hostname)") + Currently set to "$(echo $HM_USER)" + EOF + } + + if [[ $# -eq 0 ]]; then + show_usage + exit 1 + fi + + case "$1" in + apply) + "$HM" switch --flake "$FLAKE_PATH#$FLAKE_OUTPUT" + ;; + generation) + if [[ $# -lt 2 ]]; then + echo "Error: generation command requires a subcommand" + show_usage + exit 1 + fi + + case "$2" in + list) + "$HM" generations + ;; + delete) + if [[ $# -lt 3 ]]; then + echo "Error: delete requires at least one generation ID" + exit 1 + fi + shift 2 + "$HM" remove-generations "$@" + ;; + rollback) + "$HM" generations | \ + sed -n 's/^[[:space:]]*id \([0-9]\+\).*/\1/p' | \ + head -n 2 | tail -n 1 | \ + xargs -I {} "$HM" switch --flake "$FLAKE_PATH" --switch-generation {} + ;; + switch) + if [[ $# -ne 3 ]]; then + echo "Error: switch requires exactly one generation ID" + exit 1 + fi + "$HM" switch --flake "$FLAKE_PATH" --switch-generation "$3" + ;; + cleanup) + CURRENT_GEN=$("$HM" generations | sed -n 's/^[[:space:]]*id \([0-9]\+\) (current).*/\1/p') + if [[ -z "$CURRENT_GEN" ]]; then + echo "Error: could not determine current generation" + exit 1 + fi + OLD_GENS=$("$HM" generations | sed -n 's/^[[:space:]]*id \([0-9]\+\).*/\1/p' | grep -v "^$CURRENT_GEN$") + if [[ -z "$OLD_GENS" ]]; then + echo "No old generations to delete" + else + echo "Deleting generations: $(echo $OLD_GENS | tr '\n' ' ')" + echo "$OLD_GENS" | xargs "$HM" remove-generations + echo "Cleanup complete. Current generation $CURRENT_GEN preserved." + fi + ;; + *) + echo "Error: unknown generation subcommand '$2'" + show_usage + exit 1 + ;; + esac + ;; + *) + echo "Error: unknown command '$1'" + show_usage + exit 1 + ;; + esac +'' diff --git a/users/modules/common/hm-cli.nix b/users/modules/common/hm-cli.nix new file mode 100644 index 0000000..d2ed715 --- /dev/null +++ b/users/modules/common/hm-cli.nix @@ -0,0 +1,10 @@ +{ pkgs, ... }: + +{ + home = { + packages = with pkgs; [ hm-cli ]; + sessionVariables = { + HM_PATH = "/etc/nixos"; + }; + }; +} diff --git a/utils.nix b/utils.nix index 23f92d0..1f30542 100644 --- a/utils.nix +++ b/utils.nix @@ -166,6 +166,10 @@ in inherit inputs; userTags = allTags; }; - modules = allModules; + modules = allModules ++ [ + { + nixpkgs.overlays = [ self.overlays.default ]; + } + ]; }; }