MyNixOS website logo
Description

Convert yarn.lock files to nix expressions.

Convert yarn.lock files to nix expressions. See yarn2nix executable. Contains a nix library to call the generated nix files in nix-lib/. Library functions and module names might be restructured in the future.

ATTENTION: You are not looking at the yarn2nix as packaged in nixpkgs. This is an alternative implementation, with different tradeoffs. For an overview of the history of these tools and current options, see this nixpkgs issue.

I currently don’t have much time to maintain this project, it should work for some cases, but has not been “battle-tested” very much.

yarn2nix

yarn2nix [--offline] [path/to/yarn.lock]

  Convert a `yarn.lock` into a synonymous nix expression.
  If no path is given, search for `./yarn.lock`.
  If --offline is given, abort if figuring out a hash
  requires network access.

yarn2nix --template [path/to/package.json]

  Generate a package template nix-expression for your `package.json`.

Features

  • Purely transform yarn.lock files into very minimal, line-diffable nix expressions.
  • Nix is used to its fullest. Every package is a derivation, whole dependency subtrees are shared in an optimal way, even between projects.
  • The ability to resolve git dependencies by prefetching their repos and including the hashes.
  • Completely local transformation if there are no git dependencies (can be used inside nix-build, no large file check-in).
  • Extremely fast.
  • Nice code that can be easily extended, new repositories introduced, adapt to new versions of the yarn.lock format.
  • Comes with a nix library that uses the power of overlays to make overriding dependencies possible.
  • POWERED BY HNIX™ since before it was cool.

Probably a few more.

Example Output

The CodiMD server is an elaborate npm package with hundreds of dependencies. yarn2nix flawlessly parses the current (2020-07) yarn.lock file distributed with the project, including resolving their manual git forks of multiple npm packages:

$ yarn2nix ~/tmp/server/yarn.lock | wc
   7320   22701  399111
$ wc ~/tmp/server/yarn.lock
 11938  18615 500078 /home/lukas/tmp/server/yarn.lock

The output of this conversion can be seen here. Also note that git dependencies are resolved correctly.

Pushing it through the provided library of nix functions, we get a complete build of CodiMD's dependencies, using the project template (generated with --template), we also build the CodiMD server. Included executables will be in node_modules/.bin as expected and correctly link to their respective library paths in the nix store, for example:

$ /nix/store/zs9jk7yhdxsasn26m0903fq89cmyllzv-CodiMD-1.6.0/node_modules/.bin/markdown-it -v
10.0.0
$ readlink /nix/store/zs9jk7yhdxsasn26m0903fq89cmyllzv-CodiMD-1.6.0/node_modules/.bin/markdown-it
/nix/store/bgas2l5izznq1b61a3jyf3gpb73x8chn-markdown-it-10.0.0/bin/markdown-it.js

Building yarn2nix

$ nix-build
$ result/bin/yarn2nix

Using the generated nix files to build a project

Note: This is a temporary interface. Ideally, the library will be in nixpkgs and yarn2nix will be callable from inside the build (so the resulting nix files don’t have to be checked in).

Once you have the yarn2nix binary, use it to generate nix files for the yarn.lock file and the package.json:

$ yarn2nix ./jsprotect/yarn.lock > npm-deps.nix
$ yarn2nix --template ./jsproject/package.json > npm-package.nix

Then use the library to assemble the generated files in a default.nix:

let
  pkgs = import <nixpkgs> {};
  yarn2nix = import /path/to/yarn2nix {};
  nixLib = yarn2nix.nixLib;

in
  nixLib.buildNodePackage
    ( { src = nixLib.removePrefixes [ "node_modules" ] ./.; } //
      nixLib.callTemplate ./npm-package.nix
        (nixLib.buildNodeDeps (pkgs.callPackage ./npm-deps.nix {})))

Finally, run nix-build, and voilà, in ./result/ you find the project with all its dependencies correctly linked to their corresponding node_modules folder, recursively.

Using private package repository

Since yarn2nix uses standard fetchurl to download packages, it is possible to authenticate by overriding fetchurl to use the access credentials in /etc/nix/netrc.

Refer to the Enterprise NixOS Wiki article for instructions.

Development

$ nix-shell
nix-shell> ninja
nix-shell> cabal build yarn2nix
Metadata

Version

0.10.1

License

Maintainers (1)

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows