aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstuebinm2021-03-07 23:52:19 +0100
committerstuebinm2021-03-07 23:52:19 +0100
commit92c1ef1af95740077282d773242fdd1820c8d15b (patch)
treee8b6feb39975f4b1126eb49f2c7564e2cf3dae34
parent09eeb58abb279e1049493460a7a99124aa4d9a78 (diff)
Added basic nixos module (and restructured some files)
Right now, this is capable of setting up a running instance of picarones in a couple lines of config (both backend server and the webapp, using nginx). More options should still be added, especially to make the backend's port configurable (but this requires adding features to the haskell code)
Diffstat (limited to '')
-rw-r--r--.gitignore2
-rw-r--r--default.nix80
-rw-r--r--example.nix15
-rw-r--r--picarones-elm/cover.html19
-rw-r--r--picarones-elm/default.nix24
-rw-r--r--picarones-elm/index.html21
-rw-r--r--picarones-elm/slide.html34
-rw-r--r--picarones-elm/src/Cover.elm2
-rw-r--r--picarones-hs/build.nix15
-rw-r--r--picarones-hs/default.nix48
-rw-r--r--pkgs.nix6
11 files changed, 186 insertions, 80 deletions
diff --git a/.gitignore b/.gitignore
index 7d57a10..88eab70 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
*/dist-newstyle/*
-*/result
+*result
*/elm-stuff/*
*js
diff --git a/default.nix b/default.nix
new file mode 100644
index 0000000..d739a4c
--- /dev/null
+++ b/default.nix
@@ -0,0 +1,80 @@
+{config, lib, pkgs, ...}:
+let
+ picarones = import ./pkgs.nix { inherit pkgs; };
+in
+with lib;
+{
+ options.services.picarones = {
+ enable = mkOption {
+ default = false;
+ example = true;
+ type = types.bool;
+ description = "'next slide please' as a service";
+ };
+ package = mkOption {
+ description = "the server package to use";
+ default = picarones.picarones-hs;
+ type = types.package;
+ };
+ port = mkOption {
+ description = "port at which the backend server should listen.";
+ default = 9000;
+ type = types.int;
+ };
+ frontend = {
+ enable = mkOption {
+ description = "Whether to setup nginx to deliver the picarones frontend";
+ default = true;
+ type = types.bool;
+ };
+ package = mkOption {
+ description = "Which package provides the frontend app.";
+ default = picarones.picarones-elm;
+ type = types.package;
+ };
+ domain = mkOption {
+ description = "Which domain nginx should listen on.";
+ default = "";
+ type = types.str;
+ };
+ proxyBackend = mkOption {
+ description = "whether to set up a proxy for the backend using nginx (on the same domain).";
+ default = false;
+ type = types.bool;
+ };
+ config = mkOption {
+ description = "extra options for nginx.";
+ default = {};
+ type = types.attrs;
+ };
+ };
+ };
+
+ config = let cfg = config.services.picarones; in {
+ systemd.services.picarones-hs = {
+ description = "picarones backend server";
+ path = [ cfg.package ];
+ wantedBy = [ "multi-user.target" ];
+ script = ''
+ ${cfg.package}/bin/picarones-hs
+ '';
+ };
+
+ services.nginx = mkIf cfg.frontend.enable {
+ enable = true;
+ virtualHosts.${cfg.frontend.domain} = cfg.frontend.config // {
+ root =
+ # need to do it this (long) way since a user may change this value outside of this module
+ let ssl = config.services.nginx.virtualHosts.${cfg.frontend.domain}.enableSSL;
+ in (cfg.frontend.package.override {
+ baseurl = (if ssl then "https://" else "http://") + cfg.frontend.domain;
+ serverurl = (if ssl then "wss" else "ws") + "://${cfg.frontend.domain}/websocket";
+ }).outPath;
+ locations."/websocket" = {
+ proxyPass = "http://localhost:9160";
+ proxyWebsockets = true;
+ };
+ };
+ };
+ };
+}
diff --git a/example.nix b/example.nix
new file mode 100644
index 0000000..4c66bc9
--- /dev/null
+++ b/example.nix
@@ -0,0 +1,15 @@
+{config, pkgs, ...}:
+
+{
+ imports = [ ./default.nix ];
+
+ services.picarones = {
+ enable = true;
+ frontend = {
+ enable = true;
+ domain = "10.233.4.2";
+ };
+ };
+
+ networking.firewall.allowedTCPPorts = [ 80 ];
+}
diff --git a/picarones-elm/cover.html b/picarones-elm/cover.html
deleted file mode 100644
index d92eba9..0000000
--- a/picarones-elm/cover.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
- <meta charset="UTF-8">
- <title>Main</title>
- <script src="Cover.js"></script>
- <link rel="stylesheet" type="text/css" href="document.css" />
-</head>
-
-<body>
- <div id="elm"></div>
- <script>
- var app = Elm.Cover.init({
- node: document.getElementById('elm'),
- flags: "https://picarones.stuebinm.eu"
- });
- </script>
-</body>
-</html>
diff --git a/picarones-elm/default.nix b/picarones-elm/default.nix
index ed71959..b5e9d9d 100644
--- a/picarones-elm/default.nix
+++ b/picarones-elm/default.nix
@@ -1,9 +1,10 @@
-{ nixpkgs ? <nixpkgs>
-, config ? {}
+{ stdenv
+, elmPackages
+, lib
+, baseurl ? "example.org"
+, serverurl ? "wss://example.org"
}:
-with (import nixpkgs config);
-
let
mkDerivation =
{ srcs ? ./elm-srcs.nix
@@ -13,13 +14,15 @@ let
, targets ? []
, registryDat ? ./registry.dat
, outputJavaScript ? true
+ , patchPhase ? ""
+ , ...
}:
stdenv.mkDerivation {
- inherit name src;
+ inherit name src patchPhase;
buildInputs = [ elmPackages.elm ];
- buildPhase = pkgs.elmPackages.fetchElmDeps {
+ buildPhase = elmPackages.fetchElmDeps {
elmPackages = import srcs;
elmVersion = "0.19.1";
inherit registryDat;
@@ -32,7 +35,7 @@ let
mkdir -p $out/share/doc
${lib.concatStrings (map (module: ''
echo "compiling ${elmfile module}"
- elm make ${elmfile module} --output $out/${module}.${extension}
+ elm make ${elmfile module} --optimize --output $out/${module}.${extension}
'') targets)}
cp *.html $out
cp *.css $out
@@ -41,7 +44,12 @@ let
};
in mkDerivation {
name = "picarones";
-
+
+ patchPhase = ''
+ substituteInPlace index.html --replace "{{ baseurl }}" ${lib.escapeShellArg baseurl}
+ substituteInPlace slide.html --replace "{{ serverurl }}" ${lib.escapeShellArg serverurl}
+ '';
+
srcs = ./elm-srcs.nix;
src = ./.;
targets = ["Main" "Cover"];
diff --git a/picarones-elm/index.html b/picarones-elm/index.html
index 4fe72c7..156eb9c 100644
--- a/picarones-elm/index.html
+++ b/picarones-elm/index.html
@@ -3,32 +3,17 @@
<head>
<meta charset="UTF-8">
<title>Main</title>
- <script src="Main.js"></script>
+ <script src="Cover.js"></script>
<link rel="stylesheet" type="text/css" href="document.css" />
</head>
<body>
<div id="elm"></div>
<script>
- let args = location.hash.split("#").slice(1);
-
- var app = Elm.Main.init({
+ var app = Elm.Cover.init({
node: document.getElementById('elm'),
- flags: [args[0], parseInt(args[1])]
+ flags: "{{ baseurl }}"
});
-
- let ws = new WebSocket("ws://localhost:9160")
-
- ws.onopen = () => ws.send ("{\"room\":\"testroom\"}");
-
- ws.onmessage = function(msg) {
- console.log(msg.data)
- app.ports.recvPort.send(msg.data)
- }
-
- app.ports.sendPort.subscribe(function(msg) {
- ws.send(msg)
- })
</script>
</body>
</html>
diff --git a/picarones-elm/slide.html b/picarones-elm/slide.html
new file mode 100644
index 0000000..a72fb3c
--- /dev/null
+++ b/picarones-elm/slide.html
@@ -0,0 +1,34 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta charset="UTF-8">
+ <title>Main</title>
+ <script src="Main.js"></script>
+ <link rel="stylesheet" type="text/css" href="document.css" />
+</head>
+
+<body>
+ <div id="elm"></div>
+ <script>
+ let args = location.hash.split("#").slice(1);
+
+ var app = Elm.Main.init({
+ node: document.getElementById('elm'),
+ flags: [args[0], parseInt(args[1])]
+ });
+
+ let ws = new WebSocket("{{ serverurl }}")
+
+ ws.onopen = () => ws.send (JSON.stringify ({room:args[0]}));
+
+ ws.onmessage = function(msg) {
+ console.log(msg.data)
+ app.ports.recvPort.send(msg.data)
+ }
+
+ app.ports.sendPort.subscribe(function(msg) {
+ ws.send(msg)
+ })
+ </script>
+</body>
+</html>
diff --git a/picarones-elm/src/Cover.elm b/picarones-elm/src/Cover.elm
index 5c9d040..fee40c0 100644
--- a/picarones-elm/src/Cover.elm
+++ b/picarones-elm/src/Cover.elm
@@ -56,7 +56,7 @@ mkNumberInput i =
mkLink : Model -> Html Msg
mkLink (p, max, baseurl) = case p of
"" -> text "(none yet)"
- prefix -> let link = baseurl ++ "/slide#" ++ prefix ++ "#" ++ (String.fromInt max)
+ prefix -> let link = baseurl ++ "/slide.html#" ++ prefix ++ "#" ++ (String.fromInt max)
in a [ href link ] [ text link ]
diff --git a/picarones-hs/build.nix b/picarones-hs/build.nix
new file mode 100644
index 0000000..c61d763
--- /dev/null
+++ b/picarones-hs/build.nix
@@ -0,0 +1,15 @@
+{ mkDerivation, aeson, base, bytestring, stdenv, text
+, unordered-containers, websockets
+}:
+mkDerivation {
+ pname = "picarones-hs";
+ version = "0.1.0.0";
+ src = ./.;
+ isLibrary = false;
+ isExecutable = true;
+ executableHaskellDepends = [
+ aeson base bytestring text unordered-containers websockets
+ ];
+ license = "unknown";
+ hydraPlatforms = stdenv.lib.platforms.none;
+}
diff --git a/picarones-hs/default.nix b/picarones-hs/default.nix
index ebcb880..c61d763 100644
--- a/picarones-hs/default.nix
+++ b/picarones-hs/default.nix
@@ -1,33 +1,15 @@
-{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false }:
-
-let
-
- inherit (nixpkgs) pkgs;
-
- f = { mkDerivation, aeson, base, bytestring, stdenv, text
- , unordered-containers, websockets
- }:
- mkDerivation {
- pname = "haskell-ws-test";
- version = "0.1.0.0";
- src = ./.;
- isLibrary = false;
- isExecutable = true;
- executableHaskellDepends = [
- aeson base bytestring text unordered-containers websockets
- ];
- license = "unknown";
- hydraPlatforms = stdenv.lib.platforms.none;
- };
-
- haskellPackages = if compiler == "default"
- then pkgs.haskellPackages
- else pkgs.haskell.packages.${compiler};
-
- variant = if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;
-
- drv = variant (haskellPackages.callPackage f {});
-
-in
-
- if pkgs.lib.inNixShell then drv.env else drv
+{ mkDerivation, aeson, base, bytestring, stdenv, text
+, unordered-containers, websockets
+}:
+mkDerivation {
+ pname = "picarones-hs";
+ version = "0.1.0.0";
+ src = ./.;
+ isLibrary = false;
+ isExecutable = true;
+ executableHaskellDepends = [
+ aeson base bytestring text unordered-containers websockets
+ ];
+ license = "unknown";
+ hydraPlatforms = stdenv.lib.platforms.none;
+}
diff --git a/pkgs.nix b/pkgs.nix
new file mode 100644
index 0000000..2ede710
--- /dev/null
+++ b/pkgs.nix
@@ -0,0 +1,6 @@
+{ pkgs }:
+
+{
+ picarones-hs = pkgs.haskellPackages.callPackage ./picarones-hs/default.nix {};
+ picarones-elm = pkgs.lib.callPackageWith pkgs ./picarones-elm/default.nix {};
+}