I have a project that will create some bindings for a library called testProject
here.
Running nix develop
is fine.
Running nix build
is also fine.
but when trying pip install .
I get errors about missing dependencies.
I thought maybe creating a new development shell with a virtual environment that I start like this nix develop .#pyenv
and installing manually my dependencies with be a good idea.
But is this the best way of doing things?
Or is there a better way to use the dependencies that I already got with nix, to be used when I try to also build my python project?
dependencies.sh
if [[ -n "${VIRTUAL_ENV}" ]]; then
INSTALL_PREFIX="${VIRTUAL_ENV}"
elif [[ -n "${CONDA_PREFIX}" ]]; then
INSTALL_PREFIX="${CONDA_PREFIX}"
else
INSTALL_PREFIX=$(python -c "import sysconfig; print(sysconfig.get_config_var('prefix'))")
fi
if [[ -z "${INSTALL_PREFIX}" ]]; then
echo "Error: could not determine install prefix."
echo $INSTALL_PREFIX
exit
fi
set -e
PLATFORM=linux
if [[ "$OSTYPE" == "darwin"* ]]; then
PLATFORM=osx
fi
# openlibm
git clone https://github.com/JuliaMath/openlibm.git
pushd openlibm
git checkout c4667caea25ae3487adf6760b4a1dcf32477a4b8
mkdir build
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_INSTALL_LIBDIR=lib \
-DCMAKE_INSTALL_INCLUDEDIR=include \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-DCMAKE_PREFIX_PATH=${INSTALL_PREFIX} \
-DOPENLIBM_ENABLE_TESTS=OFF
cmake --build build -j
cmake --install build
popd
rm -rf openlibm
...
flakes.nix
{
description = "My development environment";
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
mypackages.url = "github:mypackages/nur-packages";
nixpkgs.url = "github:nixos/nixpkgs/master";
testProject.url = "github:mypackages/test-project";
mypackages.inputs.nixpkgs.follows = "nixpkgs";
testProject.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs@{ self, flake-parts, nixpkgs, mypackages, testProject }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ "x86_64-linux" "aarch64-darwin" ];
perSystem = { pkgs, system, ... }:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
mypackages.overlay
(final: prev: {
})
];
};
enableShared = false;
stdenv_ = pkgs.llvmPackages_18.stdenv;
python_ = pkgs.python312;
venvDir = ".venv";
testProject_ = if enableShared then testProject.packages.${system}.library else testProject.packages.${system}.library-static;
pyforge = stdenv_.mkDerivation {
name = "pyforge";
src = self;
cmakeFlags = [
"--preset ${if pkgs.stdenv.hostPlatform.isx86_64 then "build-linux" else "build-osx"}"
];
cmakeBuildType = "Debug";
dontStrip = true;
nativeBuildInputs = with pkgs; [
clang-tools_18
cmake
ninja
pkg-config
python_
python_.pkgs.pybind11
];
buildInputs = with pkgs; [
(python_.withPackages (ps: with ps; [ setuptools wheel requests ]))
testProject_
] ++ testProject_.buildInputs;
propagatedBuildInputs = with pkgs.python312Packages; [
scikit-build
pybind11
# Add other Python dependencies here
];
};
in
rec {
packages = {
default = pyforge;
pyforge-generic = pyforge.overrideAttrs (old: {
cmakeFlags = [
"-DCMAKE_BUILD_TYPE=Release"
"-DCMAKE_CXX_FLAGS=${
if pkgs.hostPlatform.isx86_64 then "-march=x86-64" else ""
}"
];
});
pyforge-debug = pyforge.overrideAttrs
(old: { cmakeBuildType = "Debug"; });
};
devShells.default = stdenv_.mkDerivation {
name = "pyforge-dev";
nativeBuildInputs = pyforge.nativeBuildInputs;
buildInputs = pyforge.buildInputs;
shellHook = ''
export SHELL=/run/current-system/sw/bin/bash
# Virtual environment setup
if [ ! -d "${venvDir}" ]; then
echo "Creating virtual environment in ${venvDir}"
${python_.interpreter} -m venv ${venvDir}
fi
source ${venvDir}/bin/activate
# Allow pip to install wheels
unset SOURCE_DATE_EPOCH
# Install development dependencies if requirements-dev.txt exists
if [ -f requirements-dev.txt ]; then
pip install -r requirements-dev.txt
fi
'';
};
devShells.pyenv = stdenv_.mkDerivation {
name = "pyforge-dev";
nativeBuildInputs = pyforge.nativeBuildInputs;
buildInputs = pyforge.buildInputs
++ (with pkgs; [ gdb valgrind gcc13 ])
++ (with python_.pkgs; [ scikit-build ]) # cmake integration and release preparation
++ (with python_.pkgs; [ numpy scikit-learn pandas pyarrow ipdb sympy requests matplotlib optuna jax jaxlib-bin torch ])
++ (with pkgs; [ (pmlb.override { pythonPackages = pkgs.python312Packages; }) ]);
shellHook = ''
export SHELL=/run/current-system/sw/bin/bash
# Virtual environment setup
if [ ! -d "${venvDir}" ]; then
echo "Creating virtual environment in ${venvDir}"
${python_.interpreter} -m venv ${venvDir}
fi
source ${venvDir}/bin/activate
# Allow pip to install wheels
unset SOURCE_DATE_EPOCH
# Install all Python dependencies via pip if needed
if [ -f requirements.txt ]; then
pip install -r requirements.txt
fi
# Install development dependencies if they exist
if [ -f requirements-dev.txt ]; then
pip install -r requirements-dev.txt
fi
'';
};
};
};
}