]> rtime.felk.cvut.cz Git - coffee/main.git/commitdiff
Add Nix expression for rtime-like environment
authorMichal Sojka <michal.sojka@cvut.cz>
Sun, 2 Aug 2020 22:23:06 +0000 (00:23 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Sun, 2 Aug 2020 22:23:06 +0000 (00:23 +0200)
The environment can be entered either by nix-shell or with the help of
Lorri and direnv, whose .envrc is also included.

.envrc [new file with mode: 0644]
shell.nix [new file with mode: 0644]

diff --git a/.envrc b/.envrc
new file mode 100644 (file)
index 0000000..d0ea541
--- /dev/null
+++ b/.envrc
@@ -0,0 +1,2 @@
+eval "$(lorri direnv)"
+export FLASK_APP="$PWD/coffee-flask/app.py"
diff --git a/shell.nix b/shell.nix
new file mode 100644 (file)
index 0000000..a03c7e5
--- /dev/null
+++ b/shell.nix
@@ -0,0 +1,83 @@
+with import <nixpkgs> {};
+
+let
+  pythonEnv = (let
+    python = let
+      packageOverrides = self: super: {
+        matplotlib = super.matplotlib.overridePythonAttrs(old: rec {
+          version = "3.0.2";
+          src =  super.fetchPypi {
+            pname = "matplotlib";
+            inherit version;
+            sha256 = "1brn3ism9755wibylb24yzs3b6ndg89iiclyhnvavxiiyhm7jjy9";
+          };
+        });
+        flask = super.flask.overridePythonAttrs(old: rec {
+          version = "1.0.2";
+          src =  super.fetchPypi {
+            pname = "Flask";
+            inherit version;
+            sha256 = "0j6f4a9rpfh25k1gp7azqhnni4mb4fgy50jammgjgddw1l3w0w92";
+          };
+          preFixup = ''
+            makeWrapperArgs+=(--set FLASK_NIX_HACK 1)
+          '';
+        });
+        flask-cors = super.flask-cors.overridePythonAttrs(old: rec {
+          version = "3.0.7";
+          src =  super.fetchPypi {
+            pname = "Flask-Cors";
+            inherit version;
+            sha256 = "1v6gq4vjgyxi8q8lxawpdfhq01adb4bznnabp08ks5nzbwibz43y";
+          };
+        });
+
+        # Workarounds for two Flask/Nix-related errors when FLASK_DEBUG = 1.
+        #
+        # Error #1: `flask run` prints the following:
+        #
+        #      File "/nix/store/iygklcrqcyshaq57ybizia55x70cn1vq-python3.7-Flask-1.0.2/bin/flask", line 2
+        #      export PATH='/nix/store/wpw6qdjbrv7s72kk81n1lwf3vj6fldk2-python3-3.7.8/bin:/nix/store/iygklcrqcyshaq57ybizia55x70cn1vq-python3.7-Flask-1.0.2/bin'${PATH:+':'}$PATH
+        #      SyntaxError: invalid syntax
+        #
+        # This can be fixed with updated version of
+        # https://github.com/NixOS/nixpkgs/issues/42924#issuecomment-409101368
+        # (need to prepend src/ in substituteInPlace)
+        #
+        # Error #2: After #1 is fixed, Flask complains about missing
+        # flask_cors. To solve this, we hardcode path to Flask wrapper
+        # script and use it if FLACK_NIX_HACK is set. People try to
+        # solve it properly at
+        # https://github.com/NixOS/nixpkgs/issues/72345#issuecomment-622244484
+        werkzeug = super.werkzeug.overrideAttrs (old: rec {
+          postPatch = ''
+            substituteInPlace src/werkzeug/_reloader.py \
+                --replace "rv = [sys.executable]" \
+                          'if "FLASK_NIX_HACK" in os.environ: sys.argv[0] = os.path.join(os.path.dirname(sys.executable), "flask")
+                return sys.argv'
+          '';
+        });
+      };
+    in pkgs.python37.override {inherit packageOverrides; self = python;};
+
+  in python.withPackages(ps: with ps; [
+    flask
+    flask-cors
+    matplotlib
+    requests
+  ]));
+in mkShell {
+  buildInputs = [
+    libev
+    openssl
+    pythonEnv
+    zlib
+  ];
+  FLASK_DEBUG = 1;
+
+  # Setting FLASK_APP this way work in nix-shell but not in lorri. Se
+  # we override the value in .envrc.
+  shellHook = ''
+    export FLASK_APP="$PWD/coffee-flask/app.py"
+  '';
+}