Skip to content

Commit

Permalink
Use flang-webr nix flake
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Nov 13, 2023
1 parent ea25f72 commit c914e0a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 36 deletions.
46 changes: 41 additions & 5 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 98 additions & 31 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,60 @@
# Flake inputs
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
# Use this commit to get Emscripten 3.1.45
# See https://www.nixhub.io/packages/emscripten
nixpkgs-emscripten.url =
"github:NixOS/nixpkgs/75a52265bda7fd25e06e3a67dee3f0354e73243c";
nixpkg-flang-webr.url = "github:wch/flang-webr/main";
};

# Flake outputs
outputs = { self, nixpkgs, nixpkgs-emscripten }:
outputs = { self, nixpkgs, nixpkg-flang-webr }:
let
# Systems supported
allSystems = [
"x86_64-linux" # 64-bit Intel/AMD Linux
"aarch64-linux" # 64-bit ARM Linux
"x86_64-darwin" # 64-bit Intel macOS
"aarch64-darwin" # 64-bit ARM macOS
];
allSystems =
[ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];

# Helper to provide system-specific attributes
forAllSystems = f:
nixpkgs.lib.genAttrs allSystems (system:
f {
f rec {
pkgs = import nixpkgs { inherit system; };
pkgs-emscripten = import nixpkgs-emscripten { inherit system; };
pkg-flang-webr = nixpkg-flang-webr.packages.${system}.default;

# Get emscripten from the flang-webr package
pkg-emscripten = builtins.head
(builtins.filter (x: x.pname == "emscripten")
pkg-flang-webr.propagatedNativeBuildInputs);

# Cached npm dependencies specified by src/package-lock.json. During
# development, whenever package-lock.json is updated, the hash needs
# to be updated. To find the hash, run:
# cd src; prefetch-npm-deps package-lock.json
srcNpmDeps = pkgs.fetchNpmDeps {
src = "${self}/src";
hash = "sha256-FvKuT6ComD2KKOpd4ucnoyqZJz3wZfR6nKYOCKTLgYM=";
};

inherit system;
});

in {
# Development environment output
devShells = forAllSystems ({ pkgs, pkgs-emscripten }: {
default = pkgs.mkShell {
# The Nix packages provided in the environment
packages = with pkgs;
[
in rec {
packages = forAllSystems
({ pkgs, pkg-flang-webr, system, pkg-emscripten, srcNpmDeps, ... }: {
default = pkgs.stdenv.mkDerivation {
name = "webr";
src = ./.;

nativeBuildInputs = with pkgs; [
pkg-flang-webr
pkg-emscripten

git
cacert

cmake
gperf
lzma
pcre2
nodejs_18
quilt
wget

libxml2
git
python3
nodejs_18

# Inputs for building R borrowed from:
# https://github.com/NixOS/nixpkgs/blob/85f1ba3e/pkgs/applications/science/math/R/default.nix
Expand All @@ -67,7 +78,63 @@
sqlite # For proj
glib # For pango
unzip # For extracting font data
] ++ [ pkgs-emscripten.emscripten ];
];

# Copy files from flang-webr package to their correct location in
# the build source dir. Make sure those directories are writable
# because this build will write to some of the same directories.
postUnpack = ''
cp -R ${pkg-flang-webr}/webr/* $sourceRoot
chmod -R u+w $sourceRoot
'';

# Need to call configure _without_ extra arguments that mkDerivation
# would normally throw in.
#
# The cd src; npm config stuff is so that it writes an .npmrc file
# in the src/ directory. Later `npm ci` will be run from that
# directory. If .npmrc is not present in the working dir, then npm
# will look for it in the home directory, which does not exist in a
# build.
configurePhase = ''
./configure
cd src
npm config set cache "${srcNpmDeps}" --location project
npm config set offline true --location project
npm config set progress false --location project
cd ..
if [ ! -d $(pwd)/.emscripten_cache-${pkg-emscripten.version} ]; then
cp -R ${pkg-emscripten}/share/emscripten/cache/ $(pwd)/.emscripten_cache-${pkg-emscripten.version}
chmod u+rwX -R $(pwd)/.emscripten_cache-${pkg-emscripten.version}
fi
export EM_CACHE=$(pwd)/.emscripten_cache-${pkg-emscripten.version}
echo emscripten cache dir: $EM_CACHE
'';

buildPhase = ''
make -j$NIX_BUILD_CORES
'';

installPhase = ''
mkdir -p $out
cp -r dist $out
'';
};
});

# Development environment output
devShells = forAllSystems ({ pkgs, system, pkg-emscripten, ... }: {
default = pkgs.mkShell {
inputsFrom = [ packages.${system}.default ];

packages = with pkgs; [
prefetch-npm-deps
];

# TODO: Add information on how to get the SHA256:
# cd src; prefetch-npm-deps package-lock.json

# This is a workaround for nix emscripten cache directory not being
# writable. Borrowed from:
Expand All @@ -82,11 +149,11 @@
# widespread use, we'll be able to use
# https://github.com/NixOS/nix/issues/8034
shellHook = ''
if [ ! -d $(pwd)/.emscripten_cache-${pkgs-emscripten.emscripten.version} ]; then
cp -R ${pkgs-emscripten.emscripten}/share/emscripten/cache/ $(pwd)/.emscripten_cache-${pkgs-emscripten.emscripten.version}
chmod u+rwX -R $(pwd)/.emscripten_cache-${pkgs-emscripten.emscripten.version}
if [ ! -d $(pwd)/.emscripten_cache-${pkg-emscripten.version} ]; then
cp -R ${pkg-emscripten}/share/emscripten/cache/ $(pwd)/.emscripten_cache-${pkg-emscripten.version}
chmod u+rwX -R $(pwd)/.emscripten_cache-${pkg-emscripten.version}
fi
export EM_CACHE=$(pwd)/.emscripten_cache-${pkgs-emscripten.emscripten.version}
export EM_CACHE=$(pwd)/.emscripten_cache-${pkg-emscripten.version}
echo emscripten cache dir: $EM_CACHE
'';
};
Expand Down

0 comments on commit c914e0a

Please sign in to comment.