refactored to use imports so that flake.nix is under 100 lines

This commit is contained in:
Quinn Dougherty 2022-08-15 21:24:14 -07:00
parent e411e70db9
commit 909ebbec94
5 changed files with 263 additions and 253 deletions

280
flake.nix
View File

@ -35,249 +35,39 @@
hciPkgs = import nixpkgs { system = hciSystem; };
effects = hercules-ci-effects.lib.withPkgs hciPkgs;
buildInputsCommon = with pkgs; [ nodejs yarn ];
prettierCommon = with pkgs.nodePackages; [ prettier ];
pkgWhich = [ pkgs.which ];
# To get prettier into later source trees
monorepo-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-monorepo_source";
src = ./.;
packageJSON = ./package.json;
yarnLock = ./yarn.lock;
common = {
buildInputs = with pkgs; [ nodejs yarn ];
prettier = with pkgs.nodePackages; [ prettier ];
which = [ pkgs.which ];
};
lang = import ./nix/squiggle-lang.nix {
inherit system pkgs common gentype;
};
components =
import ./nix/squiggle-components.nix { inherit pkgs common lang; };
website = import ./nix/squiggle-website.nix {
inherit pkgs common lang components;
};
# packages in subrepos
lang-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-lang_source";
src = ./packages/squiggle-lang;
packageJSON = ./packages/squiggle-lang/package.json;
yarnLock = ./yarn.lock;
# extraBuildInputs = prettierCommon;
pkgConfig = {
rescript = {
buildInputs = pkgWhich ++ [ pkgs.gcc_multi ];
postInstall = ''
echo "PATCHELF'ING RESCRIPT EXECUTABLES (INCL NINJA)"
# Patching interpreter for linux/*.exe's
THE_LD=$(patchelf --print-interpreter $(which mkdir))
patchelf --set-interpreter $THE_LD linux/*.exe && echo "- patched interpreter for linux/*.exe's"
# Replacing needed shared library for linux/ninja.exe
THE_SO=$(find /nix/store/*/lib64 -name libstdc++.so.6 | head -n 1)
patchelf --replace-needed libstdc++.so.6 $THE_SO linux/ninja.exe && echo "- replaced needed for linux/ninja.exe"
'';
};
bisect_ppx = {
buildInputs =
pkgWhich; # ++ (with pkgs; [ ocaml nodePackages.esy ocamlPackages.bisect_ppx ]);
postInstall = ''
echo "PATCHELF'ING BISECT_PPX EXECUTABLE"
THE_LD=$(patchelf --print-interpreter $(which mkdir))
patchelf --set-interpreter $THE_LD bin/linux/ppx
patchelf --set-interpreter $THE_LD bin/linux/bisect-ppx-report
'';
};
gentype = {
postInstall = ''
mv gentype.exe ELFLESS-gentype.exe
cp ${
gentype.outputs.defaultPackage."${system}"
}/GenType.exe gentype.exe
'';
};
};
};
lang-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-lint";
src = lang-yarnPackage
+ "/libexec/@quri/squiggle-lang/deps/@quri/squiggle-lang";
buildInputs = buildInputsCommon ++ prettierCommon;
buildPhase = ''
yarn lint:prettier
yarn lint:rescript
'';
installPhase = "mkdir -p $out";
};
lang-build = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-build";
# `peggy` is in the `node_modules` that's adjacent to `deps`.
src = lang-yarnPackage + "/libexec/@quri/squiggle-lang";
buildInputs = buildInputsCommon;
buildPhase = ''
mv node_modules deps
pushd deps/@quri/squiggle-lang
yarn --offline build:peggy
yarn --offline build:rescript
yarn --offline build:typescript
# custom gitignore so that the flake keeps build artefacts
mv .gitignore GITIGNORE
sed -i /Reducer_Peggy_GeneratedParser.js/d GITIGNORE
sed -i /\*.bs.js/d GITIGNORE
sed -i /\*.gen.ts/d GITIGNORE
sed -i /\*.gen.tsx/d GITIGNORE
sed -i /\*.gen.js/d GITIGNORE
sed -i /helpers.js/d GITIGNORE
popd
'';
installPhase = ''
mkdir -p $out
# mkdir -p $out/node_modules
mv deps/@quri/squiggle-lang/GITIGNORE deps/@quri/squiggle-lang/.gitignore
# annoying hack because permissions on transitive dependencies later on
mv deps/@quri/squiggle-lang/node_modules deps/@quri/squiggle-lang/NODE_MODULES
mv deps/node_modules deps/@quri/squiggle-lang
# the proper install phase
cp -r deps/@quri/squiggle-lang/. $out
'';
};
lang-test = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-test";
src = lang-build;
buildInputs = buildInputsCommon;
buildPhase = ''
yarn --offline test
'';
installPhase = ''
mkdir -p $out
cp -r . $out
'';
};
lang-bundle = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-bundle";
src = lang-test;
buildInputs = buildInputsCommon;
buildPhase = ''
yarn --offline bundle
'';
installPhase = ''
mkdir -p $out
cp -r dist $out
cp *.json $out/dist
'';
};
componentsPackageJson = let
raw = pkgs.lib.importJSON ./packages/components/package.json;
modified = pkgs.lib.recursiveUpdate raw {
dependencies.react-dom = "^18.2.0";
};
packageJsonString = builtins.toJSON modified;
in pkgs.writeText "packages/components/patched-package.json"
packageJsonString;
components-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-components_source";
buildInputs = buildInputsCommon;
src = ./packages/components;
packageJSON = componentsPackageJson;
yarnLock = ./yarn.lock;
packageResolutions."@quri/squiggle-lang" = lang-build;
};
components-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-components-lint";
src = components-yarnPackage
+ "/libexec/@quri/squiggle-components/deps/@quri/squiggle-components";
buildInputs = buildInputsCommon ++ prettierCommon;
buildPhase = "yarn lint";
installPhase = "mkdir -p $out";
};
components-package-build = pkgs.stdenv.mkDerivation {
name = "squiggle-components-package-build";
src = components-yarnPackage + "/libexec/@quri/squiggle-components";
buildInputs = buildInputsCommon;
buildPhase = ''
cp -r node_modules/@quri/squiggle-lang deps/@quri
pushd deps/@quri/squiggle-components
yarn --offline build:cjs
yarn --offline build:css
popd
'';
installPhase = ''
mkdir -p $out
# annoying hack because permissions on transitive dependencies later on
mv deps/@quri/squiggle-components/node_modules deps/@quri/squiggle-components/NODE_MODULES
mv node_modules deps/@quri/squiggle-components
# patching .gitignore so flake keeps build artefacts
sed -i /dist/d deps/@quri/squiggle-components/.gitignore
cp -r deps/@quri/squiggle-components/. $out
'';
};
components-site-build = pkgs.stdenv.mkDerivation {
name = "squiggle-components-storybook";
src = components-package-build;
buildInputs = buildInputsCommon;
buildPhase = "yarn build:storybook";
installPhase = ''
mkdir -p $out
# patching .gitignore so flake keeps build artefacts
sed -i /build/d .gitignore
sed -i /storybook-static/d .gitignore
'';
};
websitePackageJson = let
raw = pkgs.lib.importJSON ./packages/website/package.json;
modified = pkgs.lib.recursiveUpdate raw {
dependencies.postcss-import = "^14.1.0";
dependencies.tailwindcss = "^3.1.8";
};
packageJsonString = builtins.toJSON modified;
in pkgs.writeText "packages/website/patched-package.json"
packageJsonString;
website-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-website_source";
src = ./packages/website;
packageJSON = websitePackageJson;
yarnLock = ./yarn.lock;
packageResolutions."@quri/squiggle-lang" = lang-build;
packageResolutions."@quri/squiggle-components" =
components-package-build;
workspaceDependencies = [ lang-yarnPackage components-yarnPackage ];
};
website-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-website-lint";
buildInputs = buildInputsCommon ++ prettierCommon;
src = website-yarnPackage
+ "/libexec/squiggle-website/deps/squiggle-website";
buildPhase = "yarn --offline lint";
installPhase = "mkdir -p $out";
};
website = pkgs.stdenv.mkDerivation {
name = "squiggle-website";
buildInputs = buildInputsCommon;
src = website-yarnPackage + "/libexec/squiggle-website";
buildPhase = ''
pushd deps/squiggle-website
yarn --offline build
popd
'';
installPhase = ''
mkdir -p $out
cp -r $src/build $out
'';
};
in rec {
checks = flake-utils.lib.flattenTree {
lang-lint = lang-lint;
lang-test = lang-test;
components-lint = components-lint;
docusaurus-lint = website-lint;
lang-lint = lang.lang-lint;
lang-test = lang.lang-test;
components-lint = components.components-lint;
docusaurus-lint = website.website-lint;
};
packages = flake-utils.lib.flattenTree {
default = website;
lang-bundle = lang-bundle;
components = components-package-build;
storybook = components-site-build;
docs-site = website;
default = website.website;
lang-bundle = lang.lang-bundle;
components = components.components-package-build;
storybook = components.components-site-build;
docs-site = website.website;
};
# nix develop
devShells = flake-utils.lib.flattenTree {
default = (import ./nix/shell.nix { inherit pkgs; }).shell;
};
# herc
@ -287,7 +77,7 @@
lang.outputs = {
squiggle-lang-lint = checks.${hciSystem}.lang-lint;
squiggle-lang-test = checks.${hciSystem}.lang-test;
squiggle-lang-build = lang-build;
squiggle-lang-build = lang.lang-build;
squiggle-lang-bundle = packages.${hciSystem}.lang-bundle;
};
components.outputs = {
@ -302,21 +92,5 @@
};
};
# nix develop
devShells = flake-utils.lib.flattenTree {
default = pkgs.mkShell {
name = "squiggle-wasm-development-shell";
buildInputs = with pkgs; [
wasm-pack
cargo
cargo-generate
yarn
rustup
pkg-config
openssl
nixfmt
];
};
};
});
}

