From 21efd7ac828c27868ee8d241d092261356fb4248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SnO=E2=82=82WMaN?= Date: Wed, 14 Dec 2022 13:00:56 +0900 Subject: [PATCH] feat: Update for deno2nix v2 (#21) * move simple script to `examples/simple` * rm vscode settings.json * example npm * wip * refactoring * Update mkExecutable * rm custom formatter from vscode extensions * Add .gitignore * Update flake.nix * bundled/executable wip * Update flake * importMap * bundled wrapper * somerefactoring * no more overlay(default) * add todo * Update README.md --- .gitignore | 3 + .vscode/extensions.json | 1 - .vscode/settings.json | 59 --------------- README.md | 52 ++++---------- deno.jsonc => examples/npm/deno.jsonc | 6 +- examples/npm/deno.lock | 15 ++++ examples/npm/import_map.json | 5 ++ examples/npm/mod.ts | 36 ++++++++++ examples/simple/.gitignore | 1 + examples/simple/bundled-wrapper.nix | 11 +++ examples/simple/bundled.nix | 11 +++ examples/simple/deno.jsonc | 20 ++++++ examples/simple/deno.lock | 6 ++ examples/simple/deps-link.nix | 1 + examples/simple/executable.nix | 16 +++++ examples/simple/import_map.json | 5 ++ examples/simple/mod.ts | 32 +++++++++ flake.nix | 68 ++++++------------ import_map.json | 5 -- lock.json | 6 -- mod.ts | 3 - nix/internal/artifact-path.nix | 14 ++++ nix/internal/default.nix | 7 ++ nix/internal/find-import-map.nix | 10 +++ .../mk-deps-link.nix} | 22 ++---- nix/internal/url-part.nix | 11 +++ nix/make-bundled-wrapper.nix | 17 ----- nix/make-executable.nix | 45 ------------ nix/mk-bundled-wrapper.nix | 19 +++++ nix/{make-bundled.nix => mk-bundled.nix} | 39 +++++----- nix/mk-executable.nix | 72 +++++++++++++++++++ nix/overlay.nix | 8 +-- 32 files changed, 364 insertions(+), 262 deletions(-) delete mode 100644 .vscode/settings.json rename deno.jsonc => examples/npm/deno.jsonc (50%) create mode 100644 examples/npm/deno.lock create mode 100644 examples/npm/import_map.json create mode 100644 examples/npm/mod.ts create mode 100644 examples/simple/.gitignore create mode 100644 examples/simple/bundled-wrapper.nix create mode 100644 examples/simple/bundled.nix create mode 100644 examples/simple/deno.jsonc create mode 100644 examples/simple/deno.lock create mode 100644 examples/simple/deps-link.nix create mode 100644 examples/simple/executable.nix create mode 100644 examples/simple/import_map.json create mode 100644 examples/simple/mod.ts delete mode 100644 import_map.json delete mode 100644 lock.json delete mode 100644 mod.ts create mode 100644 nix/internal/artifact-path.nix create mode 100644 nix/internal/default.nix create mode 100644 nix/internal/find-import-map.nix rename nix/{internal.nix => internal/mk-deps-link.nix} (63%) create mode 100644 nix/internal/url-part.nix delete mode 100644 nix/make-bundled-wrapper.nix delete mode 100644 nix/make-executable.nix create mode 100644 nix/mk-bundled-wrapper.nix rename nix/{make-bundled.nix => mk-bundled.nix} (62%) create mode 100644 nix/mk-executable.nix diff --git a/.gitignore b/.gitignore index b8cb981..97f89d7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ result dist + +# VSCode +**/.vscode/settings.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 8b2e71c..7b1b7fd 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -3,7 +3,6 @@ "jnoortheen.nix-ide", "arrterian.nix-env-selector", "tamasfe.even-better-toml", - "jkillian.custom-local-formatters", "denoland.vscode-deno" ] } diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 8595713..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "nixEnvSelector.nixFile": "${workspaceRoot}/shell.nix", - - "deno.enable": true, - "deno.lint": true, - "deno.importMap": "./import_map.json", - "deno.config": "./deno.jsonc", - - "json.schemas": [ - { - "fileMatch": ["deno.jsonc"], - "url": "https://deno.land/x/deno/cli/schemas/config-file.v1.json" - } - ], - - "customLocalFormatters.formatters": [ - { - "command": "treefmt -q --stdin ${file}", - "languages": [ - "json", - "jsonc", - "javascript", - "javascriptreact", - "typescript", - "typescriptreact", - "markdown", - "nix", - "toml" - ] - } - ], - "[toml]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - }, - "[nix]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - }, - "[markdown]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - }, - "[json]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - }, - "[jsonc]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - }, - "[javascript]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - }, - "[javascriptreact]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - }, - "[typescript]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - }, - "[typescriptreact]": { - "editor.defaultFormatter": "jkillian.custom-local-formatters" - } -} diff --git a/README.md b/README.md index 13b2c6b..023a574 100644 --- a/README.md +++ b/README.md @@ -4,17 +4,7 @@ ## Usage -- lockfile -> `./lock.json` -- import map -> `./import_map.json` -- entrypoint -> `./mod.ts` - -### Update `lock.json` for caching - -```bash -deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts -``` - -### Setup for nix flake (example) +There is a [sample project](/examples/simple). ```nix { @@ -33,39 +23,25 @@ deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts inherit system; overlays = with inputs; [ devshell.overlay - deno2nix.overlay + deno2nix.overlays.default ]; }; in { packages.executable = deno2nix.mkExecutable { - pname = "example-executable"; - version = "0.1.2"; - + pname = "simple-executable"; + version = "0.1.0"; + src = ./.; - lockfile = ./lock.json; - - output = "example"; + bin = "simple"; + entrypoint = "./mod.ts"; - importMap = "./import_map.json"; - }; - }); -} -``` - -### `deno2nix.mkExecutable` - -#### Args - -```nix -{ - pname, - version, - src, - lockfile, - output ? pname, # generate binary name - entrypoint, - importMap ? null, # ex. "./import_map.json" to $src/${importMap} - additionalDenoFlags ? "", # ex. "--allow-net" + lockfile = "./deno.lock"; + config = "./deno.jsonc"; + + allow = { + all = true; + }; + }); } ``` diff --git a/deno.jsonc b/examples/npm/deno.jsonc similarity index 50% rename from deno.jsonc rename to examples/npm/deno.jsonc index 7c4b050..0297cef 100644 --- a/deno.jsonc +++ b/examples/npm/deno.jsonc @@ -1,8 +1,8 @@ { + "importMap": "./import_map.json", "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" + "run": "deno run -A ./mod.ts", + "compile": "deno compile -A ./mod.ts" }, "fmt": { "options": { diff --git a/examples/npm/deno.lock b/examples/npm/deno.lock new file mode 100644 index 0000000..9c68eeb --- /dev/null +++ b/examples/npm/deno.lock @@ -0,0 +1,15 @@ +{ + "version": "2", + "remote": { + "https://deno.land/std@0.118.0/fmt/colors.ts": "8368ddf2d48dfe413ffd04cdbb7ae6a1009cf0dccc9c7ff1d76259d9c61a0621" + }, + "npm": { + "specifiers": { "chalk": "chalk@5.2.0" }, + "packages": { + "chalk@5.2.0": { + "integrity": "sha512-ree3Gqw/nazQAPuJJEy+avdl7QfZMcUvmHIKgEZkGL+xOBzRvup5Hxo6LHuMceSxOabuJLJm5Yp/92R9eMmMvA==", + "dependencies": {} + } + } + } +} diff --git a/examples/npm/import_map.json b/examples/npm/import_map.json new file mode 100644 index 0000000..a2827b1 --- /dev/null +++ b/examples/npm/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "chalk": "npm:chalk" + } +} diff --git a/examples/npm/mod.ts b/examples/npm/mod.ts new file mode 100644 index 0000000..c189dc1 --- /dev/null +++ b/examples/npm/mod.ts @@ -0,0 +1,36 @@ +import chalk from "chalk"; + +console.log(`Hello!`); +console.log( + `This script was written for ${chalk.bgBlack(chalk.white("deno"))}${chalk.black("2")}${ + chalk.rgb(126, 126, 255)("Nix") + } with npm ${chalk.italic("chalk")} module.`, +); + +// https://github.com/dylanaraps/neofetch/blob/ccd5d9f52609bbdcd5d8fa78c4fdb0f12954125f/neofetch#L8976-L8998 +const c1 = (t: string) => chalk.rgb(71, 111, 182)(t); +const c2 = (t: string) => chalk.rgb(126, 182, 225)(t); +console.log( + [ + c1(" ▗▄▄▄ ") + c2("▗▄▄▄▄ ▄▄▄▖"), + c1(" ▜███▙ ") + c2("▜███▙ ▟███▛"), + c1(" ▜███▙ ") + c2("▜███▙▟███▛"), + c1(" ▜███▙ ") + c2("▜██████▛"), + c1(" ▟█████████████████▙ ") + c2("▜████▛ ") + c1("▟▙"), + c1(" ▟███████████████████▙ ") + c2("▜███▙ ") + c1("▟██▙"), + c2(" ▄▄▄▄▖ ") + c2("▜███▙ ") + c1("▟███▛"), + c2(" ▟███▛ ") + c2("▜██▛ ") + c1("▟███▛"), + c2(" ▟███▛ ") + c2("▜▛ ") + c1("▟███▛"), + c2("▟███████████▛ ") + c1("▟██████████▙"), + c2("▜██████████▛ ") + c1("▟███████████▛"), + c2(" ▟███▛") + c1(" ▟▙ ") + c1("▟███▛"), + c2(" ▟███▛") + c1(" ▟██▙ ") + c1("▟███▛"), + c2(" ▟███▛") + c1(" ▜███▙ ") + c1("▝▀▀▀▀"), + c2(" ▜██▛") + c1(" ▜███▙ ") + c2("▜██████████████████▛"), + c2(" ▜▛") + c1(" ▟████▙ ") + c2("▜████████████████▛"), + c1(" ▟██████▙ ") + c2("▜███▙"), + c1(" ▟███▛▜███▙ ") + c2("▜███▙"), + c1(" ▟███▛ ▜███▙ ") + c2("▜███▙"), + c1(" ▝▀▀▀ ▀▀▀▀▘ ") + c2("▀▀▀▘"), + ].join("\n"), +); diff --git a/examples/simple/.gitignore b/examples/simple/.gitignore new file mode 100644 index 0000000..ab23474 --- /dev/null +++ b/examples/simple/.gitignore @@ -0,0 +1 @@ +simple diff --git a/examples/simple/bundled-wrapper.nix b/examples/simple/bundled-wrapper.nix new file mode 100644 index 0000000..bd0d49f --- /dev/null +++ b/examples/simple/bundled-wrapper.nix @@ -0,0 +1,11 @@ +{deno2nix}: +deno2nix.mkBundledWrapper { + pname = "simple-bundled"; + version = "0.1.0"; + + src = ./.; + bin = "simple"; + + entrypoint = "./mod.ts"; + lockfile = "./deno.lock"; +} diff --git a/examples/simple/bundled.nix b/examples/simple/bundled.nix new file mode 100644 index 0000000..6d87220 --- /dev/null +++ b/examples/simple/bundled.nix @@ -0,0 +1,11 @@ +{deno2nix}: +deno2nix.mkBundled { + pname = "simple-bundled"; + version = "0.1.0"; + + src = ./.; + output = "bundled.js"; + + entrypoint = "./mod.ts"; + lockfile = "./deno.lock"; +} diff --git a/examples/simple/deno.jsonc b/examples/simple/deno.jsonc new file mode 100644 index 0000000..6599b37 --- /dev/null +++ b/examples/simple/deno.jsonc @@ -0,0 +1,20 @@ +{ + "importMap": "./import_map.json", + "tasks": { + "run": "deno run ./mod.ts", + "compile": "deno compile ./mod.ts" + }, + "fmt": { + "options": { + "lineWidth": 120, + "indentWidth": 2, + "singleQuote": false, + "useTabs": false + }, + "files": { + "exclude": [ + "./lock.json" + ] + } + } +} diff --git a/examples/simple/deno.lock b/examples/simple/deno.lock new file mode 100644 index 0000000..21e2bf7 --- /dev/null +++ b/examples/simple/deno.lock @@ -0,0 +1,6 @@ +{ + "version": "2", + "remote": { + "https://deno.land/std@0.167.0/fmt/colors.ts": "03ad95e543d2808bc43c17a3dd29d25b43d0f16287fe562a0be89bf632454a12" + } +} diff --git a/examples/simple/deps-link.nix b/examples/simple/deps-link.nix new file mode 100644 index 0000000..57c301f --- /dev/null +++ b/examples/simple/deps-link.nix @@ -0,0 +1 @@ +{deno2nix}: deno2nix.internal.mkDepsLink ./deno.lock diff --git a/examples/simple/executable.nix b/examples/simple/executable.nix new file mode 100644 index 0000000..fe72e10 --- /dev/null +++ b/examples/simple/executable.nix @@ -0,0 +1,16 @@ +{deno2nix}: +deno2nix.mkExecutable { + pname = "simple-executable"; + version = "0.1.0"; + + src = ./.; + bin = "simple"; + + entrypoint = "./mod.ts"; + lockfile = "./deno.lock"; + config = "./deno.jsonc"; + + allow = { + all = true; + }; +} diff --git a/examples/simple/import_map.json b/examples/simple/import_map.json new file mode 100644 index 0000000..249c7ef --- /dev/null +++ b/examples/simple/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "std/": "https://deno.land/std@0.167.0/" + } +} diff --git a/examples/simple/mod.ts b/examples/simple/mod.ts new file mode 100644 index 0000000..4290006 --- /dev/null +++ b/examples/simple/mod.ts @@ -0,0 +1,32 @@ +import { bgBlack, black, rgb24, white } from "std/fmt/colors.ts"; + +console.log(`Hello!`); +console.log(`This script was written for ${bgBlack(white("deno"))}${black("2")}${rgb24("Nix", 0x7E7EFF)}.`); + +// https://github.com/dylanaraps/neofetch/blob/ccd5d9f52609bbdcd5d8fa78c4fdb0f12954125f/neofetch#L8976-L8998 +const c1 = (t: string) => rgb24(t, 0x4D6FB6); +const c2 = (t: string) => rgb24(t, 0x7EB6E1); +console.log( + [ + c1(" ▗▄▄▄ ") + c2("▗▄▄▄▄ ▄▄▄▖"), + c1(" ▜███▙ ") + c2("▜███▙ ▟███▛"), + c1(" ▜███▙ ") + c2("▜███▙▟███▛"), + c1(" ▜███▙ ") + c2("▜██████▛"), + c1(" ▟█████████████████▙ ") + c2("▜████▛ ") + c1("▟▙"), + c1(" ▟███████████████████▙ ") + c2("▜███▙ ") + c1("▟██▙"), + c2(" ▄▄▄▄▖ ") + c2("▜███▙ ") + c1("▟███▛"), + c2(" ▟███▛ ") + c2("▜██▛ ") + c1("▟███▛"), + c2(" ▟███▛ ") + c2("▜▛ ") + c1("▟███▛"), + c2("▟███████████▛ ") + c1("▟██████████▙"), + c2("▜██████████▛ ") + c1("▟███████████▛"), + c2(" ▟███▛") + c1(" ▟▙ ") + c1("▟███▛"), + c2(" ▟███▛") + c1(" ▟██▙ ") + c1("▟███▛"), + c2(" ▟███▛") + c1(" ▜███▙ ") + c1("▝▀▀▀▀"), + c2(" ▜██▛") + c1(" ▜███▙ ") + c2("▜██████████████████▛"), + c2(" ▜▛") + c1(" ▟████▙ ") + c2("▜████████████████▛"), + c1(" ▟██████▙ ") + c2("▜███▙"), + c1(" ▟███▛▜███▙ ") + c2("▜███▙"), + c1(" ▟███▛ ▜███▙ ") + c2("▜███▙"), + c1(" ▝▀▀▀ ▀▀▀▀▘ ") + c2("▀▀▀▘"), + ].join("\n"), +); diff --git a/flake.nix b/flake.nix index 478ae18..fbe966b 100644 --- a/flake.nix +++ b/flake.nix @@ -26,7 +26,6 @@ } @ inputs: { overlays.default = import ./nix/overlay.nix; - overlay = self.overlays.default; } // flake-utils.lib.eachSystem [ "x86_64-linux" @@ -45,50 +44,33 @@ ]; }; in { - packages.depslink = deno2nix.internal.mkDepsLink ./lock.json; - packages.bundled = deno2nix.mkBundled { - pname = "example-bundled"; - version = "0.1.0"; - - src = ./.; - lockfile = ./lock.json; - - output = "bundled.js"; - entrypoint = "./mod.ts"; - importMap = "./import_map.json"; - minify = true; + /* + TODO: It can't but I don't why + packages = flake-utils.lib.flattenTree { + simple = { + deps-link = pkgs.callPackage ./examples/simple/deps-link.nix {}; + executable = pkgs.callPackage ./examples/simple/executable.nix {}; + }; }; - packages.bundled-wrapper = deno2nix.mkBundledWrapper { - pname = "example-bundled-wrapper"; - version = "0.1.0"; - - src = ./.; - lockfile = ./lock.json; - - output = "bundled.js"; - entrypoint = "./mod.ts"; - importMap = "./import_map.json"; + */ + packages = { + "simple/deps-link" = pkgs.callPackage ./examples/simple/deps-link.nix {}; + "simple/bundled" = pkgs.callPackage ./examples/simple/bundled.nix {}; + "simple/bundled-wrapper" = pkgs.callPackage ./examples/simple/bundled-wrapper.nix {}; + "simple/executable" = pkgs.callPackage ./examples/simple/executable.nix {}; }; - packages.executable = deno2nix.mkExecutable { - pname = "example-executable"; - version = "0.1.2"; - - src = ./.; - lockfile = ./lock.json; - - output = "example"; - entrypoint = "./mod.ts"; - importMap = "./import_map.json"; + apps = { + "simple/executable" = flake-utils.lib.mkApp { + drv = self.packages.${system}."simple/executable"; + name = "simple"; + }; + "simple/bundled-wrapper" = flake-utils.lib.mkApp { + drv = self.packages.${system}."simple/bundled-wrapper"; + name = "simple"; + }; }; - packages.default = self.packages.${system}.executable; - defaultPackage = self.packages.${system}.default; - - 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}; - devShells.default = pkgs.devshell.mkShell { packages = with pkgs; [ alejandra @@ -96,12 +78,6 @@ treefmt taplo-cli ]; - commands = [ - { - package = "treefmt"; - category = "formatters"; - } - ]; }; } ); diff --git a/import_map.json b/import_map.json deleted file mode 100644 index 6db072a..0000000 --- a/import_map.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "imports": { - "std/": "https://deno.land/std@0.118.0/" - } -} diff --git a/lock.json b/lock.json deleted file mode 100644 index 4d853b5..0000000 --- a/lock.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "version": "2", - "remote": { - "https://deno.land/std@0.118.0/fmt/colors.ts": "8368ddf2d48dfe413ffd04cdbb7ae6a1009cf0dccc9c7ff1d76259d9c61a0621" - } -} diff --git a/mod.ts b/mod.ts deleted file mode 100644 index f1f0981..0000000 --- a/mod.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { green, italic } from "std/fmt/colors.ts"; - -console.log(`Hello ${italic(green("World"))}!`); diff --git a/nix/internal/artifact-path.nix b/nix/internal/artifact-path.nix new file mode 100644 index 0000000..ae116d5 --- /dev/null +++ b/nix/internal/artifact-path.nix @@ -0,0 +1,14 @@ +{ + lib, + deno2nix, + ... +}: let + inherit (builtins) hashString; + inherit (deno2nix.internal) urlPart; +in + # input: https://deno.land/std@0.118.0/fmt/colors.ts + # + # output: https/deno.land/ + url: let + up = urlPart url; + in "${up 0}/${up 1}/${hashString "sha256" (up 2)}" diff --git a/nix/internal/default.nix b/nix/internal/default.nix new file mode 100644 index 0000000..88392e9 --- /dev/null +++ b/nix/internal/default.nix @@ -0,0 +1,7 @@ +{pkgs, ...}: { + urlPart = pkgs.callPackage ./url-part.nix {}; + artifactPath = pkgs.callPackage ./artifact-path.nix {}; + mkDepsLink = pkgs.callPackage ./mk-deps-link.nix {}; + findImportMap = + pkgs.callPackage ./find-import-map.nix {}; +} diff --git a/nix/internal/find-import-map.nix b/nix/internal/find-import-map.nix new file mode 100644 index 0000000..b1e9058 --- /dev/null +++ b/nix/internal/find-import-map.nix @@ -0,0 +1,10 @@ +{lib, ...}: let + inherit (lib) importJSON; +in + { + src, + config, + importMap, + }: + # TODO: if importMap is exists, fallback + ((importJSON (src + "/${config}")).importMap) diff --git a/nix/internal.nix b/nix/internal/mk-deps-link.nix similarity index 63% rename from nix/internal.nix rename to nix/internal/mk-deps-link.nix index 8f6cbc9..935bbf0 100644 --- a/nix/internal.nix +++ b/nix/internal/mk-deps-link.nix @@ -1,27 +1,16 @@ { - pkgs, lib, linkFarm, writeText, + deno2nix, ... }: let inherit (builtins) split elemAt fetchurl toJSON hashString baseNameOf; inherit (lib) flatten mapAttrsToList importJSON; inherit (lib.strings) sanitizeDerivationName; - - # https://deno.land/std@0.118.0/fmt/colors.ts - # - # 0. "https" - # 1. "deno.land" - # 2. "/std@0.118.0/fmt/colors.ts" - urlPart = url: elemAt (flatten (split "://([a-z0-9\.]*)" url)); - - # https://deno.land/std@0.118.0/fmt/colors.ts - # - # https/deno.land/ - artifactPath = url: let up = urlPart url; in "${up 0}/${up 1}/${hashString "sha256" (up 2)}"; -in { - mkDepsLink = lockfile: ( + inherit (deno2nix.internal) artifactPath; +in + lockfile: ( linkFarm "deps" (flatten ( mapAttrsToList ( @@ -45,5 +34,4 @@ in { ) (importJSON lockfile).remote )) - ); -} + ) diff --git a/nix/internal/url-part.nix b/nix/internal/url-part.nix new file mode 100644 index 0000000..cdc7a69 --- /dev/null +++ b/nix/internal/url-part.nix @@ -0,0 +1,11 @@ +{lib, ...}: let + inherit (builtins) split elemAt; + inherit (lib) flatten; +in + # input: https://deno.land/std@0.118.0/fmt/colors.ts + # + # output: + # 0. "https" + # 1. "deno.land" + # 2. "/std@0.118.0/fmt/colors.ts" + url: elemAt (flatten (split "://([a-z0-9\.]*)" url)) diff --git a/nix/make-bundled-wrapper.nix b/nix/make-bundled-wrapper.nix deleted file mode 100644 index 0928bb1..0000000 --- a/nix/make-bundled-wrapper.nix +++ /dev/null @@ -1,17 +0,0 @@ -{ - pkgs, - deno, - deno2nix, - writeShellScriptBin, - ... -}: { - pname, - bin ? pname, - output ? "bundled.js", - ... -} @ args: let - bundled = deno2nix.mkBundled args; -in - writeShellScriptBin - bin - "${deno}/bin/deno run ${bundled}/dist/${output}" diff --git a/nix/make-executable.nix b/nix/make-executable.nix deleted file mode 100644 index 84244d5..0000000 --- a/nix/make-executable.nix +++ /dev/null @@ -1,45 +0,0 @@ -{ - pkgs, - stdenv, - deno2nix, - ... -}: { - pname, - version, - src, - lockfile, - output ? pname, - entrypoint, - importMap ? null, - additionalDenoFlags ? "", -}: let - inherit (deno2nix.internal) mkDepsLink; -in - stdenv.mkDerivation { - inherit pname version src; - dontFixup = true; - - buildInputs = with pkgs; [deno jq]; - buildPhase = '' - export DENO_DIR="/tmp/deno2nix" - mkdir -p $DENO_DIR - ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache) - - 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 - cp "${output}" "$out/bin/" - ''; - } diff --git a/nix/mk-bundled-wrapper.nix b/nix/mk-bundled-wrapper.nix new file mode 100644 index 0000000..723ab5d --- /dev/null +++ b/nix/mk-bundled-wrapper.nix @@ -0,0 +1,19 @@ +{ + lib, + deno, + deno2nix, + writeShellScriptBin, + ... +}: { + pname, + bin ? pname, + output ? "${pname}.bundled.js", + outPath ? "dist", + ... +} @ args: let + inherit (lib) filterAttrs; + bundled = deno2nix.mkBundled (filterAttrs (n: v: n != "bin") args); +in + writeShellScriptBin + bin + "${deno}/bin/deno run ${bundled}/${outPath}/${output}" diff --git a/nix/make-bundled.nix b/nix/mk-bundled.nix similarity index 62% rename from nix/make-bundled.nix rename to nix/mk-bundled.nix index 5b73da8..67aa67a 100644 --- a/nix/make-bundled.nix +++ b/nix/mk-bundled.nix @@ -1,22 +1,36 @@ { pkgs, + lib, stdenv, deno2nix, - lib, ... }: { pname, version, src, - lockfile, - output ? "bundled.js", + output ? "${pname}.bundled.js", outPath ? "dist", - minify ? false, entrypoint, - importMap ? null, + lockfile, + minify ? false, additionalDenoFlags ? "", }: let + inherit (builtins) isString; + inherit (lib.strings) concatStringsSep; inherit (deno2nix.internal) mkDepsLink; + + bundleCmd = concatStringsSep " " ( + [ + "deno bundle" + "--lock=${lockfile}" + # "--config=${config}" + ] + ++ [additionalDenoFlags] + ++ [ + "${entrypoint}" + "${output}" + ] + ); in stdenv.mkDerivation { inherit pname version entrypoint src; @@ -25,19 +39,8 @@ in buildPhase = '' export DENO_DIR="/tmp/deno2nix" mkdir -p $DENO_DIR - ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache) - - deno bundle \ - --lock="${lockfile}" \ - ${ - if importMap != null - then "--import-map=\"$src/${importMap}\"" - else "" - } \ - ${additionalDenoFlags} \ - "$src/${entrypoint}" \ - "${output}" - + ln -s "${mkDepsLink (src + "/${lockfile}")}" $(deno info --json | jq -r .modulesCache) + ${bundleCmd} ${ if minify then '' diff --git a/nix/mk-executable.nix b/nix/mk-executable.nix new file mode 100644 index 0000000..3218a94 --- /dev/null +++ b/nix/mk-executable.nix @@ -0,0 +1,72 @@ +{ + pkgs, + lib, + stdenv, + deno2nix, + ... +}: { + pname, + version, + src, + bin ? pname, + entrypoint, + lockfile, + config, + allow ? {}, + additionalDenoFlags ? "", +} @ inputs: let + inherit (builtins) isString; + inherit (lib) importJSON concatStringsSep; + inherit (deno2nix.internal) mkDepsLink findImportMap; + + allowflag = flag: ( + if (allow ? flag) && allow."${flag}" + then ["--allow-${flag}"] + else [] + ); + + importMap = findImportMap { + inherit (inputs) src config importMap; + }; + + compileCmd = concatStringsSep " " ( + [ + "deno compile --cached-only" + "--lock=${lockfile}" + "--output=${bin}" + # "--config=${config}" + ] + ++ ( + if (isString importMap) + then ["--import-map=${importMap}"] + else [] + ) + ++ (allowflag "all") + ++ (allowflag "env") + ++ (allowflag "ffi") + ++ (allowflag "hrtime") + ++ (allowflag "net") + ++ (allowflag "read") + ++ (allowflag "run") + ++ (allowflag "sys") + ++ (allowflag "write") + ++ [additionalDenoFlags] + ++ ["${entrypoint}"] + ); +in + stdenv.mkDerivation { + inherit pname version src; + dontFixup = true; + + buildInputs = with pkgs; [deno jq]; + buildPhase = '' + export DENO_DIR="/tmp/deno2nix" + mkdir -p $DENO_DIR + ln -s "${mkDepsLink (src + "/${lockfile}")}" $(deno info --json | jq -r .modulesCache) + ${compileCmd} + ''; + installPhase = '' + mkdir -p $out/bin + cp "${bin}" "$out/bin/" + ''; + } diff --git a/nix/overlay.nix b/nix/overlay.nix index fdc0158..9e97066 100644 --- a/nix/overlay.nix +++ b/nix/overlay.nix @@ -1,9 +1,9 @@ final: prev: { deno2nix = { - mkBundled = final.callPackage ./make-bundled.nix {}; - mkBundledWrapper = final.callPackage ./make-bundled-wrapper.nix {}; - mkExecutable = final.callPackage ./make-executable.nix {}; + mkBundled = final.callPackage ./mk-bundled.nix {}; + mkBundledWrapper = final.callPackage ./mk-bundled-wrapper.nix {}; + mkExecutable = final.callPackage ./mk-executable.nix {}; - internal = final.callPackage ./internal.nix {}; + internal = final.callPackage ./internal {}; }; }