diff --git a/.gitignore b/.gitignore index 33c24ce..76128a6 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ dist-ssr *.sln *.sw? /tmp +/result diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..84905bc --- /dev/null +++ b/flake.lock @@ -0,0 +1,77 @@ +{ + "nodes": { + "naked-shell": { + "locked": { + "lastModified": 1681286841, + "narHash": "sha256-3XlJrwlR0nBiREnuogoa5i1b4+w/XPe0z8bbrJASw0g=", + "owner": "90-008", + "repo": "mk-naked-shell", + "rev": "7612f828dd6f22b7fb332cc69440e839d7ffe6bd", + "type": "github" + }, + "original": { + "owner": "90-008", + "repo": "mk-naked-shell", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1778869304, + "narHash": "sha256-30sZNZoA1cqF5JNO9fVX+wgiQYjB7HJqqJ4ztCDeBZE=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d233902339c02a9c334e7e593de68855ad26c4cb", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1777168982, + "narHash": "sha256-GOkGPcboWE9BmGCRMLX3worL4EMnsnG8MyKmXNeYuhQ=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "f5901329dade4a6ea039af1433fb087bd9c1fe14", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1778716662, + "narHash": "sha256-m1Yf0wZ8j1OHjTc2UwHwyQRSnNeSgLJOd7q5Y45hzi4=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "f7c1a2d347e4c52d5fb8d10cb4d94b5884e546fb", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "root": { + "inputs": { + "naked-shell": "naked-shell", + "nixpkgs": "nixpkgs", + "parts": "parts" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..9b7dbf6 --- /dev/null +++ b/flake.nix @@ -0,0 +1,35 @@ +{ + inputs.parts.url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + inputs.naked-shell.url = "github:90-008/mk-naked-shell"; + + outputs = inp: + inp.parts.lib.mkFlake {inputs = inp;} { + systems = ["x86_64-linux"]; + imports = [ + inp.naked-shell.flakeModule + ]; + perSystem = { + lib, + config, + pkgs, + ... + }: { + devShells.default = pkgs.mkShell { + name = "untangled-devshell"; + packages = with pkgs; [ + bun + typescript-language-server + ]; + shellHook = '' + export PATH="$PATH:$PWD/node_modules/.bin" + ''; + }; + packages.modules = pkgs.callPackage ./nix/modules.nix {}; + packages.untangled = pkgs.callPackage ./nix { + inherit (config.packages) modules; + }; + packages.default = config.packages.untangled; + }; + }; +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..e95ca59 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,63 @@ +{ + lib, + stdenv, + bun, + makeBinaryWrapper, + modules, + PUBLIC_BASE_URL ? "http://localhost:5173", +}: +stdenv.mkDerivation { + name = "untangled"; + + src = lib.fileset.toSource { + root = ../.; + fileset = lib.fileset.unions [ + ../package.json + ../bun.lock + ../tsconfig.json + ../tsconfig.app.json + ../tsconfig.node.json + ../vite.config.ts + ../index.html + ../src + ../public + ]; + }; + + nativeBuildInputs = [makeBinaryWrapper]; + buildInputs = [bun stdenv.cc.cc.lib]; + + inherit PUBLIC_BASE_URL; + + dontCheck = true; + + configurePhase = '' + runHook preConfigure + cp -R --no-preserve=ownership,mode ${modules} node_modules + find node_modules/.bin -exec chmod 755 {} \; + find node_modules -type d -exec chmod 755 {} \; + substituteInPlace node_modules/.bin/vite \ + --replace-fail "/usr/bin/env node" "${bun}/bin/bun --bun" + substituteInPlace node_modules/.bin/tsc \ + --replace-fail "/usr/bin/env node" "${bun}/bin/bun --bun" + runHook postConfigure + ''; + buildPhase = '' + runHook preBuild + HOME=$TMPDIR ${bun}/bin/bun -b --prefer-offline run build + runHook postBuild + ''; + installPhase = '' + runHook preInstall + + mkdir -p $out/bin + cp -R ./dist/* $out + cp -R ./node_modules $out + + makeBinaryWrapper ${bun}/bin/bun $out/bin/untangled \ + --prefix PATH : ${lib.makeBinPath [bun]} \ + --add-flags "run --bun --no-install --cwd $out index.js" + + runHook postInstall + ''; +} diff --git a/nix/modules.nix b/nix/modules.nix new file mode 100644 index 0000000..7181805 --- /dev/null +++ b/nix/modules.nix @@ -0,0 +1,34 @@ +{ + lib, + stdenv, + bun, +}: +stdenv.mkDerivation { + name = "modules"; + + src = lib.fileset.toSource { + root = ../.; + fileset = lib.fileset.unions [ + ../package.json + ../bun.lock + ]; + }; + + nativeBuildInputs = [bun]; + + outputHash = "sha256-oPgWdB2WleTUUSiEuXzSGLbQMwT+qFxMM+fvnAS6W0o="; + outputHashAlgo = "sha256"; + outputHashMode = "recursive"; + + dontConfigure = true; + dontCheck = true; + dontFixup = true; + + buildPhase = '' + HOME=$TMPDIR bun install --no-cache --no-progress --frozen-lockfile + ''; + installPhase = '' + cp -R node_modules $out + ls -la $out + ''; +}