From 3814d6175f5543cdc50470cff6f4fe242c308086 Mon Sep 17 00:00:00 2001 From: NullBite Date: Sat, 23 Mar 2024 16:13:03 -0400 Subject: [PATCH] Add packwiz-wrapper package this searches up the directory tree for a pack.toml before executing packwiz --- flake.nix | 2 +- nix/packages/default.nix | 1 + nix/packages/packwiz-wrapper/default.nix | 25 ++++++++++++++ .../packwiz-wrapper/packwiz-wrapper.py | 34 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 nix/packages/packwiz-wrapper/default.nix create mode 100755 nix/packages/packwiz-wrapper/packwiz-wrapper.py diff --git a/flake.nix b/flake.nix index 919e9d8..3e18ac1 100644 --- a/flake.nix +++ b/flake.nix @@ -7,7 +7,7 @@ devShells = eachSystem (system: let pkgs = import nixpkgs { inherit system; }; in { default = pkgs.mkShell { - buildInputs = with pkgs; [ packwiz nix-update ]; + buildInputs = with pkgs; [ self.packages.${system}.packwiz-wrapper nix-update ]; }; }); packages = eachSystem (system: let pkgs = import nixpkgs { inherit system; }; diff --git a/nix/packages/default.nix b/nix/packages/default.nix index a61c610..4f69f6d 100644 --- a/nix/packages/default.nix +++ b/nix/packages/default.nix @@ -3,4 +3,5 @@ let shaders = callPackage ./shaders.nix {}; in { inherit (shaders) bliss-dh complementary-reimagined-dh rethinking-voxels-dh; + packwiz-wrapper = callPackage ./packwiz-wrapper { }; } diff --git a/nix/packages/packwiz-wrapper/default.nix b/nix/packages/packwiz-wrapper/default.nix new file mode 100644 index 0000000..e9fd3c9 --- /dev/null +++ b/nix/packages/packwiz-wrapper/default.nix @@ -0,0 +1,25 @@ +{ lib, + stdenvNoCC, + python3, + makeWrapper, + packwiz }: +let + wrappedPath = lib.makeBinPath [ packwiz ]; +in stdenvNoCC.mkDerivation { + pname = "packwiz-wrapper"; + version = "0.0.0"; + buildInputs = [ + python3 + makeWrapper + packwiz + ]; + + src = ./.; + + installPhase = '' + install -Dm555 packwiz-wrapper.py $out/libexec/packwiz-wrapper.py + mkdir -p $out/bin + makeShellWrapper $out/libexec/packwiz-wrapper.py $out/bin/packwiz \ + --prefix PATH : "${wrappedPath}" + ''; +} diff --git a/nix/packages/packwiz-wrapper/packwiz-wrapper.py b/nix/packages/packwiz-wrapper/packwiz-wrapper.py new file mode 100755 index 0000000..84cf194 --- /dev/null +++ b/nix/packages/packwiz-wrapper/packwiz-wrapper.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +import sys, os +from pathlib import Path + +# Returns a parent path with pack.toml, or None if it can't be found +def search_recursive(dir: str | os.PathLike) -> Path | None: + dir = Path(dir) + pack = dir / "pack.toml" + + if pack.is_file(): + return dir + + # If we've made it to / without finding it, return None + if dir.samefile("/"): + return None + + # Otherwise, recurse + return search_recursive(dir / "..") + +def search(dir: str | os.PathLike) -> Path: + new_dir = search_recursive(dir) + if new_dir is not None: + return new_dir + else: + return Path(dir) + +def main(): + args = ["packwiz"] + sys.argv[1:] + os.chdir(search(".")) + os.execvp("packwiz", args) + + +if __name__ == "__main__": + main()