setup env

This commit is contained in:
SnO2WMaN 2022-06-19 10:24:56 +09:00
parent 07dd7c3206
commit af11612750
17 changed files with 498 additions and 0 deletions

29
.editorconfig Normal file
View file

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

2
.envrc Normal file
View file

@ -0,0 +1,2 @@
#!/usr/bin/env bash
use flake

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
.direnv/
result
dist

9
.vscode/extensions.json vendored Normal file
View file

@ -0,0 +1,9 @@
{
"recommendations": [
"jnoortheen.nix-ide",
"arrterian.nix-env-selector",
"tamasfe.even-better-toml",
"jkillian.custom-local-formatters",
"denoland.vscode-deno"
]
}

59
.vscode/settings.json vendored Normal file
View file

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

14
default.nix Normal file
View file

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

18
deno.jsonc Normal file
View file

@ -0,0 +1,18 @@
{
"tasks": {
"run": "deno run ./mod.ts"
},
"fmt": {
"options": {
"lineWidth": 120,
"indentWidth": 2,
"singleQuote": false,
"useTabs": false
},
"files": {
"exclude": [
"./lock.json"
]
}
}
}

10
devshell.toml Normal file
View file

@ -0,0 +1,10 @@
[devshell]
packages = [
"alejandra", # Formatter for nix
"deno",
"taplo-cli",
]
[[commands]]
package = "treefmt"
category = "formatter"

111
flake.lock Normal file
View file

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

73
flake.nix Normal file
View file

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

5
import_map.json Normal file
View file

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

3
lock.json Normal file
View file

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

3
mod.ts Normal file
View file

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

126
nix/default.nix Normal file
View file

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

5
overlay.nix Normal file
View file

@ -0,0 +1,5 @@
final: prev: {
deno2nix = {
inherit (import ./nix {pkgs = prev;}) mkBundled mkBundledWrapper mkExecutable;
};
}

14
shell.nix Normal file
View file

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

13
treefmt.toml Normal file
View file

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