.hive/modules/bin/nix-scripts.nix

119 lines
3.9 KiB
Nix

{
config,
lib,
pkgs,
isHM,
...
}: let
cfg = config.hive.nix-scripts;
home-rebuild =
pkgs.writeShellScriptBin ".home-rebuild"
''
set -e
pushd ~/.hive/
${pkgs.alejandra}/bin/alejandra . &>/dev/null
${pkgs.git}/bin/git diff -U0
echo "NixOS Rebuilding..."
home-manager switch --flake ~/.hive -b backup --log-format internal-json |& ${pkgs.nix-output-monitor}/bin/nom --json
gen=$(home-manager generations | head -n1 | ${pkgs.gawk}/bin/awk '{print "Gen" $5 " @ " $1 "-" $2}')
by="$(${pkgs.coreutils-full}/bin/whoami)@$(${pkgs.nettools}/bin/hostname)"
${pkgs.git}/bin/git commit --no-gpg-sign -am "Home $gen by $by"
popd
'';
rebuild =
pkgs.writeShellScriptBin ".nixos-rebuild"
''
set -e
pushd ~/.hive/
${pkgs.alejandra}/bin/alejandra . &>/dev/null
${pkgs.git}/bin/git diff -U0
echo "NixOS Rebuilding..."
${pkgs.nh}/bin/nh os switch ~/.hive
gen=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | ${pkgs.gnugrep}/bin/grep current | ${pkgs.gawk}/bin/awk '{print "Gen" $1 " @ " $2 "-" $3}')
by="$(${pkgs.coreutils-full}/bin/whoami)@$(${pkgs.nettools}/bin/hostname)"
${pkgs.git}/bin/git commit --no-gpg-sign -am "System $gen by $by"
popd
'';
upgrade =
pkgs.writeShellScriptBin ".nixos-upgrade"
''
set -e
pushd ~/.hive/
if [ -n "$(${pkgs.git}/bin/git status --porcelain)" ]; then
echo ".hive is unclean!"
exit 1
fi
branch_staging="staging-update"
if ${pkgs.git}/bin/git rev-parse --verify "$branch_staging" >/dev/null 2>&1; then
echo "Using staging update branch."
else
echo "No staging update branch found."
exit 1
fi
${pkgs.git}/bin/git checkout "$branch_staging" flake.lock
echo "Updating nix-flake..."
nix flake update --flake .
echo "NixOS Rebuilding..."
${pkgs.nh}/bin/nh os switch ~/.hive
gen=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | ${pkgs.gnugrep}/bin/grep current | ${pkgs.gawk}/bin/awk '{print "Gen" $1 " @ " $2 "-" $3}')
by="$(${pkgs.coreutils-full}/bin/whoami)@$(${pkgs.nettools}/bin/hostname)"
${pkgs.git}/bin/git commit --no-gpg-sign -am "Upgrade $gen by $by"
${pkgs.git}/bin/git branch -D "$branch_staging"
popd
'';
update =
pkgs.writeShellScriptBin ".nixos-update"
''
set -e
pushd ~/.hive/
if [ -n "$(${pkgs.git}/bin/git status --porcelain)" ]; then
echo ".hive is unclean!"
exit 1
fi
branch_staging="staging-update"
branch_current="$(${pkgs.git}/bin/git branch --show-current)"
if ${pkgs.git}/bin/git rev-parse --verify "$branch_staging" >/dev/null 2>&1; then
echo "There is already a staging update branch."
else
echo "Creating a new staging update branch."
${pkgs.git}/bin/git switch -c "$branch_staging"
nix flake update --verbose --flake .
${pkgs.git}/bin/git add flake.lock
${pkgs.git}/bin/git commit --no-gpg-sign -m "staging update"
${pkgs.git}/bin/git switch "$branch_current"
fi
nix store --log-format internal-json -v diff-closures \
'.?ref='"$branch_current"'#nixosConfigurations.'"$(${pkgs.hostname}/bin/hostname)"'.config.system.build.toplevel' \
'.?ref='"$branch_staging"'#nixosConfigurations.'"$(${pkgs.hostname}/bin/hostname)"'.config.system.build.toplevel' \
|& ${pkgs.nix-output-monitor}/bin/nom --json
popd
'';
in {
options = {
hive.nix-scripts.enable = lib.mkEnableOption "Enable the nix build/update scripts";
};
config =
lib.mkIf cfg.enable
(
if isHM
then {
home.packages = [
home-rebuild
];
}
else {
environment.systemPackages = [
rebuild
upgrade
update
];
}
);
}