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
This commit is contained in:
SnO₂WMaN 2022-12-14 13:00:56 +09:00 committed by GitHub
parent 9f8afeffb2
commit 21efd7ac82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 364 additions and 262 deletions

3
.gitignore vendored
View file

@ -2,3 +2,6 @@
result result
dist dist
# VSCode
**/.vscode/settings.json

View file

@ -3,7 +3,6 @@
"jnoortheen.nix-ide", "jnoortheen.nix-ide",
"arrterian.nix-env-selector", "arrterian.nix-env-selector",
"tamasfe.even-better-toml", "tamasfe.even-better-toml",
"jkillian.custom-local-formatters",
"denoland.vscode-deno" "denoland.vscode-deno"
] ]
} }

59
.vscode/settings.json vendored
View file

@ -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"
}
}

View file

@ -4,17 +4,7 @@
## Usage ## Usage
- lockfile -> `./lock.json` There is a [sample project](/examples/simple).
- 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)
```nix ```nix
{ {
@ -33,39 +23,25 @@ deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts
inherit system; inherit system;
overlays = with inputs; [ overlays = with inputs; [
devshell.overlay devshell.overlay
deno2nix.overlay deno2nix.overlays.default
]; ];
}; };
in { in {
packages.executable = deno2nix.mkExecutable { packages.executable = deno2nix.mkExecutable {
pname = "example-executable"; pname = "simple-executable";
version = "0.1.2"; version = "0.1.0";
src = ./.; src = ./.;
lockfile = ./lock.json; bin = "simple";
output = "example";
entrypoint = "./mod.ts"; entrypoint = "./mod.ts";
importMap = "./import_map.json"; lockfile = "./deno.lock";
}; config = "./deno.jsonc";
});
} allow = {
``` all = true;
};
### `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"
} }
``` ```

View file

@ -1,8 +1,8 @@
{ {
"importMap": "./import_map.json",
"tasks": { "tasks": {
"run": "deno run --import-map=./import_map.json ./mod.ts", "run": "deno run -A ./mod.ts",
"compile": "deno compile --import-map=./import_map.json ./mod.ts", "compile": "deno compile -A ./mod.ts"
"cache": "deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts"
}, },
"fmt": { "fmt": {
"options": { "options": {

15
examples/npm/deno.lock Normal file
View file

@ -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": {}
}
}
}
}

View file

@ -0,0 +1,5 @@
{
"imports": {
"chalk": "npm:chalk"
}
}

36
examples/npm/mod.ts Normal file
View file

@ -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"),
);

1
examples/simple/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
simple

View file

@ -0,0 +1,11 @@
{deno2nix}:
deno2nix.mkBundledWrapper {
pname = "simple-bundled";
version = "0.1.0";
src = ./.;
bin = "simple";
entrypoint = "./mod.ts";
lockfile = "./deno.lock";
}

View file

@ -0,0 +1,11 @@
{deno2nix}:
deno2nix.mkBundled {
pname = "simple-bundled";
version = "0.1.0";
src = ./.;
output = "bundled.js";
entrypoint = "./mod.ts";
lockfile = "./deno.lock";
}

View file

@ -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"
]
}
}
}

View file

@ -0,0 +1,6 @@
{
"version": "2",
"remote": {
"https://deno.land/std@0.167.0/fmt/colors.ts": "03ad95e543d2808bc43c17a3dd29d25b43d0f16287fe562a0be89bf632454a12"
}
}

View file

@ -0,0 +1 @@
{deno2nix}: deno2nix.internal.mkDepsLink ./deno.lock

View file

@ -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;
};
}

View file

@ -0,0 +1,5 @@
{
"imports": {
"std/": "https://deno.land/std@0.167.0/"
}
}

32
examples/simple/mod.ts Normal file
View file

@ -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"),
);

View file

@ -26,7 +26,6 @@
} @ inputs: } @ inputs:
{ {
overlays.default = import ./nix/overlay.nix; overlays.default = import ./nix/overlay.nix;
overlay = self.overlays.default;
} }
// flake-utils.lib.eachSystem [ // flake-utils.lib.eachSystem [
"x86_64-linux" "x86_64-linux"
@ -45,50 +44,33 @@
]; ];
}; };
in { in {
packages.depslink = deno2nix.internal.mkDepsLink ./lock.json; /*
packages.bundled = deno2nix.mkBundled { TODO: It can't but I don't why
pname = "example-bundled"; packages = flake-utils.lib.flattenTree {
version = "0.1.0"; simple = {
deps-link = pkgs.callPackage ./examples/simple/deps-link.nix {};
src = ./.; executable = pkgs.callPackage ./examples/simple/executable.nix {};
lockfile = ./lock.json; };
output = "bundled.js";
entrypoint = "./mod.ts";
importMap = "./import_map.json";
minify = true;
}; };
packages.bundled-wrapper = deno2nix.mkBundledWrapper { */
pname = "example-bundled-wrapper"; packages = {
version = "0.1.0"; "simple/deps-link" = pkgs.callPackage ./examples/simple/deps-link.nix {};
"simple/bundled" = pkgs.callPackage ./examples/simple/bundled.nix {};
src = ./.; "simple/bundled-wrapper" = pkgs.callPackage ./examples/simple/bundled-wrapper.nix {};
lockfile = ./lock.json; "simple/executable" = pkgs.callPackage ./examples/simple/executable.nix {};
output = "bundled.js";
entrypoint = "./mod.ts";
importMap = "./import_map.json";
}; };
packages.executable = deno2nix.mkExecutable { apps = {
pname = "example-executable"; "simple/executable" = flake-utils.lib.mkApp {
version = "0.1.2"; drv = self.packages.${system}."simple/executable";
name = "simple";
src = ./.; };
lockfile = ./lock.json; "simple/bundled-wrapper" = flake-utils.lib.mkApp {
drv = self.packages.${system}."simple/bundled-wrapper";
output = "example"; name = "simple";
entrypoint = "./mod.ts"; };
importMap = "./import_map.json";
}; };
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}; checks = self.packages.${system};
devShells.default = pkgs.devshell.mkShell { devShells.default = pkgs.devshell.mkShell {
packages = with pkgs; [ packages = with pkgs; [
alejandra alejandra
@ -96,12 +78,6 @@
treefmt treefmt
taplo-cli taplo-cli
]; ];
commands = [
{
package = "treefmt";
category = "formatters";
}
];
}; };
} }
); );

