Skip to content

Python development with uv and Nix

In this tutorial, you will learn how to set up a development environment for Python.

In the environment, you can use uv for package management BasedPyright as the language server. It is basic but sufficient for navigating an existing code base with LSP, and can be effortlessly added to an existing project.

Initialize a new project

  1. Make uv executable available:

    Terminal window
    nix shell nixpkgs#uv
  2. Use uv init to create a new project:

    Terminal window
    uv init sample_app --bare
  3. Enter the directory:

    Terminal window
    cd 'sample_app'

Below are extra steps to create a minimal Streamlit application. Actual steps would be dependent on the type of application you are creating.

  1. Add streamlit to the Python project:

    Terminal window
    uv add streamlit
  2. Create app.py:

    Terminal window
    import streamlit as st
    _ = st.title("My First Streamlit App with uv")
    st.write("Hello, Streamlit!")

Add flake.nix to the project

  1. Initialize the template from the root directory of the project:

    Terminal window
    nix flake init github:akirak/flake-templates#python-uv-simple
  2. Add .envrc:

    Terminal window
    use flake
  3. Allow direnv:

    Terminal window
    direnv allow

For the sample streamlit application, also add pkgs.streamlit to the development shell in flake.nix.

Developing

Use uv to develop the Python project as you would normally do without Nix.

Depending on the dependencies of your Python project, you might need to take extra steps. Follow instructions in the NixOS Wiki.

To build the project with Nix, follow the instructions in Nixpkgs Reference Manual.

The sample streamlit application can be run with the following command:

Terminal window
streamlit run app.py