fix and split

This commit is contained in:
SnO2WMaN 2022-08-04 08:32:54 +09:00
parent 1f99487aaf
commit 38c163ab75
6 changed files with 127 additions and 123 deletions

View file

@ -1,122 +0,0 @@
{
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;
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 src name entrypoint;
denoFlags =
denoFlags
++ ["--lock" "${src}/${lockfile}"]
++ ["--cached-only"]
++ ["--output" name]
++ (
if importMap != null
then ["--import-map" "${src}/${importMap}"]
else []
);
buildInputs = with pkgs; [
deno
jq
];
fixupPhase = ":";
buildPhase = ''
export DENO_DIR=`mktemp -d`
ln -s "${mkDepsLink "${src}/${lockfile}"}" $(deno info --json | jq -r .modulesCache)
ls $(dirname $src/$entrypoint)
deno compile $denoFlags "$entrypoint"
'';
installPhase = ''
mkdir -p $out/bin
mv "$name" "$out/bin/"
'';
};
}

View file

@ -0,0 +1,6 @@
{pkgs, ...}: {name, ...} @ args: let
bundled = (pkgs.callPackage ./make-bundled.nix {}) args;
in
pkgs.writeShellScriptBin
"${name}"
"${pkgs.deno}/bin/deno run ${bundled}/dist/bundled.js"

45
nix/make-bundled.nix Normal file
View file

@ -0,0 +1,45 @@
{
pkgs,
lib ? pkgs.lib,
...
}: {
name,
version,
src,
entrypoint,
lockfile,
importMap ? null,
denoFlags ? [],
}: let
inherit (pkgs.callPackage ./utils.nix {}) mkDepsLink;
in
pkgs.stdenv.mkDerivation {
inherit name version entrypoint;
denoFlags =
denoFlags
++ (
if importMap != null
then ["--import-map" importMap]
else []
);
src = lib.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
'';
}

40
nix/make-executable.nix Normal file
View file

@ -0,0 +1,40 @@
{pkgs, ...}: {
name,
version,
src,
entrypoint,
lockfile,
importMap ? null,
denoFlags ? [],
}: let
inherit (pkgs.callPackage ./utils.nix {}) mkDepsLink;
in
pkgs.stdenv.mkDerivation {
inherit src name entrypoint;
denoFlags =
denoFlags
++ ["--lock" lockfile]
++ ["--cached-only"]
++ ["--output" name]
++ (
if importMap != null
then ["--import-map" importMap]
else []
);
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/"
'';
}

33
nix/utils.nix Normal file
View file

@ -0,0 +1,33 @@
{
pkgs,
lib ? pkgs.lib,
...
}: let
inherit (builtins) hashString split elemAt fetchurl toJSON;
inherit (pkgs) linkFarm writeText;
inherit (lib) flatten mapAttrsToList importJSON;
in rec {
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" (lib.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)
))
);
}

View file

@ -1,5 +1,7 @@
final: prev: {
deno2nix = {
inherit (import ./nix {pkgs = prev;}) mkBundled mkBundledWrapper mkExecutable;
mkBundled = final.callPackage ./nix/make-bundled.nix {};
mkBundledWrapper = final.callPackage ./nix/make-bundled-wrapper.nix {};
mkExecutable = final.callPackage ./nix/make-executable.nix {};
};
}