Packaging deno applications

You need to essentially generate a list of sources that deno cache would install and then organize them such that deno can load them as if they were cached. It’s enough work that it is encourage to write a dedicated converter e.g deno2nix. There’s some standardization to make this easier but it’s in the early stages.

As far as getting the data you need from deno, it appears that getting the URL & sha256 sums should be easy.

XDG_CACHE_HOME=/tmp/deno deno cache --lock-write --lock deno.lock https://deno.land/std@0.111.0/http/file_server.ts

This will produce a deno.lock file with each of the URL + sha256 checksum. I have confirmed that this is identical to the hash nix would procuce.

For example:

# Using nix store prefetch-file
nix store prefetch-file https://deno.land/std@0.111.0/_util/assert.ts
Downloaded 'https://deno.land/std@0.111.0/_util/assert.ts' to '/nix/store/8cy592azy5vbg5gb4ggdafvrdznks415-assert.ts' (hash 'sha256-L4aBRaBCoR1a0KPHSNz1gK3YoNvA6HbqoAJjA6VIj1g=').

# Using deno cache
"https://deno.land/std@0.111.0/_util/assert.ts": "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58"

# Convert to base64 sha256:
nix hash to-base64 --type sha256 2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58

L4aBRaBCoR1a0KPHSNz1gK3YoNvA6HbqoAJjA6VIj1g=

This is sufficient to use fetchurl to grab the source:

src = fetchurl {
   url = "https://deno.land/std@0.111.0/_util/assert.ts";
   sha256 = "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58";
};
# I think it's OK the leave the sha256 in the current base but not 100%. Otherwise just convert using nix hash

This src can then be an input to a function that recreates the deno cache directory structure… along the lines of this:

transformedSrc = deno2nix.convert2nix {
   name = "std_util_assert";
   another_needed_variable = "maybe the version?";
   src = fetchurl {
     url = "https://deno.land/std@0.111.0/_util/assert.ts";
     sha256 = "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58";
   };
};

I looked a bit at the structure but I’m not sure how it generates it from the downloaded files. There is also probably a better way to set the cache location than using XDG_CACHE_HOME. I’d recommend looking at some other ‘2nix’ converts like composer2nix and yarn2nix (in nixpkgs actually now).

1 Like