Skip to content

Commit

Permalink
fix up language in python environment guide
Browse files Browse the repository at this point in the history
  • Loading branch information
fricklerhandwerk committed Jan 29, 2024
1 parent 75eceb5 commit 720d794
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion source/guides/recipes/direnv.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(direnv)=
(automatic-direnv)=
# Automatic environment activation with `direnv`

Instead of manually activating the environment for each project, you can reload a [declarative shell](declarative-reproducible-envs) every time you enter the project's directory or change the `shell.nix` inside it.
Expand Down
55 changes: 27 additions & 28 deletions source/guides/recipes/python-environment.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(python-dev-environment)=
# Setting up a Python development environment

In this example you will build a Python web application using the Flask web framework as an exercise.
In this example you will build a Python web application using the [Flask](https://palletsprojects.com/p/flask/) web framework as an exercise.
To make best use of it you should be familiar with [defining declarative shell environments](declarative-reproducible-envs).

Create a new file called `myapp.py` and add the following code:
Expand All @@ -26,40 +26,33 @@ if __name__ == "__main__":
run()
```

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

To declare the development environment, create a new file `shell.nix`:
Create a new file `shell.nix` to declare the development environment:

```{code-block} nix shell.nix
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-22.11") {} }:
pkgs.mkShell {
packages = [
(pkgs.python3.withPackages (ps: [
ps.flask
]))
pkgs.curl
pkgs.jq
packages = with pkgs; [
(python3.withPackages (ps: [ ps.flask ]))
curl
jq
];
}
```

This creates a shell environment with an instance of `python3` that includes the `flask` package.

This describes a shell environment with an instance of `python3` that includes the `flask` package using [`python3.withPackages`](https://nixos.org/manual/nixpkgs/stable/#python.withpackages-function).
It also contains [`curl`], a utility to perform web
requests, and [`jq`], a tool to parse and format JSON documents.

[`curl`]: https://curl.se
[`jq`]: https://stedolan.github.io/jq/
[virtualenv]: https://virtualenv.pypa.io/en/latest/
[`curl`]: https://search.nixos.org/packages?show=curl
[`jq`]: https://search.nixos.org/packages?show=jq

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.
If you went with Python's [virtualenv](https://virtualenv.pypa.io/en/latest/), it would not be possible to add these utilities to the development environment without additional manual steps.

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

```shell-session
$ nix-shell
Expand All @@ -71,13 +64,13 @@ these 93 paths will be fetched (109.50 MiB download, 468.52 MiB unpacked):
/nix/store/138azk9hs5a2yp3zzx6iy1vdwi9q26wv-hook
...
[nix-shell:~/dev-environment]$
[nix-shell:~]$
```

Start the web application within this shell environment:

```shell-session
[nix-shell:~/dev-environment]$ python ./myapp.py
[nix-shell:~]$ python ./myapp.py
* 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.
Expand All @@ -88,25 +81,31 @@ 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:

```shell-session
$ nix-shell
[nix-shell:~/dev-environment]$ curl 127.0.0.1:5000
[nix-shell:~]$ curl 127.0.0.1:5000
{"message":"Hello, Nix!"}
[nix-shell:~/dev-environment]$ curl 127.0.0.1:5000 | jq '.message'
[nix-shell:~]$ curl 127.0.0.1:5000 | 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, we can use both `curl` and `jq` to test the running web application without any manual installation.
Nix does all of that for us.
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](install-nix).

## Next steps

We 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.
- [](packaging-existing-software)
- [](file-sets)
- [](automatic-direnv)
- [](./dependency-management.md)
2 changes: 1 addition & 1 deletion source/tutorials/packaging-existing-software.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,5 +525,5 @@ default.nix hello.nix icat.nix result
## Next steps

- [](sharing-dependencies)
- [](direnv)
- [](automatic-direnv)
- [](python-dev-environment)

0 comments on commit 720d794

Please sign in to comment.