{ inputs = { nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-25.05"; utils.url = "github:numtide/flake-utils"; }; outputs = { self, nixpkgs, utils, }: utils.lib.eachDefaultSystem ( system: let # The path to the npm project src = ./.; # Read the package-lock.json as a Nix attrset packageLock = builtins.fromJSON (builtins.readFile (src + "/package-lock.json")); # Create an array of all (meaningful) dependencies deps = builtins.attrValues (removeAttrs packageLock.packages [ "" ]); # ++ builtins.attrValues (removeAttrs packageLock.dependencies [ "" ]); # Turn each dependency into a fetchurl call tarballs = map ( p: pkgs.fetchurl { url = p.resolved; hash = p.integrity; } ) deps; # Write a file with the list of tarballs tarballsFile = pkgs.writeTextFile { name = "tarballs"; text = builtins.concatStringsSep "\n" tarballs; }; pkgs = import nixpkgs { inherit system; }; in { devShell = with pkgs; mkShell { buildInputs = [ nodejs_22 # Even versions are more stable pocketbase ]; }; nodeModules = pkgs.stdenv.mkDerivation { name = "lazy-dm-node-modules"; src = ./.; buildInputs = [ pkgs.nodejs ]; buildPhase = '' export HOME=$PWD/.home export npm_config_cache=$PWD/.npm mkdir -p $out cd $out cp -r $src/package.json $src/package-lock.json . while read package do echo "caching $package" npm cache add "$package" done <${tarballsFile} npm ci ''; }; # Derivation for node_modules (npm ci) # nodeModules = pkgs.stdenv.mkDerivation { # name = "lazy-dm-node-modules"; # src = ./.; # buildInputs = [ pkgs.nodejs_22 ]; # installPhase = '' # mkdir -p $out # cp package.json package-lock.json $out/ # cd $out # npm ci --ignore-scripts # ''; # # Only output node_modules # dontBuild = true; # dontConfigure = true; # }; # Derivation for vite build viteBuild = pkgs.stdenv.mkDerivation { name = "lazy-dm-vite-build"; src = ./.; buildInputs = [ pkgs.nodejs_22 ]; # Use node_modules from previous derivation NODE_PATH = "${self.outputs.nodeModules}/node_modules"; installPhase = '' cp -r $src $out/app cd $out/app cp ${self.outputs.nodeModules}/package.json . cp -r ${self.outputs.nodeModules}/node_modules . npm run build mkdir -p $out/dist cp -r dist/* $out/dist/ ''; dontBuild = true; dontConfigure = true; }; dockerImage = pkgs.dockerTools.buildLayeredImage { name = "lazy-dm-app"; tag = "latest"; contents = [ pkgs.caddy ]; config = { Cmd = [ "/bin/caddy" "file-server" "--root" "/srv" "--listen" ":8080" ]; ExposedPorts = { "8080/tcp" = { }; }; WorkingDir = "/srv"; }; extraCommands = '' mkdir -p $out/srv cp -r ${self.outputs.viteBuild}/dist/* $out/srv/ ''; }; } ); }