From e3c65cfe55e20cbe92368410324fd5a21562f00c Mon Sep 17 00:00:00 2001 From: pagedmov Date: Thu, 7 Nov 2024 14:29:46 -0500 Subject: [PATCH] added fetchfromgh, which takes a github username and repo like someuser/somerepo and returns a pkgs.fetchFromGitHub call --- modules/home/scripts/default.nix | 4 +++ modules/home/scripts/nix/fetchfromgh.nix | 38 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 modules/home/scripts/nix/fetchfromgh.nix diff --git a/modules/home/scripts/default.nix b/modules/home/scripts/default.nix index 13330c8..39134df 100755 --- a/modules/home/scripts/default.nix +++ b/modules/home/scripts/default.nix @@ -6,6 +6,7 @@ pkgs, ... }: let + fetchfromgh = import ./nix/fetchfromgh.nix { inherit pkgs; }; vipkg = import ./commands/vipkg.nix { inherit pkgs; }; keyring = import ./wm-controls/keyring.nix { inherit pkgs; }; invoke = import ./commands/invoke.nix { inherit pkgs; }; @@ -77,6 +78,8 @@ in { # Nix Shortcuts + movOpts.movScripts.nixShortcuts.fetchfromgh.enable = + scriptOverride "Provides a full pkgs.fetchFromGitHub call from a repository url" "nixShortcuts" "fetchfromgh"; movOpts.movScripts.nixShortcuts.garbage-collect.enable = scriptOverride "Enables the garbage-collect script" "nixShortcuts" "garbage-collect"; movOpts.movScripts.nixShortcuts.nsp.enable = @@ -106,6 +109,7 @@ in { ++ lib.optionals config.movOpts.movScripts.hyprlandControls.mkscreenshots.enable [ mkscreenshots ] # Nix Shortcuts Overrides + ++ lib.optionals config.movOpts.movScripts.nixShortcuts.fetchfromgh.enable [ fetchfromgh ] ++ lib.optionals config.movOpts.movScripts.nixShortcuts.garbage-collect.enable [ garbage-collect ] ++ lib.optionals config.movOpts.movScripts.nixShortcuts.nsp.enable [ nsp ] ++ lib.optionals config.movOpts.movScripts.nixShortcuts.rebuild.enable [ rebuild ]; diff --git a/modules/home/scripts/nix/fetchfromgh.nix b/modules/home/scripts/nix/fetchfromgh.nix new file mode 100644 index 0000000..82d0550 --- /dev/null +++ b/modules/home/scripts/nix/fetchfromgh.nix @@ -0,0 +1,38 @@ +{ pkgs ? import { } }: + +pkgs.writeShellApplication { + name = "fetchfromgh"; + runtimeInputs = [ + pkgs.nix-prefetch-scripts + ]; + text = '' + if [ $# -ne 1 ] || ! echo "$1" | grep -qE "[A-Za-z0-9_-]+/[A-Za-z0-9_-]+"; then + echo "Usage: fetchfromgh someuser/somerepo" + echo " - i.e. fetchfromgh pagedMov/nixos-config" + exit 1 + fi + + if ! curl -s -o /dev/null -w "%{http_code}" "https://github.com/$1" | grep -q "200"; then + echo "Couldn't find that repository, curl returned 404." + exit 1 + fi + + json=$(nix-prefetch-git --quiet "https://github.com/$1") + + url=$(echo "$json" | jq '.url' | tr -d '"') + owner=$(echo "$url" | awk -F'/' '{print $(NF-1)}') + repo=$(echo "$url" | awk -F'/' '{print $NF}') + rev=$(echo "$json" | jq '.rev' | tr -d '"') + hash=$(echo "$json" | jq '.hash' | tr -d '"') + + output="\ + src = pkgs.fetchFromGitHub { + owner = \"$owner\"; + repo = \"$repo\"; + rev = \"$rev\"; + hash = \"$hash\"; + }; + " + echo "$output" + ''; +}