9
nix/shell.nix Normal file
View File

@ -0,0 +1,9 @@
{ pkgs }:
with pkgs;
{
shell = pkgs.mkShell {
name = "SQUIGGLE_yarn-wasm-devshell";
buildInputs =
[ wasm-pack cargo yarn nodejs rustup pkg-config openssl nixfmt ];
};
}

View File

@ -0,0 +1,64 @@
{ pkgs, common, lang }:
rec {
componentsPackageJson = let
raw = pkgs.lib.importJSON ./packages/components/package.json;
modified =
pkgs.lib.recursiveUpdate raw { dependencies.react-dom = "^18.2.0"; };
packageJsonString = builtins.toJSON modified;
in pkgs.writeText "packages/components/patched-package.json"
packageJsonString;
components-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-components_source";
buildInputs = common.buildInputs;
src = ./packages/components;
packageJSON = componentsPackageJson;
yarnLock = ./yarn.lock;
packageResolutions."@quri/squiggle-lang" = lang.lang-build;
};
components-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-components-lint";
src = components-yarnPackage
+ "/libexec/@quri/squiggle-components/deps/@quri/squiggle-components";
buildInputs = common.buildInputs ++ common.prettier;
buildPhase = "yarn lint";
installPhase = "mkdir -p $out";
};
components-package-build = pkgs.stdenv.mkDerivation {
name = "squiggle-components-package-build";
src = components-yarnPackage + "/libexec/@quri/squiggle-components";
buildInputs = common.buildInputs;
buildPhase = ''
cp -r node_modules/@quri/squiggle-lang deps/@quri
pushd deps/@quri/squiggle-components
yarn --offline build:cjs
yarn --offline build:css
popd
'';
installPhase = ''
mkdir -p $out
# annoying hack because permissions on transitive dependencies later on
mv deps/@quri/squiggle-components/node_modules deps/@quri/squiggle-components/NODE_MODULES
mv node_modules deps/@quri/squiggle-components
# patching .gitignore so flake keeps build artefacts
sed -i /dist/d deps/@quri/squiggle-components/.gitignore
cp -r deps/@quri/squiggle-components/. $out
'';
};
components-site-build = pkgs.stdenv.mkDerivation {
name = "squiggle-components-storybook";
src = components-package-build;
buildInputs = common.buildInputs;
buildPhase = "yarn build:storybook";
installPhase = ''
mkdir -p $out
# patching .gitignore so flake keeps build artefacts
sed -i /build/d .gitignore
sed -i /storybook-static/d .gitignore
'';
};
}