View file

@ -1,5 +0,0 @@
{
"imports": {
"std/": "https://deno.land/std@0.118.0/"
}
}

View file

@ -1,6 +0,0 @@
{
"version": "2",
"remote": {
"https://deno.land/std@0.118.0/fmt/colors.ts": "8368ddf2d48dfe413ffd04cdbb7ae6a1009cf0dccc9c7ff1d76259d9c61a0621"
}
}

3
mod.ts
View file

@ -1,3 +0,0 @@
import { green, italic } from "std/fmt/colors.ts";
console.log(`Hello ${italic(green("World"))}!`);

View file

@ -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/<sha256 "/std@0.118.0/fmt/colors.ts">
url: let
up = urlPart url;
in "${up 0}/${up 1}/${hashString "sha256" (up 2)}"

7
nix/internal/default.nix Normal file
View file

@ -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 {};
}

View file

@ -0,0 +1,10 @@
{lib, ...}: let
inherit (lib) importJSON;
in
{
src,
config,
importMap,
}:
# TODO: if importMap is exists, fallback
((importJSON (src + "/${config}")).importMap)

View file

@ -1,27 +1,16 @@
{ {
pkgs,
lib, lib,
linkFarm, linkFarm,
writeText, writeText,
deno2nix,
... ...
}: let }: let
inherit (builtins) split elemAt fetchurl toJSON hashString baseNameOf; inherit (builtins) split elemAt fetchurl toJSON hashString baseNameOf;
inherit (lib) flatten mapAttrsToList importJSON; inherit (lib) flatten mapAttrsToList importJSON;
inherit (lib.strings) sanitizeDerivationName; inherit (lib.strings) sanitizeDerivationName;
inherit (deno2nix.internal) artifactPath;
# https://deno.land/std@0.118.0/fmt/colors.ts in
# lockfile: (
# 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/<sha256 "/std@0.118.0/fmt/colors.ts">
artifactPath = url: let up = urlPart url; in "${up 0}/${up 1}/${hashString "sha256" (up 2)}";
in {
mkDepsLink = lockfile: (
linkFarm "deps" (flatten ( linkFarm "deps" (flatten (
mapAttrsToList mapAttrsToList
( (
@ -45,5 +34,4 @@ in {
) )
(importJSON lockfile).remote (importJSON lockfile).remote
)) ))
); )
}

11
nix/internal/url-part.nix Normal file
View file

@ -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))

View file

@ -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}"

View file

@ -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/"
'';
}

View file

@ -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}"

View file

@ -1,22 +1,36 @@
{ {
pkgs, pkgs,
lib,
stdenv, stdenv,
deno2nix, deno2nix,
lib,
... ...
}: { }: {
pname, pname,
version, version,
src, src,
lockfile, output ? "${pname}.bundled.js",
output ? "bundled.js",
outPath ? "dist", outPath ? "dist",
minify ? false,
entrypoint, entrypoint,
importMap ? null, lockfile,
minify ? false,
additionalDenoFlags ? "", additionalDenoFlags ? "",
}: let }: let
inherit (builtins) isString;
inherit (lib.strings) concatStringsSep;
inherit (deno2nix.internal) mkDepsLink; inherit (deno2nix.internal) mkDepsLink;
bundleCmd = concatStringsSep " " (
[
"deno bundle"
"--lock=${lockfile}"
# "--config=${config}"
]
++ [additionalDenoFlags]
++ [
"${entrypoint}"
"${output}"
]
);
in in
stdenv.mkDerivation { stdenv.mkDerivation {
inherit pname version entrypoint src; inherit pname version entrypoint src;
@ -25,19 +39,8 @@ in
buildPhase = '' buildPhase = ''
export DENO_DIR="/tmp/deno2nix" export DENO_DIR="/tmp/deno2nix"
mkdir -p $DENO_DIR mkdir -p $DENO_DIR
ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache) ln -s "${mkDepsLink (src + "/${lockfile}")}" $(deno info --json | jq -r .modulesCache)
${bundleCmd}
deno bundle \
--lock="${lockfile}" \
${
if importMap != null
then "--import-map=\"$src/${importMap}\""
else ""
} \
${additionalDenoFlags} \
"$src/${entrypoint}" \
"${output}"
${ ${
if minify if minify
then '' then ''

72
nix/mk-executable.nix Normal file
View file

@ -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/"
'';
}

View file

@ -1,9 +1,9 @@
final: prev: { final: prev: {
deno2nix = { deno2nix = {
mkBundled = final.callPackage ./make-bundled.nix {}; mkBundled = final.callPackage ./mk-bundled.nix {};
mkBundledWrapper = final.callPackage ./make-bundled-wrapper.nix {}; mkBundledWrapper = final.callPackage ./mk-bundled-wrapper.nix {};
mkExecutable = final.callPackage ./make-executable.nix {}; mkExecutable = final.callPackage ./mk-executable.nix {};
internal = final.callPackage ./internal.nix {}; internal = final.callPackage ./internal {};
}; };
} }