{
  description = "Flake using pyproject.toml metadata";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    pyproject-nix = {
      url = "github:pyproject-nix/pyproject.nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = {
    self,
    nixpkgs,
    flake-utils,
    ...
  }@inputs: let 
    system = "x86_64-linux"; 
    pkgs = nixpkgs.legacyPackages.${system};
    project = inputs.pyproject-nix.lib.project.loadPyproject {
      projectRoot = ./.;
    };
    projectName = project.pyproject.project.name;
    python = pkgs.python3;

    inswapperModel = pkgs.stdenv.mkDerivation { 
      name = "inswapper_128";
      src = pkgs.fetchurl {
        url = "https://huggingface.co/thebiglaskowski/inswapper_128.onnx/resolve/main/inswapper_128.onnx";
        hash = "sha256-5KPwjHU8ty0E4Qqg99vj3uu/OVZ9Tq1tzgjpiqSeFq8=";
      };

      phases = [ "installPhase" ];

      installPhase = ''
        mkdir -p "$out/var/lib/insightface/models/"
        cp $src "$out/var/lib/insightface/models/inswapper_128.onnx"
      '';
    };
    buffaloModel = pkgs.stdenv.mkDerivation { 
      name = "buffalo_l_Model";
      src = pkgs.fetchurl {
        url = "https://github.com/deepinsight/insightface/releases/download/v0.7/buffalo_l.zip";
        hash = "sha256-gP/jfYpZQNWac4TCAaKjjUdB8vPFHu9G67KCGKewyi8=";
      };

      unpackPhase = /*shell*/''
        ls -l $src
        echo $src

        mkdir -p "$out/var/lib/insightface/models/"
        unar -D -o $out/var/lib/insightface/models/buffalo_l/ $src
      '';

      nativeBuildInputs = [ pkgs.unar ];
    };
    postInstall = /*shell*/''
      mkdir -p $out/var/lib/insightface/models/buffalo_l
      cp ${inswapperModel} $out/var/lib/insightface/models/inswapper_128.onnx
      '';
      # ${pkgs.unar}/bin/unar -D -o $out/var/lib/insightface/models/buffalo_l ${buffaloModel}
    # '';
    packageAttrs = project.renderers.buildPythonPackage { 
      inherit python; 
    };
    packageAttrsLive = project.renderers.mkPythonEditablePackage { 
      inherit python; 
    };
    protestswap = python.pkgs.buildPythonPackage ( packageAttrs # // { inherit postInstall; } 
    );
  in {
    devShells.${system}.default = let 
      arg = project.renderers.mkPythonEditablePackage { inherit python; };
      pythonEnv = python.pkgs.mkPythonEditablePackage ( arg );
    in pkgs.mkShell {
      packages = with pkgs; [ 
        python3 
        uv 
        gcc14 
        python3Packages.fastapi
      ] ++ [protestswap];
      shellHook = /*shell*/
        ''
        ls -l ${buffaloModel}

        INSIGHTFACE_ROOT_DIR=$(mktemp -d /tmp/insightface.XXXXXXXX)
        export INSIGHTFACE_ROOT_DIR

        mkdir -p "$INSIGHTFACE_ROOT_DIR/models/"

        ln -s ${inswapperModel}/var/lib/insightface/models/inswapper_128.onnx "$INSIGHTFACE_ROOT_DIR/models/"
        ln -s ${buffaloModel}/var/lib/insightface/models/buffalo_l "$INSIGHTFACE_ROOT_DIR/models/buffalo_l"

        # export PROTESTSWAP_ROOT="/tmp/protestswap_models"
        # mkdir -p "$PROTESTSWAP_ROOT/buffalo_l"
        #
        # cp ${inswapperModel} "$PROTESTSWAP_ROOT/inswapper_128.onnx"
        unset PYTHONPATH
        uv sync
        . .venv/bin/activate
      '';
    };

    packages.${system}.default = protestswap;
  };
}
