laserproxy: add nix build

Change-Id: If93f4ba69afa028fed9098663a523f46d6134f7c
diff --git a/hswaw/laserproxy/README.md b/hswaw/laserproxy/README.md
index e843b00..0743a08 100644
--- a/hswaw/laserproxy/README.md
+++ b/hswaw/laserproxy/README.md
@@ -37,10 +37,8 @@
 Deployment
 ----------
 
-You'll need root access to customs.
+This runs on customs.hackerspace.pl, which is a NixOS box with its own independant configuration. It imports hscloud and builds the `hswaw.laserproxy` attribute. You can replicate this by running:
 
-    bazel build --platforms=@io_bazel_rules_go//go/toolchain:openbsd_amd64 //hswaw/laserproxy
-    ssh root@customs supervisorctl stop laserproxy
-    scp bazel-bin/hswaw/laserproxy/laserproxy_/laserproxy root@customs:/var/laserproxy/laserproxy
-    ssh root@customs supervisorctl start laserproxy
+    nix-build -A hswaw.laserproxy
 
+From the root of hscloud. To actually deploy it on customs, well, ssh there and upate `/etc/nixos/configuration.nix`. Some day its configuration might also get ported to hscloud.
diff --git a/hswaw/laserproxy/default.nix b/hswaw/laserproxy/default.nix
new file mode 100644
index 0000000..5b235ef
--- /dev/null
+++ b/hswaw/laserproxy/default.nix
@@ -0,0 +1,46 @@
+{ hscloud, pkgs, ... }:
+
+# This supports building laserproxy in nix, which is used for deploying it to customs.
+# This is mildly hacky and should be reworked to function for any hscloud bazel target
+# in a more generic fashion.
+#
+# To build:
+#   ~/hscloud $ nix-build -A hswaw.laserproxy
+
+pkgs.buildBazelPackage rec {
+  name = "laserproxy";
+  src = hscloud.root;
+  bazelTarget = "//hswaw/laserproxy";
+  nativeBuildInputs = with pkgs; [
+    git python3 postgresql go
+  ];
+  patches = [
+    ./nix-disable-uwsgi.patch
+    ./nix-use-system-go.patch
+    ./nix-disable-workspace-status.patch
+  ];
+
+  removeRulesCC = false;
+  fetchConfigured = true;
+  fetchAttrs = {
+    inherit patches;
+    # Nicked from nixpkgs usages of buildBazelPackage.
+    preInstall = ''
+      rm -rf $bazelOut/external/{go_sdk,\@go_sdk.marker}
+      sed -e '/^FILE:@go_sdk.*/d' -i $bazelOut/external/\@*.marker
+    
+      chmod -R 755 $bazelOut/external/{bazel_gazelle_go_repository_cache,@\bazel_gazelle_go_repository_cache.marker}
+      rm -rf $bazelOut/external/{bazel_gazelle_go_repository_cache,@\bazel_gazelle_go_repository_cache.marker}
+    
+      rm -rf $bazelOut/external/{bazel_gazelle_go_repository_tools,\@bazel_gazelle_go_repository_tools.marker}
+      sed -e '/^FILE:@bazel_gazelle_go_repository_tools.*/d' -i $bazelOut/external/\@*.marker
+    '';
+    sha256 = "0d0vlls1i0nmlqiszfhwb0cn1kwll3ffxd3sn30pk7drhfrbvqhf";
+  };
+  buildAttrs = {
+    inherit patches;
+    installPhase = ''
+      install -Dm755 bazel-bin/hswaw/laserproxy/*/laserproxy $out/bin/laserproxy
+    '';
+  };
+}
diff --git a/hswaw/laserproxy/nix-disable-uwsgi.patch b/hswaw/laserproxy/nix-disable-uwsgi.patch
new file mode 100644
index 0000000..02a69b7
--- /dev/null
+++ b/hswaw/laserproxy/nix-disable-uwsgi.patch
@@ -0,0 +1,11 @@
+diff --git a/third_party/py/requirements.txt b/third_party/py/requirements.txt
+index 94765d7..d7f0f3d 100644
+--- a/third_party/py/requirements.txt
++++ b/third_party/py/requirements.txt
+@@ -42,6 +42,5 @@ six==1.12.0
+ SQLAlchemy==1.3.8
+ sqlparse==0.3.0
+ urllib3==1.25.3
+-uWSGI==2.0.18
+ Werkzeug==0.15.5
+ WTForms==2.2.1
diff --git a/hswaw/laserproxy/nix-disable-workspace-status.patch b/hswaw/laserproxy/nix-disable-workspace-status.patch
new file mode 100644
index 0000000..b10b18f
--- /dev/null
+++ b/hswaw/laserproxy/nix-disable-workspace-status.patch
@@ -0,0 +1,11 @@
+diff --git a/.bazelrc b/.bazelrc
+index 419641e..207ae15 100644
+--- a/.bazelrc
++++ b/.bazelrc
+@@ -3,6 +3,5 @@ build --host_force_python=PY2
+ test --host_force_python=PY2
+ run --host_force_python=PY2
+ build --stamp
+-build --workspace_status_command=./bzl/workspace-status.sh
+ test --build_tests_only
+ test --test_output=errors
diff --git a/hswaw/laserproxy/nix-use-system-go.patch b/hswaw/laserproxy/nix-use-system-go.patch
new file mode 100644
index 0000000..15584f2
--- /dev/null
+++ b/hswaw/laserproxy/nix-use-system-go.patch
@@ -0,0 +1,13 @@
+diff --git a/third_party/nix/repository_rules.bzl b/third_party/nix/repository_rules.bzl
+index 35c3d2d..3386d4b 100644
+--- a/third_party/nix/repository_rules.bzl
++++ b/third_party/nix/repository_rules.bzl
+@@ -16,7 +16,7 @@ def hscloud_go_register_toolchains():
+     imports_for_non_nix = """
+ load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
+ def hscloud_go_register_toolchains():
+-    go_register_toolchains()
++    go_register_toolchains(go_version="host")
+ """
+ 
+     if has_nix(ctx):