118
nix/squiggle-lang.nix Normal file
View File

@ -0,0 +1,118 @@
{ system, pkgs, common, gentype }:
rec {
# packages in subrepos
lang-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-lang_source";
src = ./packages/squiggle-lang;
packageJSON = ./packages/squiggle-lang/package.json;
yarnLock = ./yarn.lock;
# extraBuildInputs = prettierCommon;
pkgConfig = {
rescript = {
buildInputs = common.which ++ [ pkgs.gcc_multi ];
postInstall = ''
echo "PATCHELF'ING RESCRIPT EXECUTABLES (INCL NINJA)"
# Patching interpreter for linux/*.exe's
THE_LD=$(patchelf --print-interpreter $(which mkdir))
patchelf --set-interpreter $THE_LD linux/*.exe && echo "- patched interpreter for linux/*.exe's"
# Replacing needed shared library for linux/ninja.exe
THE_SO=$(find /nix/store/*/lib64 -name libstdc++.so.6 | head -n 1)
patchelf --replace-needed libstdc++.so.6 $THE_SO linux/ninja.exe && echo "- replaced needed for linux/ninja.exe"
'';
};
bisect_ppx = {
buildInputs =
common.which; # ++ (with pkgs; [ ocaml nodePackages.esy ocamlPackages.bisect_ppx ]);
postInstall = ''
echo "PATCHELF'ING BISECT_PPX EXECUTABLE"
THE_LD=$(patchelf --print-interpreter $(which mkdir))
patchelf --set-interpreter $THE_LD bin/linux/ppx
patchelf --set-interpreter $THE_LD bin/linux/bisect-ppx-report
'';
};
gentype = {
postInstall = ''
mv gentype.exe ELFLESS-gentype.exe
cp ${
gentype.outputs.defaultPackage."${system}"
}/GenType.exe gentype.exe
'';
};
};
};
lang-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-lint";
src = lang-yarnPackage
+ "/libexec/@quri/squiggle-lang/deps/@quri/squiggle-lang";
buildInputs = common.buildInputs ++ common.prettier;
buildPhase = ''
yarn lint:prettier
yarn lint:rescript
'';
installPhase = "mkdir -p $out";
};
lang-build = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-build";
# `peggy` is in the `node_modules` that's adjacent to `deps`.
src = lang-yarnPackage + "/libexec/@quri/squiggle-lang";
buildInputs = common.buildInputs;
buildPhase = ''
mv node_modules deps
pushd deps/@quri/squiggle-lang
yarn --offline build:peggy
yarn --offline build:rescript
yarn --offline build:typescript
# custom gitignore so that the flake keeps build artefacts
mv .gitignore GITIGNORE
sed -i /Reducer_Peggy_GeneratedParser.js/d GITIGNORE
sed -i /\*.bs.js/d GITIGNORE
sed -i /\*.gen.ts/d GITIGNORE
sed -i /\*.gen.tsx/d GITIGNORE
sed -i /\*.gen.js/d GITIGNORE
sed -i /helpers.js/d GITIGNORE
popd
'';
installPhase = ''
mkdir -p $out
# mkdir -p $out/node_modules
mv deps/@quri/squiggle-lang/GITIGNORE deps/@quri/squiggle-lang/.gitignore
# annoying hack because permissions on transitive dependencies later on
mv deps/@quri/squiggle-lang/node_modules deps/@quri/squiggle-lang/NODE_MODULES
mv deps/node_modules deps/@quri/squiggle-lang
# the proper install phase
cp -r deps/@quri/squiggle-lang/. $out
'';
};
lang-test = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-test";
src = lang-build;
buildInputs = common.buildInputs;
buildPhase = ''
yarn --offline test
'';
installPhase = ''
mkdir -p $out
cp -r . $out
'';
};
lang-bundle = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-bundle";
src = lang-test;
buildInputs = common.buildInputs;
buildPhase = ''
yarn --offline bundle
'';
installPhase = ''
mkdir -p $out
cp -r dist $out
cp *.json $out/dist
'';
};
}

