nix flake complete, added alternative data storage option

This commit is contained in:
TheK0tYaRa 2025-12-11 01:56:20 +02:00
parent c583ad191d
commit 1f72e01273

View file

@ -1,5 +1,5 @@
{
description = "Install main.py to data directory with dependencies";
description = "py2fa-gtk packaged the Nix way";
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
@ -7,11 +7,8 @@
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
packageName = "py2fa-gtk";
scriptSource = ./src/main.py;
# 1. Define Python environment
python = pkgs.python312.withPackages (ps: with ps; [
pythonEnv = pkgs.python312.withPackages (ps: with ps; [
cryptography
opencv-python
pygobject3
@ -19,52 +16,51 @@
pyzbar
]);
# 2. Define libraries for ZBar (shared object)
zbarLibs = [ pkgs.zbar ];
runtimeLibs = with pkgs; [
gtk3
zbar
gobject-introspection
];
in
{
packages.${system}.default = pkgs.stdenv.mkDerivation {
name = packageName;
name = "py2fa-gtk";
nativeBuildInputs = [ pkgs.wrapGAppsHook3 ];
src = ./src;
buildInputs = with pkgs; [
gtk3
pango
gdk-pixbuf
atk
cairo
gobject-introspection
# Python env included here so the wrapper knows about it
python
] ++ zbarLibs;
nativeBuildInputs = [
pkgs.wrapGAppsHook3
pkgs.makeWrapper
];
dontUnpack = true;
buildInputs = runtimeLibs ++ [ pythonEnv ];
dontBuild = true;
installPhase = ''
mkdir -p $out/bin
runHook preInstall
cat > $out/bin/${packageName} <<EOF
#!/bin/sh
export LD_LIBRARY_PATH="${pkgs.lib.makeLibraryPath zbarLibs}:\$LD_LIBRARY_PATH"
# A. Create standard directory structure in the Nix Store
mkdir -p $out/bin $out/share/py2fa-gtk
TARGET_DIR="\$HOME/.local/share/${packageName}"
TARGET_FILE="\$TARGET_DIR/main.py"
# B. Copy the python script to the store (Read-Only)
cp main.py $out/share/py2fa-gtk/main.py
mkdir -p "\$TARGET_DIR"
# C. Create the binary executable
# We wrap the python executable.
# 1. We tell it to run our specific script.
# 2. We inject the ZBar library path so pyzbar works.
makeWrapper ${pythonEnv}/bin/python3 $out/bin/py2fa-gtk \
--add-flags "$out/share/py2fa-gtk/main.py" \
--prefix LD_LIBRARY_PATH : "${pkgs.lib.makeLibraryPath [ pkgs.zbar ]}"
if [ ! -f "\$TARGET_FILE" ]; then
echo "Installing main.py to \$TARGET_FILE..."
cp "${scriptSource}" "\$TARGET_FILE"
chmod +w "\$TARGET_FILE"
fi
exec ${python}/bin/python3 "\$TARGET_FILE" "\$@"
EOF
chmod +x $out/bin/${packageName}
runHook postInstall
'';
# wrapGAppsHook runs automatically after installPhase.
# It will see the binary in $out/bin/py2fa-gtk and double-wrap it
# to inject XDG_DATA_DIRS and GI_TYPELIB_PATH (fixing Pango/GTK).
};
};
}