From a2092563c48cb71adc397f496f05049d4f20647a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SnO=E2=82=82WMaN?= Date: Sat, 10 Sep 2022 04:06:41 +0900 Subject: [PATCH] refactor: Fix and rename arg names (#9) --- README.md | 45 +++++++++++++++++++----------------- deno.jsonc | 1 + flake.nix | 37 ++++++++++++++++++----------- nix/make-bundled-wrapper.nix | 11 ++++++--- nix/make-bundled.nix | 39 ++++++++++++++----------------- nix/make-executable.nix | 38 +++++++++++++++--------------- 6 files changed, 93 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index 1fe0fc2..13b2c6b 100644 --- a/README.md +++ b/README.md @@ -20,28 +20,33 @@ deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts { inputs.deno2nix.url = "github:SnO2WMaN/deno2nix"; inputs.devshell.url = "github:numtide/devshell"; - + outputs = { - deno2nix, + self, + nixpkgs, flake-utils, ... - }: + } @ inputs: flake-utils.lib.eachDefaultSystem (system: let + inherit (pkgs) deno2nix; pkgs = import nixpkgs { inherit system; - overlays = [ + overlays = with inputs; [ devshell.overlay deno2nix.overlay ]; }; in { - packages.executable = pkgs.deno2nix.mkExecutable { - name = "example"; - version = "0.1.0"; - src = self; + packages.executable = deno2nix.mkExecutable { + pname = "example-executable"; + version = "0.1.2"; + + src = ./.; lockfile = ./lock.json; - importmap = ./import_map.json; - entrypoint = ./mod.ts; + + output = "example"; + entrypoint = "./mod.ts"; + importMap = "./import_map.json"; }; }); } @@ -52,20 +57,18 @@ deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts #### Args ```nix - { - name, - version, - src, - entrypoint, - lockfile, - importmap ? null, - denoFlags ? [], +{ + pname, + version, + src, + lockfile, + output ? pname, # generate binary name + entrypoint, + importMap ? null, # ex. "./import_map.json" to $src/${importMap} + additionalDenoFlags ? "", # ex. "--allow-net" } ``` -- `importMap = ./import_map.json` -- `denoFlags = ["--allow-net" true]` - ## Thanks - [esselius/nix-deno](https://github.com/esselius/nix-deno) diff --git a/deno.jsonc b/deno.jsonc index 14ae2d9..7c4b050 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,6 +1,7 @@ { "tasks": { "run": "deno run --import-map=./import_map.json ./mod.ts", + "compile": "deno compile --import-map=./import_map.json ./mod.ts", "cache": "deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts" }, "fmt": { diff --git a/flake.nix b/flake.nix index f8ee465..661d05a 100644 --- a/flake.nix +++ b/flake.nix @@ -40,35 +40,44 @@ in { packages.depslink = deno2nix.internal.mkDepsLink ./lock.json; packages.bundled = deno2nix.mkBundled { - name = "example"; + pname = "example-bundled"; version = "0.1.0"; + src = ./.; lockfile = ./lock.json; - importMap = ./import_map.json; - entrypoint = ./mod.ts; + + output = "bundled.js"; + entrypoint = "./mod.ts"; + importMap = "./import_map.json"; }; - packages.wrapper = deno2nix.mkBundledWrapper { - name = "example"; + packages.bundled-wrapper = deno2nix.mkBundledWrapper { + pname = "example-bundled-wrapper"; version = "0.1.0"; + src = ./.; lockfile = ./lock.json; - importMap = ./import_map.json; - entrypoint = ./mod.ts; + + output = "bundled.js"; + entrypoint = "./mod.ts"; + importMap = "./import_map.json"; }; packages.executable = deno2nix.mkExecutable { - name = "example"; - version = "0.1.0"; + pname = "example-executable"; + version = "0.1.2"; + src = ./.; lockfile = ./lock.json; - importMap = ./import_map.json; - entrypoint = ./mod.ts; + + output = "example"; + entrypoint = "./mod.ts"; + importMap = "./import_map.json"; }; packages.default = self.packages.${system}.executable; defaultPackage = self.packages.${system}.default; - apps.default = flake-utils.lib.mkApp { - drv = self.packages.${system}.executable; - }; + apps.bundled-wrapper = flake-utils.lib.mkApp {drv = self.packages.${system}.bundled-wrapper;}; + apps.executable = flake-utils.lib.mkApp {drv = self.packages.${system}.executable;}; + apps.default = self.apps.${system}.executable; checks = self.packages.${system}; diff --git a/nix/make-bundled-wrapper.nix b/nix/make-bundled-wrapper.nix index 4ab053a..0928bb1 100644 --- a/nix/make-bundled-wrapper.nix +++ b/nix/make-bundled-wrapper.nix @@ -4,9 +4,14 @@ deno2nix, writeShellScriptBin, ... -}: {name, ...} @ args: let +}: { + pname, + bin ? pname, + output ? "bundled.js", + ... +} @ args: let bundled = deno2nix.mkBundled args; in writeShellScriptBin - "${name}" - "${deno}/bin/deno run ${bundled}/dist/bundled.js" + bin + "${deno}/bin/deno run ${bundled}/dist/${output}" diff --git a/nix/make-bundled.nix b/nix/make-bundled.nix index 5949cea..707c6b3 100644 --- a/nix/make-bundled.nix +++ b/nix/make-bundled.nix @@ -5,43 +5,38 @@ lib, ... }: { - name, + pname, version, src, - entrypoint, lockfile, + output ? "bundled.js", + entrypoint, importMap ? null, - denoFlags ? [], + additionalDenoFlags ? "", }: let inherit (deno2nix.internal) mkDepsLink; in 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 - ]; + inherit pname version entrypoint src; + 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 + deno bundle \ + --lock="${lockfile}" \ + ${ + if importMap != null + then "--import-map=\"$src/${importMap}\"" + else "" + } \ + ${additionalDenoFlags} \ + "$src/${entrypoint}" \ + "${output}" ''; installPhase = '' mkdir -p $out/dist - install -t $out/dist bundled.js + install -t $out/dist "${output}" ''; } diff --git a/nix/make-executable.nix b/nix/make-executable.nix index 916963b..742b3b2 100644 --- a/nix/make-executable.nix +++ b/nix/make-executable.nix @@ -4,39 +4,41 @@ deno2nix, ... }: { - name, + pname, version, src, - entrypoint, lockfile, + output ? pname, + entrypoint, importMap ? null, - denoFlags ? [], + additionalDenoFlags ? "", }: let inherit (deno2nix.internal) mkDepsLink; in 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 = ":"; + inherit pname version src; + dontFixup = true; + buildInputs = with pkgs; [deno jq]; buildPhase = '' export DENO_DIR=`mktemp -d` ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache) - deno compile $denoFlags "$entrypoint" + deno compile \ + --cached-only \ + --lock="${lockfile}" \ + --output="${output}" \ + ${ + if importMap != null + then "--import-map=\"$src/${importMap}\"" + else "" + } \ + ${additionalDenoFlags} \ + "$src/${entrypoint}" ''; + installPhase = '' mkdir -p $out/bin - mv "$name" "$out/bin/" + cp "${output}" "$out/bin/" ''; }