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