From c914e0a8301e61f681200ed7529a3f3c95d4c67d Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Mon, 13 Nov 2023 00:17:41 -0600 Subject: [PATCH] Use flang-webr nix flake --- flake.lock | 46 ++++++++++++++++--- flake.nix | 129 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 139 insertions(+), 36 deletions(-) diff --git a/flake.lock b/flake.lock index 7f496176..3350e562 100644 --- a/flake.lock +++ b/flake.lock @@ -1,12 +1,32 @@ { "nodes": { + "nixpkg-flang-webr": { + "inputs": { + "nixpkgs": "nixpkgs", + "nixpkgs-emscripten": "nixpkgs-emscripten" + }, + "locked": { + "lastModified": 1699838609, + "narHash": "sha256-2GYbXH7es40TeEaZ3+aNX9SXHSFfQ7gI+ErVR1sryCU=", + "owner": "wch", + "repo": "flang-webr", + "rev": "b5db98feee04aba930e289f1fcb307ad4cc1acaa", + "type": "github" + }, + "original": { + "owner": "wch", + "ref": "main", + "repo": "flang-webr", + "type": "github" + } + }, "nixpkgs": { "locked": { - "lastModified": 1699291058, - "narHash": "sha256-5ggduoaAMPHUy4riL+OrlAZE14Kh7JWX4oLEs22ZqfU=", + "lastModified": 1699596684, + "narHash": "sha256-XSXP8zjBZJBVvpNb2WmY0eW8O2ce+sVyj1T0/iBRIvg=", "owner": "nixos", "repo": "nixpkgs", - "rev": "41de143fda10e33be0f47eab2bfe08a50f234267", + "rev": "da4024d0ead5d7820f6bd15147d3fe2a0c0cec73", "type": "github" }, "original": { @@ -32,10 +52,26 @@ "type": "github" } }, + "nixpkgs_2": { + "locked": { + "lastModified": 1699596684, + "narHash": "sha256-XSXP8zjBZJBVvpNb2WmY0eW8O2ce+sVyj1T0/iBRIvg=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "da4024d0ead5d7820f6bd15147d3fe2a0c0cec73", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-23.05", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { - "nixpkgs": "nixpkgs", - "nixpkgs-emscripten": "nixpkgs-emscripten" + "nixpkg-flang-webr": "nixpkg-flang-webr", + "nixpkgs": "nixpkgs_2" } } }, diff --git a/flake.nix b/flake.nix index 5e71c053..c08b4072 100644 --- a/flake.nix +++ b/flake.nix @@ -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 @@ -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: @@ -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 ''; };