]> rtime.felk.cvut.cz Git - coffee/main.git/blob - shell.nix
README: Document how to use nix-shell for development
[coffee/main.git] / shell.nix
1 { sources ? import ./nix/sources.nix
2 , nixpkgs ? sources.nixpkgs
3 , pkgs ? import nixpkgs {}
4 }:
5
6 with pkgs;
7
8 let
9   pythonEnv = (let
10     python = let
11       packageOverrides = self: super: {
12         matplotlib = (super.matplotlib.override { enableTk = false; }).overridePythonAttrs(old: rec {
13           version = "3.3.4";
14           src =  super.fetchPypi {
15             pname = "matplotlib";
16             inherit version;
17             sha256 = "1c15g3rhnp4pn46akwzqqgrwvalhsy44zi3nd169x4i2djvpsiry";
18           };
19
20           # qhull library in nixpkgs in newer than expected by matplotlib 3.3.4 and has a different name
21           postPatch = old.postPatch + ''
22             substituteInPlace setupext.py --replace 'ext.libraries.append("qhull")' 'ext.libraries.append("qhull_r")'
23           '';
24         });
25         flask = super.flask.overridePythonAttrs(old: rec {
26           version = "1.1.2";
27           src =  super.fetchPypi {
28             pname = "Flask";
29             inherit version;
30             sha256 = "0j6f4a9rpfh25k1gp7azqhnni4mb4fgy50jammgjgddw1l3w0w92";
31           };
32           preFixup = ''
33             makeWrapperArgs+=(--set FLASK_NIX_HACK 1)
34           '';
35         });
36         flask-cors = super.flask-cors.overridePythonAttrs(old: rec {
37           version = "3.0.9";
38           src =  super.fetchPypi {
39             pname = "Flask-Cors";
40             inherit version;
41             sha256 = "1v6gq4vjgyxi8q8lxawpdfhq01adb4bznnabp08ks5nzbwibz43y";
42           };
43         });
44         jinja2 = super.jinja2.overridePythonAttrs(old: rec {
45           version = "2.11.3";
46           src = old.src.override {
47             inherit version;
48             sha256 = "1iiklf3wns67y5lfcacxma5vxfpb7h2a67xbghs01s0avqrq9md6";
49           };
50         });
51
52         # Workarounds for two Flask/Nix-related errors when FLASK_DEBUG = 1.
53         #
54         # Error #1: `flask run` prints the following:
55         #
56         #      File "/nix/store/iygklcrqcyshaq57ybizia55x70cn1vq-python3.7-Flask-1.0.2/bin/flask", line 2
57         #      export PATH='/nix/store/wpw6qdjbrv7s72kk81n1lwf3vj6fldk2-python3-3.7.8/bin:/nix/store/iygklcrqcyshaq57ybizia55x70cn1vq-python3.7-Flask-1.0.2/bin'${PATH:+':'}$PATH
58         #      SyntaxError: invalid syntax
59         #
60         # This can be fixed with updated version of
61         # https://github.com/NixOS/nixpkgs/issues/42924#issuecomment-409101368
62         # (need to prepend src/ in substituteInPlace)
63         #
64         # Error #2: After #1 is fixed, Flask complains about missing
65         # flask_cors. To solve this, we hardcode path to Flask wrapper
66         # script and use it if FLACK_NIX_HACK is set. People try to
67         # solve it properly at
68         # https://github.com/NixOS/nixpkgs/issues/72345#issuecomment-622244484
69         werkzeug = super.werkzeug.overrideAttrs (old: rec {
70           postPatch = ''
71             substituteInPlace src/werkzeug/_reloader.py \
72                 --replace "rv = [sys.executable]" \
73                           'if "FLASK_NIX_HACK" in os.environ: sys.argv[0] = os.path.join(os.path.dirname(sys.executable), "flask")
74                 return sys.argv'
75           '';
76         });
77       };
78     in pkgs.python39.override {inherit packageOverrides; self = python;};
79
80   in python.withPackages(ps: with ps; [
81     flake8                      # For syntax checking while editing .py files
82     flask
83     flask-cors
84     matplotlib
85     requests
86   ]));
87 in mkShell {
88   buildInputs = [
89     libev
90     openssl
91     pythonEnv
92     zlib
93     pkg-config
94   ];
95   FLASK_DEBUG = 1;
96
97   # Setting FLASK_APP this way work in nix-shell but not in lorri. Se
98   # we override the value in .envrc.
99   shellHook = ''
100     export FLASK_APP="$PWD/coffee-flask/app.py"
101   '';
102 }