stephank / yarn-plugin-nixify

Yarn v3/v4 plugin to help with Nix packaging
BSD 2-Clause "Simplified" License
68 stars 7 forks source link
nix yarn

yarn-plugin-nixify

Upgrading the plugin? See UPGRADING.md

Generates a Nix expression to build a Yarn v3 or v4 project (not using zero-install).

Related projects:

Usage

This plugin is compatible with Yarn >= 3.1.0 or >= 4.0.0. Run the following in your project folder to check:

# Check your Yarn version
yarn --version

# Upgrade to the latest version, if necessary
yarn set version berry

To then use the Nixify plugin:

# Install the plugin
yarn plugin import https://raw.githubusercontent.com/stephank/yarn-plugin-nixify/main/dist/yarn-plugin-nixify.js

# Run Yarn as usual
yarn

# Build your project with Nix
nix-build

Running yarn with this plugin enabled will generate two files:

This may already build successfully! But if your project needs extra build steps or native dependencies, you may have to customize default.nix a bit. Some examples of what's possible:

{ pkgs ? import <nixpkgs> { } }:

let

  project = pkgs.callPackage ./yarn-project.nix {

    # Example of selecting a specific version of Node.js.
    nodejs = pkgs.nodejs-14_x;

  } {

    # Example of providing a different source tree.
    src = pkgs.lib.cleanSource ./.;

  };

in project.overrideAttrs (oldAttrs: {

  # If your top-level package.json doesn't set a name, you can set one here.
  name = "myproject";

  # Example of adding packages to the build environment.
  # Especially dependencies with native modules may need a Python installation.
  buildInputs = oldAttrs.buildInputs ++ [ pkgs.python3 ];

  # Example of invoking a build step in your project.
  buildPhase = ''
    yarn build
  '';

})

Settings

Some additional settings are available in .yarnrc.yml:

Hacking

# In this directory:
yarn
yarn build-dev

# In your test project:
yarn plugin import /path/to/yarn-plugin-nixify/dist/yarn-plugin-nixify.dev.js

(Alternatively, add a direct reference in .yarnrc.yml. This will likely only work if the Nix sandbox is disabled.)