Python Tkinter not working on nixos

For a little project of mine in Python, I want to use tkinter. But despite my efforts I can’t seem to make tkinter work with my interpreter. I am using uv to manage my environment.

I have done this kind of install in my home.nix :

home.packages = with pkgs; [
uv
tk
(python313.withPackages (ps: [ ps.tkinter ]))

home.sessionVariables = { UV_PYTHON = "${pkgs.python313.withPackages (ps: [ ps.tkinter ])}/bin/python3"; UV_PYTHON_PREFERENCE = "only-system"; # empêche uv de télécharger son propre Python UV_PYTHON_DOWNLOADS = "never";

and here is the error i get when i run uv run main.py

Traceback (most recent call last):
File “/home/user/projects//main.py”, line 2, in
import tkinter
File “/etc/profiles/per-user/user/lib/python3.13/tkinter/init.py”, line 38, in
import _tkinter # If this fails your Python may not be configured for Tk
^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named ‘_tkinter’

my system and nix version, i have enabled unstable :
26.05.20260409.4c1018d (Yarara)
nix (Nix) 2.31.4

I would be happy to know what I can do to fix this situation. I read about creating a shell.nix but it seems really overkill for what I want to do — I wanted to know if the approach I am using is possible at all.

uv always wants to send scripts it runs through the packages in a virtualenv it creates. So you need to create that virtualenv so that it links to the site packages in your profile; otherwise, it only uses packages installed by uv.

$ uv venv --no-managed-python --system-site-packages
Using CPython 3.13.11 interpreter at: /nix/store/id9zb69ns7mz3vlz2dm2d4vw6j147lmz-python3-3.13.11-env/bin/python3.13
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate

$ uv run main.py

(The --no-managed-python is redundant with the UV_PYTHON_PREFERENCE = "only-system" that you have in your environment variables, but these are the commands that work for me in a bare nix-shell -p uv 'python3.withPackages (ps: ...)', with no environment variables set.)

1 Like

It worked, i just supressed my actuall env I had to remove from my home.nix the line UV_PYTHON_PREFERENCE = "only-system". Big thanks for the answer.

Please mark the topic as resolved !