diff --git a/nix/default.nix b/nix/default.nix deleted file mode 100644 index 74c6a83..0000000 --- a/nix/default.nix +++ /dev/null @@ -1,122 +0,0 @@ -{ - pkgs, - lib ? pkgs.lib, -}: let - inherit (builtins) readFile hashString split elemAt fetchurl toJSON baseNameOf; - inherit (pkgs) linkFarm writeText stdenv writeShellScriptBin; - inherit (lib) flatten mapAttrsToList importJSON cleanSourceWith; - - urlPart = url: elemAt (flatten (split "://([a-z0-9\.]*)" url)); - artifactPath = url: "${urlPart url 0}/${urlPart url 1}/${hashString "sha256" (urlPart url 2)}"; - - mkDepsLink = lockfile: - linkFarm "deps" (flatten ( - mapAttrsToList - ( - url: sha256: [ - { - name = artifactPath url; - path = fetchurl {inherit url sha256;}; - } - { - name = (artifactPath url) + ".metadata.json"; - path = writeText "metadata.json" (toJSON { - inherit url; - headers = {}; - }); - } - ] - ) - (importJSON lockfile) - )); -in rec { - mkBundled = { - name, - version, - src, - entrypoint, - lockfile, - importMap ? null, - denoFlags ? [], - }: - stdenv.mkDerivation { - inherit name version entrypoint; - denoFlags = - denoFlags - ++ ( - if importMap != null - then ["--import-map" importMap] - else [] - ); - - src = cleanSourceWith { - inherit src; - filter = path: type: (baseNameOf path != "bundled.js"); - }; - buildInputs = with pkgs; [ - deno - jq - ]; - - buildPhase = '' - export DENO_DIR=`mktemp -d` - ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache) - - deno bundle $denoFlags $entrypoint bundled.js - ''; - installPhase = '' - mkdir -p $out/dist - install -t $out/dist bundled.js - ''; - }; - mkBundledWrapper = { - name, - entrypoint, - ... - } @ args: let - bundled = mkBundled args; - in - writeShellScriptBin - "${name}" - "${pkgs.deno}/bin/deno run ${bundled}/dist/bundled.js"; - - mkExecutable = { - name, - version, - src, - entrypoint, - lockfile, - importMap ? null, - denoFlags ? [], - }: - stdenv.mkDerivation { - inherit src name entrypoint; - denoFlags = - denoFlags - ++ ["--lock" "${src}/${lockfile}"] - ++ ["--cached-only"] - ++ ["--output" name] - ++ ( - if importMap != null - then ["--import-map" "${src}/${importMap}"] - else [] - ); - buildInputs = with pkgs; [ - deno - jq - ]; - fixupPhase = ":"; - - buildPhase = '' - export DENO_DIR=`mktemp -d` - ln -s "${mkDepsLink "${src}/${lockfile}"}" $(deno info --json | jq -r .modulesCache) - ls $(dirname $src/$entrypoint) - - deno compile $denoFlags "$entrypoint" - ''; - installPhase = '' - mkdir -p $out/bin - mv "$name" "$out/bin/" - ''; - }; -} diff --git a/nix/make-bundled-wrapper.nix b/nix/make-bundled-wrapper.nix new file mode 100644 index 0000000..0bb7413 --- /dev/null +++ b/nix/make-bundled-wrapper.nix @@ -0,0 +1,6 @@ +{pkgs, ...}: {name, ...} @ args: let + bundled = (pkgs.callPackage ./make-bundled.nix {}) args; +in + pkgs.writeShellScriptBin + "${name}" + "${pkgs.deno}/bin/deno run ${bundled}/dist/bundled.js" diff --git a/nix/make-bundled.nix b/nix/make-bundled.nix new file mode 100644 index 0000000..57d5739 --- /dev/null +++ b/nix/make-bundled.nix @@ -0,0 +1,45 @@ +{ + pkgs, + lib ? pkgs.lib, + ... +}: { + name, + version, + src, + entrypoint, + lockfile, + importMap ? null, + denoFlags ? [], +}: let + inherit (pkgs.callPackage ./utils.nix {}) mkDepsLink; +in + pkgs.stdenv.mkDerivation { + inherit name version entrypoint; + denoFlags = + denoFlags + ++ ( + if importMap != null + then ["--import-map" importMap] + else [] + ); + + src = lib.cleanSourceWith { + inherit src; + filter = path: type: (baseNameOf path != "bundled.js"); + }; + buildInputs = with pkgs; [ + deno + jq + ]; + + buildPhase = '' + export DENO_DIR=`mktemp -d` + ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache) + + deno bundle $denoFlags $entrypoint bundled.js + ''; + installPhase = '' + mkdir -p $out/dist + install -t $out/dist bundled.js + ''; + } diff --git a/nix/make-executable.nix b/nix/make-executable.nix new file mode 100644 index 0000000..39fb4c7 --- /dev/null +++ b/nix/make-executable.nix @@ -0,0 +1,40 @@ +{pkgs, ...}: { + name, + version, + src, + entrypoint, + lockfile, + importMap ? null, + denoFlags ? [], +}: let + inherit (pkgs.callPackage ./utils.nix {}) mkDepsLink; +in + pkgs.stdenv.mkDerivation { + inherit src name entrypoint; + denoFlags = + denoFlags + ++ ["--lock" lockfile] + ++ ["--cached-only"] + ++ ["--output" name] + ++ ( + if importMap != null + then ["--import-map" importMap] + else [] + ); + buildInputs = with pkgs; [ + deno + jq + ]; + fixupPhase = ":"; + + buildPhase = '' + export DENO_DIR=`mktemp -d` + ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache) + + deno compile $denoFlags "$entrypoint" + ''; + installPhase = '' + mkdir -p $out/bin + mv "$name" "$out/bin/" + ''; + } diff --git a/nix/utils.nix b/nix/utils.nix new file mode 100644 index 0000000..719f199 --- /dev/null +++ b/nix/utils.nix @@ -0,0 +1,33 @@ +{ + pkgs, + lib ? pkgs.lib, + ... +}: let + inherit (builtins) hashString split elemAt fetchurl toJSON; + inherit (pkgs) linkFarm writeText; + inherit (lib) flatten mapAttrsToList importJSON; +in rec { + urlPart = url: elemAt (flatten (split "://([a-z0-9\.]*)" url)); + artifactPath = url: "${urlPart url 0}/${urlPart url 1}/${hashString "sha256" (urlPart url 2)}"; + mkDepsLink = lockfile: ( + linkFarm "deps" (lib.flatten ( + mapAttrsToList + ( + url: sha256: [ + { + name = artifactPath url; + path = fetchurl {inherit url sha256;}; + } + { + name = (artifactPath url) + ".metadata.json"; + path = writeText "metadata.json" (toJSON { + inherit url; + headers = {}; + }); + } + ] + ) + (importJSON lockfile) + )) + ); +} diff --git a/overlay.nix b/overlay.nix index d1ff507..6e145ce 100644 --- a/overlay.nix +++ b/overlay.nix @@ -1,5 +1,7 @@ final: prev: { deno2nix = { - inherit (import ./nix {pkgs = prev;}) mkBundled mkBundledWrapper mkExecutable; + mkBundled = final.callPackage ./nix/make-bundled.nix {}; + mkBundledWrapper = final.callPackage ./nix/make-bundled-wrapper.nix {}; + mkExecutable = final.callPackage ./nix/make-executable.nix {}; }; }