From 050d595926fcd74608e753c6a18a5ea8c50e97a8 Mon Sep 17 00:00:00 2001 From: efim Date: Wed, 28 Jun 2023 12:42:59 +0000 Subject: [PATCH] feat(12): nix installation of exercise --- .../.gitignore | 16 ++++ .../default.nix | 90 +++++++++++++++++++ .../project/plugins.sbt | 2 + flake.nix | 6 ++ 4 files changed, 114 insertions(+) create mode 100644 12-order-summary-component-thymeleaf/.gitignore create mode 100644 12-order-summary-component-thymeleaf/default.nix diff --git a/12-order-summary-component-thymeleaf/.gitignore b/12-order-summary-component-thymeleaf/.gitignore new file mode 100644 index 0000000..b6ed91e --- /dev/null +++ b/12-order-summary-component-thymeleaf/.gitignore @@ -0,0 +1,16 @@ +.bsp/ +.scala-build/ +.metals/ +.direnv +*/dist/ +/11-single-price-grid-component/.bloop/ + +**/.bloop +**/project/project/ +**/project/metals.sbt +**/project/.bloop +**/project/target/ +**/target/ + +*/result +result diff --git a/12-order-summary-component-thymeleaf/default.nix b/12-order-summary-component-thymeleaf/default.nix new file mode 100644 index 0000000..2dfec80 --- /dev/null +++ b/12-order-summary-component-thymeleaf/default.nix @@ -0,0 +1,90 @@ +{ pkgs, lib, sbt-derivation }: + +let + package = sbt-derivation.lib.mkSbtDerivation { + inherit pkgs; + # ...and the rest of the arguments + pname = "order-summary-component-app"; + version = "0.0.1"; + src = pkgs.nix-gitignore.gitignoreSource [ ] ./.; + nativeBuildInputs = [ pkgs.nodePackages.tailwindcss ]; + buildPhase = '' + sbt assembly + tailwindcss -i ./src/input.css -o ./output.css + ''; + # css path different from ordinary development, + # because .gitignore makes it unavailable during nix build + # anyway copied to correct place + installPhase = '' + mkdir -p $out/bin + cp target/scala-*/order-summary-component-assembly-*.jar $out/bin/order-summary-component.jar + mkdir -p $out/bin/dist + cp ./output.css $out/bin/dist/output.css + cp -r public $out/bin/public + ''; + + depsSha256 = "sha256-ADQB4qTl69ERlLAURrtR3fWa7PUdYjFLk5QdU5QgxRQ="; + }; + + module = { config, pkgs, ... }: + let cfg = config.services.orderSummaryComponent; + in { + options.services.orderSummaryComponent = { + enable = lib.mkEnableOption "My service"; + + port = lib.mkOption { + type = lib.types.int; + default = 8080; + description = "Port to listen on."; + }; + + 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.order-summary-component-group = { }; + users.users.order-summary-component-user = { + isSystemUser = true; + group = "order-summary-component-group"; + }; + + systemd.services.price-grid-app = + let serverHost = if cfg.useNginx then "localhost" else cfg.host; + in { + description = "Exercise app Order Summary Component"; + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + startLimitIntervalSec = 30; + startLimitBurst = 10; + serviceConfig = { + ExecStart = + "${pkgs.jdk}/bin/java -jar ${package}/bin/order-summary-component.jar -p ${ + toString cfg.port + } --host ${serverHost}"; + WorkingDirectory = "${package}/bin"; + Restart = "on-failure"; + User = "order-summary-component-user"; + Group = "order-summary-component-group"; + }; + }; + + services.nginx = lib.mkIf cfg.useNginx { + virtualHosts.${cfg.host} = { + locations."/".proxyPass = "http://127.0.0.1:${toString cfg.port}"; + }; + }; + }; + }; +in { + package = package; + module = module; +} diff --git a/12-order-summary-component-thymeleaf/project/plugins.sbt b/12-order-summary-component-thymeleaf/project/plugins.sbt index 59cd318..cbb9bf7 100644 --- a/12-order-summary-component-thymeleaf/project/plugins.sbt +++ b/12-order-summary-component-thymeleaf/project/plugins.sbt @@ -1 +1,3 @@ addSbtPlugin("io.spray" % "sbt-revolver" % "0.10.0") + +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.1") diff --git a/flake.nix b/flake.nix index a886be6..81c90f4 100644 --- a/flake.nix +++ b/flake.nix @@ -15,6 +15,10 @@ inherit pkgs sbt-derivation; lib = pkgs.lib; }; + order-summary = import ./12-order-summary-component-thymeleaf/default.nix { + inherit pkgs sbt-derivation; + lib = pkgs.lib; + }; in { devShells.default = pkgs.mkShell { buildInputs = [ @@ -28,6 +32,8 @@ }; packages.price-grid-app = price-grid.package; nixosModules.price-grid-app = price-grid.module; + packages.order-summary = order-summary.package; + nixosModules.order-summary = order-summary.module; }); # see https://serokell.io/blog/practical-nix-flakes }