diff --git a/14-rock-paper-scissors/default.nix b/14-rock-paper-scissors/default.nix new file mode 100644 index 0000000..ee9c4df --- /dev/null +++ b/14-rock-paper-scissors/default.nix @@ -0,0 +1,87 @@ +{ pkgs, lib, sbt-derivation }: + +let + pname = "rock-paper-scissors"; + package = sbt-derivation.lib.mkSbtDerivation { + inherit pkgs pname; + # ...and the rest of the arguments + version = "0.0.1"; + src = pkgs.nix-gitignore.gitignoreSource [ ] ./.; + nativeBuildInputs = [ pkgs.nodePackages.tailwindcss ]; + buildPhase = '' + tailwindcss -i ./src/input.css -o ./src/main/resources/public/output.css + sbt assembly + ''; + # 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-*/${pname}-assembly-*.jar $out/bin/${pname}.jar + ''; + + depsSha256 = "sha256-Y5RktcE3fxUJci4o7LTuNlBEybTdVRqsG551AkVeRPw="; + }; + + module = { config, pkgs, ... }: + let cfg = config.services.${pname}; + in { + options.services.${pname} = { + enable = lib.mkEnableOption "My frontendmentor exercise ${pname}"; + + 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."${pname}-group" = { }; + users.users."${pname}-user" = { + isSystemUser = true; + group = "${pname}-group"; + }; + + systemd.services.${pname} = + let serverHost = if cfg.useNginx then "localhost" else cfg.host; + in { + description = "Exercise app ${pname}"; + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + startLimitIntervalSec = 30; + startLimitBurst = 10; + serviceConfig = { + ExecStart = + "${pkgs.jdk}/bin/java -jar ${package}/bin/${pname}.jar -p ${ + toString cfg.port + } --host ${serverHost}"; + WorkingDirectory = "${package}/bin"; + Restart = "on-failure"; + User = "${pname}-user"; + Group = "${pname}-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/flake.nix b/flake.nix index da26672..3c1abc6 100644 --- a/flake.nix +++ b/flake.nix @@ -23,6 +23,10 @@ inherit pkgs sbt-derivation; lib = pkgs.lib; }; + rock-paper-scissors = import ./14-rock-paper-scissors/default.nix { + inherit pkgs sbt-derivation; + lib = pkgs.lib; + }; in { devShells.default = pkgs.mkShell { buildInputs = [ @@ -40,6 +44,8 @@ nixosModules.order-summary = order-summary.module; packages.testimonials-grid = testimonials-grid.package; nixosModules.testimonials-grid = testimonials-grid.module; + packages.rock-paper-scissors = rock-paper-scissors.package; + nixosModules.rock-paper-scissors = rock-paper-scissors.module; }); # see https://serokell.io/blog/practical-nix-flakes }