45
nix/squiggle-website.nix Normal file
View File

@ -0,0 +1,45 @@
{ pkgs, common, lang, components }:
rec {
websitePackageJson = let
raw = pkgs.lib.importJSON ./packages/website/package.json;
modified = pkgs.lib.recursiveUpdate raw {
dependencies.postcss-import = "^14.1.0";
dependencies.tailwindcss = "^3.1.8";
};
packageJsonString = builtins.toJSON modified;
in pkgs.writeText "packages/website/patched-package.json" packageJsonString;
website-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-website_source";
src = ./packages/website;
packageJSON = websitePackageJson;
yarnLock = ./yarn.lock;
packageResolutions."@quri/squiggle-lang" = lang.lang-build;
packageResolutions."@quri/squiggle-components" =
components.components-package-build;
workspaceDependencies =
[ lang.lang-yarnPackage components.components-yarnPackage ];
};
website-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-website-lint";
buildInputs = common.buildInputs ++ common.prettier;
src = website-yarnPackage
+ "/libexec/squiggle-website/deps/squiggle-website";
buildPhase = "yarn --offline lint";
installPhase = "mkdir -p $out";
};
website = pkgs.stdenv.mkDerivation {
name = "squiggle-website";
buildInputs = common.buildInputs;
src = website-yarnPackage + "/libexec/squiggle-website";
buildPhase = ''
pushd deps/squiggle-website
yarn --offline build
popd
'';
installPhase = ''
mkdir -p $out
cp -r $src/build $out
'';
};
}