Setting up a Python development environment#

In this example you will build a Python web application using the Flask web framework as an exercise. To make best use of it you should be familiar with defining declarative shell environments.

Create a new file called and add the following code:

#!/usr/bin/env python

from flask import Flask

app = Flask(__name__)

def hello():
    return {
        "message": "Hello, Nix!"

def run():"", port=5000)

if __name__ == "__main__":

This is a simple Flask application which serves a JSON document with the message "Hello, Nix!".

Create a new file shell.nix to declare the development environment:

{ pkgs ? import (fetchTarball "") {} }:

pkgs.mkShellNoCC {
  packages = with pkgs; [
    (python3.withPackages (ps: [ ps.flask ]))

This describes a shell environment with an instance of python3 that includes the flask package using python3.withPackages. It also contains curl, a utility to perform web requests, and jq, a tool to parse and format JSON documents.

Both of them are not Python packages. If you went with Python’s virtualenv, it would not be possible to add these utilities to the development environment without additional manual steps.

Run nix-shell to enter the environment you just declared:

$ nix-shell
these 2 derivations will be built:
these 93 paths will be fetched (109.50 MiB download, 468.52 MiB unpacked):


Start the web application within this shell environment:

[nix-shell:~]$ python ./
 * Serving Flask app 'myapp'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (
 * Running on
 * Running on
Press CTRL+C to quit

You now have a running Python web application. Try it out!

Open a new terminal to start another session of the shell environment and follow the commands below:

$ nix-shell

[nix-shell:~]$ curl
{"message":"Hello, Nix!"}

[nix-shell:~]$ curl | jq '.message'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    26  100    26    0     0  13785      0 --:--:-- --:--:-- --:--:-- 26000
"Hello, Nix!"

As demonstrated, you can use both curl and jq to test the running web application without any manual installation.

You can commit the files we created to version control and share them with other people. Others can now use the same shell environment as long as they have Nix installed.

Next steps#