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