nix: hack for python in nixos
August 20, 2025
Using python on NixOS can be quite frustrating. While this page presents many options, it doesn’t provide a definitive recommendation on which approach to use.
Since I use python mostly for scripting, I’ve avoid using nix for python. Instead, I leverage NixOS’s buildFHSEnv to create a sandboxed environment where I can use traditional python tooling.
Here’s a home-manager module that sets up a python environment on NixOS using uv, running within this sandboxed environment.
{
config,
inputs,
pkgs,
lib,
...
}:
let
mkUvFHS =
name:
pkgs.buildFHSEnv {
inherit name;
runScript = name;
targetPkgs =
pkgs: with pkgs; [
uv
gcc
binutils
libgcc
zlib
bzip2
zstd
readline
openssl
gdbm
ncurses
sqlite
tk
libffi
expat
xz
blas
lapack
gfortran
stdenv.cc.cc.lib
libxml2
libxslt
curl
git
];
profile = ''
export LD_LIBRARY_PATH=/lib:/lib64:$LD_LIBRARY_PATH
export LIBRARY_PATH=/lib:/lib64:$LIBRARY_PATH
export C_INCLUDE_PATH=/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/include:$CPLUS_INCLUDE_PATH
export PKG_CONFIG_PATH=/lib/pkgconfig:$PKG_CONFIG_PATH
'';
};
in
{
home.packages = [
(pkgs.writeShellScriptBin "py" ''
exec uv run python "$@"
'')
(mkUvFHS "uvx")
(mkUvFHS "uv")
];
}
With this setup, you can use uv to manage multiple python versions.
uv python install cpython-3.13.3-linux-x86_64-gnu
Run multiple python versions
λ uv run python --version
Python 3.10.17
λ uv init
Initialized project `py`
λ uv python pin cpython-3.13.3-linux-x86_64-gnu
Updated `.python-version` from `3.10` -> `cpython-3.13.3-linux-x86_64-gnu`
λ uv run python --version
Using CPython 3.13.3
Creating virtual environment at: .venv
Python 3.13.3
Latest yt-dlp with uvx
uvx yt-dlp@latest --help