From af11612750e8f2670b4ae7c8291df32de5b4431e Mon Sep 17 00:00:00 2001 From: SnO2WMaN Date: Sun, 19 Jun 2022 10:24:56 +0900 Subject: [PATCH] setup env --- .editorconfig | 29 +++++++++ .envrc | 2 + .gitignore | 4 ++ .vscode/extensions.json | 9 +++ .vscode/settings.json | 59 +++++++++++++++++++ default.nix | 14 +++++ deno.jsonc | 18 ++++++ devshell.toml | 10 ++++ flake.lock | 111 +++++++++++++++++++++++++++++++++++ flake.nix | 73 +++++++++++++++++++++++ import_map.json | 5 ++ lock.json | 3 + mod.ts | 3 + nix/default.nix | 126 ++++++++++++++++++++++++++++++++++++++++ overlay.nix | 5 ++ shell.nix | 14 +++++ treefmt.toml | 13 +++++ 17 files changed, 498 insertions(+) create mode 100644 .editorconfig create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 default.nix create mode 100644 deno.jsonc create mode 100644 devshell.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 import_map.json create mode 100644 lock.json create mode 100644 mod.ts create mode 100644 nix/default.nix create mode 100644 overlay.nix create mode 100644 shell.nix create mode 100644 treefmt.toml diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..825cdec --- /dev/null +++ b/.editorconfig @@ -0,0 +1,29 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false + +[*.yml] +indent_style = space +indent_size = 2 + +[*.json] +indent_size = 2 + +[*.toml] +indent_size = 2 + +[*.py] +indent_style = tab +indent_size = 4 + +[*.nix] +indent_size = 2 diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..af0cc93 --- /dev/null +++ b/.envrc @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8cb981 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.direnv/ + +result +dist diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..8b2e71c --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,9 @@ +{ + "recommendations": [ + "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 new file mode 100644 index 0000000..8818bd1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "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", + "md", + "nix", + "toml" + ] + } + ], + "[toml]": { + "editor.defaultFormatter": "jkillian.custom-local-formatters" + }, + "[nix]": { + "editor.defaultFormatter": "jkillian.custom-local-formatters" + }, + "[md]": { + "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/default.nix b/default.nix new file mode 100644 index 0000000..73e34fc --- /dev/null +++ b/default.nix @@ -0,0 +1,14 @@ +(import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball (with lock.nodes.flake-compat.locked; { + url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"; + sha256 = narHash; + }) + ) + { + src = ./.; + }) +.defaultNix diff --git a/deno.jsonc b/deno.jsonc new file mode 100644 index 0000000..de06573 --- /dev/null +++ b/deno.jsonc @@ -0,0 +1,18 @@ +{ + "tasks": { + "run": "deno run ./mod.ts" + }, + "fmt": { + "options": { + "lineWidth": 120, + "indentWidth": 2, + "singleQuote": false, + "useTabs": false + }, + "files": { + "exclude": [ + "./lock.json" + ] + } + } +} diff --git a/devshell.toml b/devshell.toml new file mode 100644 index 0000000..a82e37d --- /dev/null +++ b/devshell.toml @@ -0,0 +1,10 @@ +[devshell] +packages = [ + "alejandra", # Formatter for nix + "deno", + "taplo-cli", +] + +[[commands]] +package = "treefmt" +category = "formatter" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ae604be --- /dev/null +++ b/flake.lock @@ -0,0 +1,111 @@ +{ + "nodes": { + "devshell": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1654858401, + "narHash": "sha256-53bw34DtVJ2bnF6WEwy6Tym+qY0pNEiEwARUlvmTZjs=", + "owner": "numtide", + "repo": "devshell", + "rev": "f55e05c6d3bbe9acc7363bc8fc739518b2f02976", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1650374568, + "narHash": "sha256-Z+s0J8/r907g149rllvwhb4pKi8Wam5ij0st8PwAh+E=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "b4a34015c698c7793d592d66adbab377907a2be8", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1642700792, + "narHash": "sha256-XqHrk7hFb+zBvRg6Ghl+AZDq03ov6OshJLiSWOoX5es=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "846b2ae0fc4cc943637d3d1def4454213e203cba", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1653893745, + "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1643381941, + "narHash": "sha256-pHTwvnN4tTsEKkWlXQ8JMY423epos8wUOhthpwJjtpc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5efc8ca954272c4376ac929f4c5ffefcc20551d5", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1655400192, + "narHash": "sha256-49OBVVRgb9H/PSmNT9W61+NRdDbuSJVuDDflwXlaUKU=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "3d7435c638baffaa826b85459df0fff47f12317d", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "devshell": "devshell", + "flake-compat": "flake-compat", + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_2" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..874b886 --- /dev/null +++ b/flake.nix @@ -0,0 +1,73 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + devshell.url = "github:numtide/devshell"; + + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + }; + outputs = { + self, + nixpkgs, + flake-utils, + devshell, + ... + } @ inputs: + flake-utils.lib.eachSystem + [ + "x86_64-linux" + ] + ( + system: let + pkgs = import nixpkgs { + inherit system; + overlays = [ + devshell.overlay + (import ./overlay.nix) + ]; + }; + in rec { + packages.bundled = pkgs.deno2nix.mkBundled { + name = "example"; + version = "0.1.0"; + src = self; + lockfile = ./lock.json; + importmap = ./import_map.json; + entrypoint = ./mod.ts; + }; + packages.wrapper = pkgs.deno2nix.mkBundledWrapper { + name = "example"; + version = "0.1.0"; + src = self; + lockfile = ./lock.json; + importmap = ./import_map.json; + entrypoint = ./mod.ts; + }; + packages.executable = pkgs.deno2nix.mkExecutable { + name = "example"; + version = "0.1.0"; + src = self; + lockfile = ./lock.json; + importmap = ./import_map.json; + entrypoint = ./mod.ts; + }; + packages.default = packages.executable; + + defaultPackage = packages.default; + + apps.default = { + type = "app"; + program = "${defaultPackage}/bin/example"; + }; + + devShell = pkgs.devshell.mkShell { + imports = [ + (pkgs.devshell.importTOML ./devshell.toml) + ]; + }; + } + ); +} diff --git a/import_map.json b/import_map.json new file mode 100644 index 0000000..6db072a --- /dev/null +++ b/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "std/": "https://deno.land/std@0.118.0/" + } +} diff --git a/lock.json b/lock.json new file mode 100644 index 0000000..1865d4d --- /dev/null +++ b/lock.json @@ -0,0 +1,3 @@ +{ + "https://deno.land/std@0.118.0/fmt/colors.ts": "8368ddf2d48dfe413ffd04cdbb7ae6a1009cf0dccc9c7ff1d76259d9c61a0621" +} diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..f1f0981 --- /dev/null +++ b/mod.ts @@ -0,0 +1,3 @@ +import { green, italic } from "std/fmt/colors.ts"; + +console.log(`Hello ${italic(green("World"))}!`); diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..6ee74c5 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,126 @@ +{ + 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 importmap; + 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 name entrypoint; + denoFlags = + denoFlags + ++ ["--lock" lockfile] + ++ ["--cached-only"] + ++ ["--output" name] + ++ ( + if importmap != null + then ["--import-map" importmap] + else [] + ); + + src = cleanSourceWith { + inherit src; + filter = path: type: (baseNameOf path != name); + }; + 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/overlay.nix b/overlay.nix new file mode 100644 index 0000000..d1ff507 --- /dev/null +++ b/overlay.nix @@ -0,0 +1,5 @@ +final: prev: { + deno2nix = { + inherit (import ./nix {pkgs = prev;}) mkBundled mkBundledWrapper mkExecutable; + }; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..a227a72 --- /dev/null +++ b/shell.nix @@ -0,0 +1,14 @@ +(import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball (with lock.nodes.flake-compat.locked; { + url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"; + sha256 = narHash; + }) + ) + { + src = ./.; + }) +.shellNix diff --git a/treefmt.toml b/treefmt.toml new file mode 100644 index 0000000..9917d83 --- /dev/null +++ b/treefmt.toml @@ -0,0 +1,13 @@ +[formatter.alejandra] +command = "alejandra" +includes = ["*.nix"] + +[formatter.deno-fmt] +command = "deno" +options = ["fmt"] +includes = ["*.json", "*.jsonc", "*.md", "*.js", "*.jsx", "*.ts", "*.tsx"] + +[formatter.taplo] +command = "taplo" +options = ["format"] +includes = ["*.toml"]