feat!: remove nih helper

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-05-04 14:27:49 -03:00
parent f62a459f18
commit f22fcb4653
5 changed files with 2 additions and 358 deletions

View File

@@ -15,10 +15,6 @@
programs.nh.enable = true;
programs.nh.flake = "/home/guz/nix";
programs.nih.enable = true;
programs.nih.flakeDir = "/home/guz/nix";
programs.nih.host = "battleship";
profiles.locale.enable = true;
programs.hyprland.enable = true;

View File

@@ -12,9 +12,8 @@
./hardware-configuration.nix
];
programs.nih.enable = true;
programs.nih.flakeDir = "/home/guz/.nix";
programs.nih.host = "spacestation";
programs.nh.enable = true;
programs.nh.flake = "/home/guz/nix";
profiles.locale.enable = true;

View File

@@ -1,7 +1,6 @@
{...}: {
imports = [
./nh
./nih
./hyprland.nix
./steam.nix
];

View File

@@ -1,270 +0,0 @@
function util-show-diff() {
local prefix="$1";
gum log --structured --prefix "$prefix" --level debug 'Creatting diff files'
temp_file="$(mktemp /tmp/nih-diff-XXXXX)"
git diff -U0 '*.*' > "$temp_file"
echo "$(gum format -l diff -t code < "$temp_file")" > "$temp_file"
gum pager < "$temp_file"
rm "$temp_file"
}
function util-build() {
local prefix="$1";
local flake_dir="$2";
local host="$3";
set -e
pushd "$flake_dir" > /dev/null
for f in ./secrets/*.lesser.*; do
local filename="$(basename -- "$f")"
local extension="${filename##*.}"
local filename="${filename%.*}"
local subextenstion="${filename##*.}"
if [[ "$subextenstion" == "decrypted" ]]; then
gum log --structured --prefix "$prefix" --level warn 'File already decrypted!' file "$f"
else
gum log --structured --prefix "$prefix" --level debug 'Decrypting lesser secret file' file "$f"
sops --output "./secrets/$filename.decrypted.$extension" -d "$f"
fi
done
# Add secret files
gum log --structured --prefix "$prefix" --level debug 'Adding decrypted secret files'
git add ./secrets/*.decrypted.*
# Build NixOS
gum log --structured --prefix "$prefix" --level debug 'Building NixOS'
sudo nixos-rebuild switch --flake "$flake_dir#$host" \
|| (gum log --structured --prefix "$prefix" --level debug 'Removing decrypted secret files' \
&& git reset ./secrets/*.decrypted.* \
&& for f in ./secrets/*.decrypted.*; do rm "$f"; done \
&& gum log --structured --prefix "$prefix" --level error 'Error building new config' \
&& exit 1)
git reset ./secrets/*.decrypted.*
for f in ./secrets/*.decrypted.*; do
gum log --structured --prefix "$prefix" --level debug 'Removing decrypted secret file' file "$f"
rm "$f"
done
popd > /dev/null
}
function util-format() {
local prefix="$1"
local flake_dir="$2"
set -e
pushd "$flake_dir" > /dev/null
gum log --structured --prefix "$prefix" --level info 'Formatting files'
gum log --structured --prefix "$prefix" --level debug 'Formatting *.nix files'
alejandra . &>/dev/null \
|| (alejandra . ; \
gum log --structured \
--prefix "$prefix" \
--level error 'Failed to format files' \
&& exit 1)
gum log --structured --prefix "$prefix" --level debug 'Formatting *.sh files'
find "$flake_dir" -type f -name "*.sh" -execdir shellharden --replace {} \;
# find "$flake_dir" -type f -name "*.sh" -execdir shfmt {} \;
# gum log --structured --prefix "$prefix" --level debug 'Formatting *.{md,scss} files'
# find "$flake_dir" -type f \( -iname '*.md' -o -iname '*.scss' \) -execdir prettier -w {} \;
popd > /dev/null
}
function nih-edit() {
local flake_dir="$1"
local host="$2"
# Exit if a command exits with a non-zero value
set -e
# Push directory to history
pushd "$flake_dir" > /dev/null
# Edit file
"$EDITOR" "$(gum file "$flake_dir")"
# Skip if there's no changes
if git diff --quiet "*.*"; then
gum log --structured \
--prefix 'nih edit' \
--level warn \
'No files changed'
popd > /dev/null
exit 0
fi
util-format 'nih edit' "$flake_dir"
# Show modifications
util-show-diff 'nih edit'
# Build nixos
util-build 'nih edit' "$flake_dir" "$host"
gum log --structured \
--prefix 'nih edit' \
--level info 'NixOS finished building, please commit the changes'
notify-send -e "NixOS finished building, please commit the changes" \
--icon=software-update-available \
--urgency=normal
case "$(gum choose --limit 1 'Commit' 'Open lazygit' 'No commit')" in
"Commit")
commit_msg="$(gum write --prompt 'Commit message' --placeholder 'Commit message')"
git commit -am "$commit_msg"
gum confirm 'Push changes to remote?' \
&& git push \
|| echo "";
;;
"Open lazygit")
lazygit
gum confirm 'Push changes to remote?' \
&& git push \
|| echo "";
;;
*)
gum log --structured \
--prefix 'nih edit' \
--level info 'Not commiting'
;;
esac
gum log --structured --prefix 'nih edit' --level info 'NixOS rebuilt!'
notify-send -e "NixOS Rebuilt!" \
--icon=software-update-available \
--urgency=low
# Pop back to previous directory
popd > /dev/null
}
function nih-switch () {
local flake_dir="$1"
local host="$2"
set -e
pushd "$flake_dir" > /dev/null
gum log --structured --prefix 'nih switch' --level info 'Switching NixOS config'
util-format 'nih switch' "$flake_dir"
# Build nixos
util-build 'nih switch' "$flake_dir" "$host"
gum log --structured --prefix 'nih switch' --level info 'NixOS rebuilt!'
notify-send -e "NixOS Rebuilt!" \
--icon=software-update-available \
--urgency=low
popd > /dev/null
}
function nih-install() {
local pkgs=()
local index=0
for arg in "$@"; do
if [[ "$arg" == "--" ]]; then
index=$(($index + 1))
break
fi
pkgs+=("nixpkgs#$arg")
index=$(($index + 1))
done
shift "$index"
gum log --structured --prefix 'nih install' --level info "Temporaly installing to current shell:"
gum log --structured --prefix 'nih install' --level info "${pkgs[@]}"
nix shell "${pkgs[@]}" "$@"
gum log --structured --prefix 'nih install' --level info "Packages intalled!"
}
function nih-execute() {
local pkg="$1"
shift 1
nix run "nixpkgs#$pkg" "$@"
}
function nih-sync() {
local flake_dir="$1"
local host="$2"
set -e
pushd "$flake_dir" > /dev/null
gum log --structured --prefix 'nih sync' --level info 'Syncing NixOS config'
util-format 'nih sync' "$flake_dir"
git reset ./secrets/*.decrypted.*
for f in ./secrets/*.decrypted.*; do
gum log --structured --prefix "$prefix" --level debug 'Removing decrypted secret file' file "$f"
rm "$f"
done
# Skip if there's no changes
if git diff --quiet "*.*"; then
gum log --structured \
--prefix 'nih sync' \
--level warn \
'No files changed'
popd
exit 0
else
# Show modifications
util-show-diff 'nih sync'
commit_msg="$(gum write --prompt 'Commit message' --placeholder 'Commit message')"
git commit -am "$commit_msg"
gum log --structured --prefix 'nih sync' --level debug 'Pushing to remote'
git push
gum log --structured --prefix 'nih sync' --level info 'NixOS configuration synced!'
fi
popd > /dev/null
}
function nih-format() {
local flake_dir="$1"
gum log --structured --prefix "nih format" --level info 'Formatting NixOS config files'
util-format 'nix format' "$flake_dir"
gum log --structured --prefix "nih format" --level info 'NixOS config files formatted'
}
case "$1" in
"edit") nih-edit "$flake_dir" "$host" ;;
"switch" | "build") nih-switch "$flake_dir" "$host" ;;
"install" | "i" ) shift 1; nih-install "$@" ;;
"exec" | "x" ) shift 1; nih-execute "$@" ;;
"sync") nih-sync "$flake_dir" "$host" ;;
"format") nih-format "$flake_dir" ;;
"forgejo") shift 1; nih-forgejo "$@" ;;
"forgejo-act") shift 1; nih-forgejo-act "$@" ;;
*) gum log --structured --prefix 'nih' --level error "Command $1 does not exist" ;;
esac
echo "";

View File

@@ -1,80 +0,0 @@
{
config,
lib,
pkgs,
...
}: let
cfg = config.programs.nih;
cli = pkgs.writeShellScriptBin "nih" ''
# Since alias= doesn't work in bash scripts
function alejandra() { ${pkgs.alejandra}/bin/alejandra "$@"; }
function git() { ${pkgs.git}/bin/git "$@"; }
function gum() { ${pkgs.gum}/bin/gum "$@"; }
function lazygit() { ${pkgs.lazygit}/bin/lazygit "$@"; }
function notify-send() {
(${pkgs.libnotify}/bin/notify-send "$@" &>/dev/null || echo "")
}
function mktemp() { ${pkgs.mktemp}/bin/mktemp "$@"; }
# function prettier() { ${pkgs.nodePackages.prettier}/lib/node_modules/.bin/prettier ; }
function shellharden() { ${pkgs.shellharden}/bin/shellharden "$@"; }
# function shfmt() { ${pkgs.shfmt}/bin/shfmt "$@"; }
function sops() { ${pkgs.sops}/bin/sops "$@"; }
flake_dir="${toString cfg.flakeDir}";
host="${toString cfg.host}";
function nih-forgejo() {
${
if config.services.forgejo.actions.enable
then ''
sudo --user=${config.services.forgejo.user} \
${config.services.forgejo.package}/bin/gitea \
--work-path ${config.services.forgejo.stateDir} \
"$@"
''
else ''
gum log --structured --prefix 'nih' --level error "Forgejo action runnser service is not enabled"
''
}
}
function nih-forgejo-act() {
${
if config.services.forgejo.enable
then ''
sudo --user=${config.services.forgejo.user} \
${config.services.gitea-actions-runner.package}/bin/act_runner \
--config /var/lib/gitea-runner/forgejo${toString config.services.forgejo.settings.server.HTTP_PORT} \
"$@"
''
else ''
gum log --structured --prefix 'nih' --level error "Forgejo service is not enabled"
''
}
}
${builtins.readFile ./cli.sh}
'';
in {
imports = [];
options.programs.nih = with lib;
with lib.types; {
enable = mkEnableOption "";
host = mkOption {
type = str;
};
flakeDir = mkOption {
type = str;
};
cli = mkOption {
type = bool;
default = cfg.enable;
};
};
config = with lib;
mkIf cfg.enable {
environment.systemPackages = [
cli
];
};
}