summary refs log tree commit diff
path: root/flake.nix
diff options
context:
space:
mode:
Diffstat (limited to 'flake.nix')
-rw-r--r--flake.nix104
1 files changed, 38 insertions, 66 deletions
diff --git a/flake.nix b/flake.nix
index a5d6c53..6137835 100644
--- a/flake.nix
+++ b/flake.nix
@@ -21,26 +21,42 @@
 
       nodejs = pkgs.nodejs-18_x;
 
-      node_modules = pkgs.stdenv.mkDerivation {
-        name = "node_modules";
+      src = filter {
+        root = ./.;
+        include = [
+          ./package.json
+          ./package-lock.json
+        ];
+      };
 
-        src = filter {
-          root = ./.;
-          include = [
-            ./package.json
-            ./package-lock.json
-          ];
-        };
+      packageLock = builtins.fromJSON(builtins.readFile (src + "/package-lock.json"));
 
-        __noChroot = true;
+      deps = builtins.attrValues (removeAttrs packageLock.packages [ "" ])
+        ++ builtins.attrValues (removeAttrs (packageLock.dependencies or {} ) [ "" ])
+      ;
+      tarballs = map (p: pkgs.fetchurl { url = p.resolved; hash = p.integrity; }) deps;
+      tarballsFile = pkgs.writeTextFile {
+        name = "tarballs";
+        text = builtins.concatStringsSep "\n" tarballs;
+      };
 
-        configurePhase = ''
-          export HOME=$TMP
-        '';
+      tempestdev_modules = pkgs.stdenv.mkDerivation {
+        inherit src;
+
+        name = "node_modules";
 
         buildInputs = [ nodejs ];
 
         buildPhase = ''
+          export HOME=$PWD/.home
+          export npm_config_cache=$PWD/.npm
+
+          while read package
+          do
+            echo "caching $(echo $package | sed 's/\/nix\/store\/[^-]*-//')"
+            npm cache add "$package"
+          done <${tarballsFile}
+
           ${nodejs}/bin/npm ci
         '';
 
@@ -65,38 +81,17 @@
 
           nativeBuildInputs = [ nodejs ];
 
-          buildPhase = ''npm run build'';
-
           configurePhase = ''
-            ln -sf ${node_modules}/node_modules node_modules
+            ln -sf ${tempestdev_modules}/node_modules node_modules
             export HOME=$TMP
           '';
 
+          buildPhase = ''
+            npm run build
+          '';
+
           installPhase = ''
-            # Use the standalone nextjs version
-            mv .next/standalone $out
-
-            # Copy non-generated static files
-            if [ -d "public" ]; then
-              cp -R public $out/public
-            fi
-
-            # Also copy generated static files
-            mv .next/static $out/.next/static
-
-            # Re-link the node_modules
-            rm $out/node_modules
-            mv node_modules $out/node_modules
-
-            # Link cache directory
-            ln -s /tmp $out/.next/cache
-
-            # Wrap the script
-            cat <<ENTRYPOINT > $out/entrypoint
-            #!${pkgs.stdenv.shell}
-            exec "$(type -p node)" "$out/server.js" "$$@"
-            ENTRYPOINT
-            chmod +x $out/entrypoint
+            mv out $out
           '';
         };
       };
@@ -109,14 +104,6 @@
           options.ashe.services."tempest.dev" = {
             enable = mkEnableOption "Enables the tempest.dev HTTP service";
 
-            port = mkOption rec {
-              type = types.int;
-              default = 8000;
-              example = default;
-              description = "The port for this service to listen on";
-
-            };
-
             domain = mkOption rec {
               type = types.str;
               default = "tempest.dev";
@@ -126,29 +113,14 @@
           };
 
           config = mkIf cfg.enable {
-            systemd.services."ashe.tempest.dev" = {
-              wantedBy = [ "multi-user.target" ];
-
-              serviceConfig = let pkg = self.packages.${pkgs.system}.default;
-              in {
-                Restart = "on-failure";
-                ExecStart = "${pkg}/entrypoint";
-                DynamicUser = "yes";
-                PrivateTmp = "yes";
-                Environment = [
-                  "PORT=${toString cfg.port}"
-                ];
-              };
-            };
-
             services.nginx.virtualHosts.${cfg.domain} = {
-              locations."/" = { proxyPass = "http://127.0.0.1:${toString cfg.port}"; };
+              locations."/" = {
+                root = "${pkg}";
+              };
               forceSSL = true;
               enableACME = true;
             };
           };
         };
-
-
     };
 }