nix-flake-hf-space / flake.nix
osbm's picture
push
a6c2dde
raw
history blame
3.02 kB
{
description = "Huggingface space flake";
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs }:
let
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
genPkgs = forAllSystems (system: nixpkgs.legacyPackages.${system});
# Extract values from README.md front matter
readmeContent = builtins.readFile ./README.md;
lines = builtins.split "\n" readmeContent;
# Helper function to extract YAML front matter values
extractYamlValue = key:
let
matchingLines = builtins.filter (line:
builtins.isString line && builtins.match "${key}: .*" line != null
) lines;
in
if matchingLines != [] then
let match = builtins.match "${key}: (.*)" (builtins.head matchingLines);
in if match != null then builtins.head match else null
else null;
sdk = extractYamlValue "sdk";
appPort = extractYamlValue "app_port";
in
{
packages = forAllSystems (system: let
pkgs = genPkgs.${system};
pythonEnv = pkgs.python3.withPackages (ps: with ps; [
gradio
]);
in {
default = pkgs.stdenv.mkDerivation {
pname = "gradio-app";
version = "0.1.0";
src = ./.;
buildInputs = with pkgs; [
pythonEnv
];
dontBuild = true;
installPhase = ''
mkdir -p $out/bin
cp ${./app.py} $out/bin/app.py
# Create bash script to run gradio with python
cat > $out/bin/run-gradio << 'EOF'
#!/usr/bin/env bash
set -euo pipefail
# Set default port if not provided
PORT=''${PORT:-${appPort}}
echo "Starting Gradio app on port $PORT..."
cd "$(dirname "$0")"
${pythonEnv}/bin/python app.py --server-port "$PORT" --server-name 0.0.0.0
EOF
chmod +x $out/bin/run-gradio
'';
meta.mainProgram = "run-gradio";
};
gradio-docker = pkgs.dockerTools.buildImage {
name = "gradio-app-image";
tag = "latest";
contents = [ self.packages.${system}.default ];
config = {
Cmd = [ "${self.packages.${system}.default}/bin/run-gradio" ];
ExposedPorts = {
"${appPort}/tcp" = {};
};
Env = [
"PORT=${appPort}"
"GRADIO_SERVER_NAME=0.0.0.0"
];
};
};
});
# devShells = forAllSystems (system: let
# in {
# default = pkgs.${system}.mkShell {
# buildInputs = with pkgs.${system}; [
# (python3.withPackages (ps: with ps; [
# gradio
# ]))
# ];
# };
# });
};
}