diff --git a/.gitignore b/.gitignore index 4c49bd7..82af664 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +result diff --git a/flake.nix b/flake.nix index e3f52f3..e4b8448 100644 --- a/flake.nix +++ b/flake.nix @@ -45,90 +45,25 @@ forAllSystemsWithPkgs [] ({ system, pkgs }: { packages.${system} = { - # necessary to load every time .nvimrc - # makes some magic to shading nvim but still uses nvim that shaded - nvim-alias = pkgs.writeShellScriptBin "nvim" '' - # Source .env file - if [ -f .env ]; then - set -a - . .env - set +a - fi - - # Get the directory of this script - SCRIPT_DIR=$(dirname "$(readlink -f "$0")") - - # Remove the script's directory from PATH to avoid recursion - PATH=$(echo "$PATH" | tr ':' '\n' | grep -v "$SCRIPT_DIR" | paste -sd ':' -) - - # Find the system's nvim - SYSTEM_NVIM=$(command -v nvim) - - if [ -z "$SYSTEM_NVIM" ]; then - echo "Error: nvim not found in PATH" >&2 - exit 1 - fi - - # Execute the system's nvim with your custom arguments - exec "$SYSTEM_NVIM" --cmd 'lua vim.o.exrc = true' "$@" - ''; - printobstacle = - let - name = "printobstacle"; - in - pkgs.writeShellScriptBin "${name}" '' - printf "%s%s%s\n" "''${RED}" "$*" "''${RESET}" - ''; - printprogress = - let - name = "printprogress"; - in - pkgs.writeShellScriptBin "${name}" '' - printf "%s%s%s\n" "''${YELLOW}" "$*" "''${RESET}" - ''; - colorize = pkgs.writeShellScriptBin "colorize" '' - awk ' - BEGIN { - # Define color codes - RED = "\x1b[31m"; - BLUE = "\x1b[34m"; - GREEN = "\x1b[32m"; - YELLOW = "\x1b[33m"; - MAGENTA = "\x1b[35m"; - CYAN = "\x1b[36m"; - RESET = "\x1b[0m"; - } - { - # Apply color based on keywords - gsub(/ERROR:/, RED "&" RESET, $0); - gsub(/DEBUG:/, BLUE "&" RESET, $0); - gsub(/INFO:/, GREEN "&" RESET, $0); - gsub(/LOG:/, GREEN "&" RESET, $0); - gsub(/EXCEPTION:/, MAGENTA "&" RESET, $0); - gsub(/WARNING:/, YELLOW "&" RESET, $0); - gsub(/NOTICE:/, CYAN "&" RESET, $0); - gsub(/HINT:/, CYAN "&" RESET, $0); - gsub(/FATAL:/, MAGENTA "&" RESET, $0); - gsub(/DETAIL:/, CYAN "&" RESET, $0); - gsub(/STATEMENT:/, CYAN "&" RESET, $0); - print; - } - ' - ''; + nvim-alias = pkgs.callPackage ./package/nvim-alias.nix {}; + printobstacle = pkgs.callPackage ./package/printobstacle.nix {}; + printprogress = pkgs.callPackage ./package/printprogress.nix {}; + colorize = pkgs.callPackage ./package/colorize.nix {}; + gh_translabeles = pkgs.callPackage ./package/github/gh_translabeles.nix {}; }; nixosModules.${system} = { "hetzner.hardware" = { - boot.loader.grub.device = "/dev/sda"; - boot.initrd.availableKernelModules = [ - "ata_piix" - "uhci_hcd" - "xen_blkfront" - "vmw_pvscsi" - ]; - boot.initrd.kernelModules = [ "nvme" ]; - fileSystems."/" = { device = "/dev/sda1"; fsType = "ext4"; }; - }; + boot.loader.grub.device = "/dev/sda"; + boot.initrd.availableKernelModules = [ + "ata_piix" + "uhci_hcd" + "xen_blkfront" + "vmw_pvscsi" + ]; + boot.initrd.kernelModules = [ "nvme" ]; + fileSystems."/" = { device = "/dev/sda1"; fsType = "ext4"; }; + }; }; }) // { lib = { diff --git a/package/colorize.nix b/package/colorize.nix new file mode 100644 index 0000000..1546233 --- /dev/null +++ b/package/colorize.nix @@ -0,0 +1,31 @@ +{ pkgs, ... }: +pkgs.writeShellScriptBin "colorize" '' + awk ' + BEGIN { + # Define color codes + RED = "\x1b[31m"; + BLUE = "\x1b[34m"; + GREEN = "\x1b[32m"; + YELLOW = "\x1b[33m"; + MAGENTA = "\x1b[35m"; + CYAN = "\x1b[36m"; + RESET = "\x1b[0m"; + IGNORECASE = 1; + } + { + line = $0; + gsub(/(^|[^A-Za-z])ERROR:/, RED "&" RESET, line); + gsub(/(^|[^A-Za-z])DEBUG:/, BLUE "&" RESET, line); + gsub(/(^|[^A-Za-z])INFO:/, GREEN "&" RESET, line); + gsub(/(^|[^A-Za-z])LOG:/, GREEN "&" RESET, line); + gsub(/(^|[^A-Za-z])EXCEPTION:/, MAGENTA "&" RESET, line); + gsub(/(^|[^A-Za-z])WARNING:/, YELLOW "&" RESET, line); + gsub(/(^|[^A-Za-z])NOTICE:/, CYAN "&" RESET, line); + gsub(/(^|[^A-Za-z])HINT:/, CYAN "&" RESET, line); + gsub(/(^|[^A-Za-z])FATAL:/, MAGENTA "&" RESET, line); + gsub(/(^|[^A-Za-z])DETAIL:/, CYAN "&" RESET, line); + gsub(/(^|[^A-Za-z])STATEMENT:/, CYAN "&" RESET, line); + print line; + } + ' +'' diff --git a/package/github/gh_translabeles.nix b/package/github/gh_translabeles.nix new file mode 100644 index 0000000..5a1d0a9 --- /dev/null +++ b/package/github/gh_translabeles.nix @@ -0,0 +1,102 @@ +# FIXME: very unstable (on every request opens pager) but works somehow +{ pkgs, ... }: +pkgs.writeShellScriptBin "gh_translabeles" '' + set -euo pipefail + + # Function to display usage information + usage() { + echo "Usage: $0 [--force] " + echo "Options:" + echo " --force Replace existing labels in the target repository." + echo "Example:" + echo " $0 owner/source-repo owner/target-repo" + echo " $0 --force owner/source-repo owner/target-repo" + exit 1 + } + + # Initialize variables + FORCE=0 + + # Parse options + while [[ "$#" -gt 0 ]]; do + case "$1" in + --force) + FORCE=1 + shift + ;; + -h|--help) + usage + ;; + *) + break + ;; + esac + done + + # Check for required arguments + if [ "$#" -ne 2 ]; then + usage + fi + + SOURCE_REPO="$1" + TARGET_REPO="$2" + TEMP_FILE="$(mktemp)" + trap 'rm -f "$TEMP_FILE"' EXIT + + # Check if required commands are available + for cmd in ${pkgs.gh}/bin/gh ${pkgs.jq}/bin/jq; do + if ! command -v "$cmd" &>/dev/null; then + echo "Error: '$cmd' is not installed or not in PATH." + exit 1 + fi + done + + # Fetch all labels from the source repository with pagination + echo "Fetching labels from $SOURCE_REPO..." + + LABELS_JSON=$(${pkgs.gh}/bin/gh api -H "Accept: application/vnd.github.v3+json" \ + /repos/"$SOURCE_REPO"/labels --paginate | ${pkgs.jq}/bin/jq -s 'add') + + if [ -z "$LABELS_JSON" ] || [ "$LABELS_JSON" == "[]" ]; then + echo "No labels found or an error occurred." >&2 + exit 1 + fi + + echo "$LABELS_JSON" > "$TEMP_FILE" + + # Create or update labels in the target repository + echo "Processing labels for $TARGET_REPO..." + ${pkgs.jq}/bin/jq -c '.[]' "$TEMP_FILE" | while IFS= read -r label; do + name=$(echo "$label" | ${pkgs.jq}/bin/jq -r '.name') + encoded_name=$(echo "$name" | ${pkgs.jq}/bin/jq -s -R -r @uri) + color=$(echo "$label" | ${pkgs.jq}/bin/jq -r '.color') + description=$(echo "$label" | ${pkgs.jq}/bin/jq -r '.description // ""') + + if [ "$FORCE" -eq 1 ]; then + echo "Creating or updating label '$name' in $TARGET_REPO..." + if ! ${pkgs.gh}/bin/gh api -X PATCH -H "Accept: application/vnd.github.v3+json" \ + /repos/"$TARGET_REPO"/labels/"$encoded_name" \ + -f name="$name" -f color="$color" -f description="$description"; then + echo "Error: Failed to create/update label '$name'. Skipping." + else + echo "Label '$name' has been created/updated in $TARGET_REPO." + fi + else + if ${pkgs.gh}/bin/gh api -H "Accept: application/vnd.github.v3+json" \ + /repos/"$TARGET_REPO"/labels/"$encoded_name" &>/dev/null; then + echo "Label '$name' already exists in $TARGET_REPO. Skipping." + else + echo "Creating label '$name' in $TARGET_REPO..." + if ! ${pkgs.gh}/bin/gh api -X POST -H "Accept: application/vnd.github.v3+json" \ + /repos/"$TARGET_REPO"/labels \ + -f name="$name" -f color="$color" -f description="$description"; then + echo "Warning: Label '$name' already exists or failed to create. Skipping." + else + echo "Label '$name' has been created in $TARGET_REPO." + fi + fi + fi + done + + echo "Label transfer completed successfully." +'' diff --git a/package/nvim-alias.nix b/package/nvim-alias.nix new file mode 100644 index 0000000..68fb097 --- /dev/null +++ b/package/nvim-alias.nix @@ -0,0 +1,28 @@ +# necessary to load every time .nvimrc +# makes some magic to shading nvim but still uses nvim that shaded +{ pkgs, ... }: +pkgs.writeShellScriptBin "nvim" '' + # Source .env file + if [ -f .env ]; then + set -a + . .env + set +a + fi + + # Get the directory of this script + SCRIPT_DIR=$(dirname "$(readlink -f "$0")") + + # Remove the script's directory from PATH to avoid recursion + PATH=$(echo "$PATH" | tr ':' '\n' | grep -v "$SCRIPT_DIR" | paste -sd ':' -) + + # Find the system's nvim + SYSTEM_NVIM=$(command -v nvim) + + if [ -z "$SYSTEM_NVIM" ]; then + echo "Error: nvim not found in PATH" >&2 + exit 1 + fi + + # Execute the system's nvim with your custom arguments + exec "$SYSTEM_NVIM" --cmd 'lua vim.o.exrc = true' "$@" +'' diff --git a/package/printobstacle.nix b/package/printobstacle.nix new file mode 100644 index 0000000..31fca93 --- /dev/null +++ b/package/printobstacle.nix @@ -0,0 +1,7 @@ +{ pkgs, ... }: +let + name = "printobstacle"; +in +pkgs.writeShellScriptBin "${name}" '' + printf "%s%s%s\n" "''${RED}" "$*" "''${RESET}" +'' diff --git a/package/printprogress.nix b/package/printprogress.nix new file mode 100644 index 0000000..5398118 --- /dev/null +++ b/package/printprogress.nix @@ -0,0 +1,7 @@ +{ pkgs, ... }: +let + name = "printprogress"; +in +pkgs.writeShellScriptBin "${name}" '' + printf "%s%s%s\n" "''${YELLOW}" "$*" "''${RESET}" +''