Compare commits

..

No commits in common. "9c832fa1b2f0a4c19f3801555d4cd65ff2ae5599" and "d54d35eed10bbff21573ab50f5519bd8159888ed" have entirely different histories.

1 changed files with 38 additions and 113 deletions

111
flake.nix
View File

@ -1,35 +1,18 @@
{ {
description = description = "Planning Poker web app. Trying to build something and learn new things";
"Planning Poker web app. Trying to build something and learn new things";
inputs.nixpkgs.url = "github:nixos/nixpkgs"; inputs.nixpkgs.url = "github:nixos/nixpkgs";
inputs.flake-utils.url = "github:numtide/flake-utils"; inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.sbt-derivation.url = "github:zaninime/sbt-derivation/master"; inputs.sbt-derivation.url = "github:zaninime/sbt-derivation/master";
inputs.sbt-derivation.inputs.nixpkgs.follows = "nixpkgs"; inputs.sbt-derivation.inputs.nixpkgs.follows = "nixpkgs";
outputs = { self, nixpkgs, flake-utils, sbt-derivation }: outputs = { self, nixpkgs, flake-utils, sbt-derivation }:
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem
(system:
let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
packageName = "blanning-poker-kazbegi"; packageName = "blanning-poker-kazbegi";
backendName = "${packageName}-backend";
version = "0.1.1"; version = "0.1.1";
backendPackage = sbt-derivation.lib.mkSbtDerivation rec {
inherit pkgs version;
# ...and the rest of the arguments
pname = "${backendName}";
depsSha256 = "sha256-7MNlMeljYqXY4/kK4BXoycp9xUz0tGwCQvPTlE1RIQU=";
src = pkgs.nix-gitignore.gitignoreSource [ ] ./.;
buildPhase = ''
sbt backend/assembly
'';
installPhase = ''
mkdir -p $out/bin
cp backend/target/scala-*/backend-assembly-*.jar $out/bin/${pname}.jar
'';
};
in { in {
# Development shell with things required for local dev
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
buildInputs = [ buildInputs = [
pkgs.nodejs pkgs.nodejs
@ -43,81 +26,23 @@
echo "dev env for planning poker BWARGH started" echo "dev env for planning poker BWARGH started"
''; '';
}; };
# Just the backend jar packages.backend = sbt-derivation.lib.mkSbtDerivation rec {
packages.backend = backendPackage; inherit pkgs version;
# Module for NixOS to allow starting backend as SystemD service
module = { config, pkgs, ... }:
let
cfg = config.services.${backendName};
lib = pkgs.lib;
in {
options.services.${backendName} = {
enable =
lib.mkEnableOption "My frontendmentor exercise ${backendName}";
port = lib.mkOption { # ...and the rest of the arguments
type = lib.types.int; pname = "${packageName}-backend";
default = 8080; depsSha256 = "sha256-UOEq6KFS4rh4eaKxnRcVOh4SQWn3hnfpzcyRqPnFvco=";
description = "Port to listen on."; src = pkgs.nix-gitignore.gitignoreSource [] ./.;
}; buildPhase = ''
sbt backend/assembly
host = lib.mkOption {
type = lib.types.str;
default = "localhost";
description = "Host to bind to.";
};
useNginx = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to use Nginx to proxy requests.";
};
};
config = lib.mkIf cfg.enable {
users.groups."${backendName}-group" = { };
users.users."${backendName}-user" = {
isSystemUser = true;
group = "${backendName}-group";
};
systemd.services.${backendName} =
let serverHost = if cfg.useNginx then "localhost" else cfg.host;
in {
description = "Exercise app ${backendName}";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
startLimitIntervalSec = 30;
startLimitBurst = 10;
serviceConfig = {
ExecStart =
"${pkgs.jdk}/bin/java -jar ${backendPackage}/bin/${backendName}.jar -p ${
toString cfg.port
} --host ${serverHost}";
WorkingDirectory = "${backendPackage}/bin";
Restart = "on-failure";
User = "${backendName}-user";
Group = "${backendName}-group";
};
};
# this is only backend. Front end still configured and installed separately.
services.nginx.virtualHosts.${cfg.host}.locations."/api" = {
proxyPass = "http://127.0.0.1:${toString cfg.port}";
# this is config for websocket
extraConfig = ''
rewrite ^/api/(.*)$ /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Add the following lines for WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
''; '';
installPhase = ''
mkdir -p $out/bin
cp backend/target/scala-*/backend-assembly-*.jar $out/bin/
'';
}; };
}; }
}; );
});
# see https://serokell.io/blog/practical-nix-flakes # see https://serokell.io/blog/practical-nix-flakes
} }