# Guide to a Slightly Non-Declarative Python Experience on NixOS
Hey, I'm Yehor, a machine learning engineer and a big NixOS fan. This article is a tutorial for you folks having troubles with Python development on NixOS.
## Problem
Most common NixOS approaches to Python development come with the following issues:
- **C-based libraries** (e.g. numpy or qt) often require manually adding paths. You may have seen something like this:
{ pkgs ? (import <nixpkgs> {}).pkgs }:
with pkgs;
mkShell {
buildInputs = [
python3Packages.python
python3Packages.virtualenv
];
shellHook = ''
# ImportError: libstdc++.so.6: cannot open shared object file: No such file or directory
# Fix:
export LD_LIBRARY_PATH=${stdenv.cc.cc.lib}/lib/
'';
}- **Devenv** setups don’t have this issue, but if you’re the lucky owner of an NVIDIA GPU, you won’t get access to it for running your juicy ML models. You also can’t install packages with pip or Poetry using this approach.
{ pkgs, ... }:
{
languages.python = {
enable = true;
venv.enable = true;
venv.requirements = ''
numpy
pandas
scipy
scikit-learn
tensorflow[and-cuda]
ngboost
crps
shap
optuna
xarray
pyarrow
xskillscore
matplotlib
plotly
seaborn
jupyter
ipykernel
jupyterlab
jupyterlab-vim
jupytext
'';
};
}* Due to my severe skill-issues, I have never managed to create a usable setup with **Poetry2Nix**. So cannot say anything about that side of things.
## Solution
Someone on Reddit mentioned using `conda-shell`, and that was a real lifesaver at the time, 'cause work had to get done.
Just add the `conda` package to your `configuration.nix` system packages:
environment.systemPackages = with pkgs; [
...
conda
];And you are good to go!
After rebuilding your system, you can enter a Conda shell with:
conda-shellNow you’re free to manage your Python environment with Conda!
conda create -n my-env python=3.10
conda activate my-envInstall packages with `pip install` or `conda install`. Or, if you like, `pip install poetry` and manage dependencies with Poetry.
## Bonus: my tmux setup
Here’s the script I use to set up my environment within a tmux session:
# start.sh
CONDA_NAME="my-env"
python () {
tmux send-keys 'conda activate '$CONDA_NAME C-m
tmux send-keys 'export PYTHONPATH=$(pwd)' C-m
tmux send-keys 'clear' C-m
}
# ----------- 1 Window -----------
# Bottom Pane
tmux split-window -v 'conda-shell'
python
# Top Right Pane
tmux select-pane -U
tmux split-window -h 'btop'
# Top Left Pane
tmux select-pane -L
tmux send-keys 'conda-shell' C-m
python
# Move to Bottom Pane
tmux select-pane -D
# ----------- 2 Window -----------
tmux new-window 'conda-shell'
python
tmux send-keys 'nvim .' C-m