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
dist
# VSCode
**/.vscode/settings.json

View file

@ -3,7 +3,6 @@
"jnoortheen.nix-ide",
"arrterian.nix-env-selector",
"tamasfe.even-better-toml",
"jkillian.custom-local-formatters",
"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
- 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;
bin = "simple";
output = "example";
entrypoint = "./mod.ts";
importMap = "./import_map.json";
};
});
}
```
lockfile = "./deno.lock";
config = "./deno.jsonc";
### `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"
allow = {
all = true;
};
});
}
```

View file

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

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:
{
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";
}
];
};
}
);

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,
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/<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: (
inherit (deno2nix.internal) artifactPath;
in
lockfile: (
linkFarm "deps" (flatten (
mapAttrsToList
(
@ -45,5 +34,4 @@ in {
)
(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,
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 ''

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