Skip to content

Commit

Permalink
chore(workspace): gh actions matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicegginton committed Jul 19, 2024
1 parent 6f9edd9 commit bdffd4c
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 51 deletions.
35 changes: 30 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
on:
workflow_dispatch:
push:
branches: [main]
branches:
- main
pull_request:
branches: [main]
workflow_dispatch:
branches:
- main

jobs:
checks:
nix-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- id: set-matrix
name: set matrix
run: |
set -Eeu
matrix="$(nix eval --json .#githubActions.matrix)"
echo $matrix
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
flake-checker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/flake-checker-action@main

checks:
needs: nix-matrix
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: ${{fromJson(needs.nix-matrix.outputs.matrix)}}
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- run: nix flake check
56 changes: 23 additions & 33 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# The noxide Nix support for building NPM packages.
# See `buildPackage` for the main entry point.

{ pkgs, lib ? pkgs.lib, ... } @ topLevelArgs:
{ pkgs, lib, ... }:

with builtins;
with lib;
Expand All @@ -10,24 +10,17 @@ with pkgs.nodejs;

{ name ? "${args.pname}-${args.version}"
, src ? null
, srcs ? null
, sourceRoot ? null
, prePatch ? ""
, patches ? [ ]
, postPatch ? ""
, nativeBuildInputs ? [ ]
, buildInputs ? [ ]
, customPatchPackages ? { }
, npmBuildScript ? "build"
, dontNpmBuild ? false
, npmFlags ? [ ]
, npmInstallFlags ? npmFlags
, npmRebuildFlags ? npmFlags
, npmBuildFlags ? npmFlags
, npmRebuildFlags ? npmFlags
, nodejs ? pkgs.nodejs
, npmConfigHook ? null
, npmBuildHook ? null
, npmInstallHook ? null
, customPatchPackages ? { }
, ...
} @ args:

Expand All @@ -52,16 +45,16 @@ let
(dep: if false then customPatchPackages.${dep} else pkgs.fetchurl { url = dep.resolved; hash = dep.integrity; })
deps;

reformatPackageName = pname:
let
parts = tail (match "^(@([^/]+)/)?([^/]+)$" pname);
non-null = filter (x: x != null) parts;
in
concatStringsSep "-" non-null;
packageJSON = readPackageJSON root;
resolvedPname = attrs.pname or (packageJSON.name or fallbackPackageName);
resolvedVersion = attrs.version or (packageJSON.version or fallbackPackageVersion);
name = attrs.name or "${reformatPackageName resolvedPname}-${resolvedVersion}";
# reformatPackageName = pname:
# let
# parts = tail (match "^(@([^/]+)/)?([^/]+)$" pname);
# non-null = filter (x: x != null) parts;
# in
# concatStringsSep "-" non-null;
# packageJSON = readPackageJSON root;
# resolvedPname = attrs.pname or (packageJSON.name or fallbackPackageName);
# resolvedVersion = attrs.version or (packageJSON.version or fallbackPackageVersion);
# name = attrs.name or "${reformatPackageName resolvedPname}-${resolvedVersion}";
npmOverrideScript = pkgs.writeShellScriptBin "npm" ''
source "${pkgs.stdenv}/setup"
set -e
Expand All @@ -85,16 +78,10 @@ let
'';
in



nodejs.stdenv.mkDerivation (args // {
inherit npmBuildScript;

nativeBuildInputs = nativeBuildInputs ++ [
nodejs
nodejs.python
]
++ optionals stdenv.isDarwin [ darwin.cctools ];
nativeBuildInputs = nativeBuildInputs ++ [ nodejs nodejs.python ] ++ optionals stdenv.isDarwin [ darwin.cctools ];
buildInputs = buildInputs ++ [ nodejs ];

configurePhase =
Expand All @@ -108,18 +95,22 @@ nodejs.stdenv.mkDerivation (args // {
export npm_config_cache=$PWD/.npm
'';

# TODO: ADD

buildPhase =
args.buildPhase
or ''
echo "Executing npmBuildHook"
runHook preBuild
mkdir -p .npm
cp -r ${cacache} .npm/_cacache
${nodejs}/bin/npm config set cache "$npm_config_cache"
${nodejs}/bin/npm config set offline true
${nodejs}/bin/npm config set progress false
${lib.optionalString (customPatchPackages != { }) ''
echo "Patching npm packages integrity"
${nodejs}/bin/node ${./scripts}/package-lock.mjs
''}
mkdir -p .npm
cp -r ${cacache} .npm/_cacache
${nodejs}/bin/npm ci --ignore-scripts --prefer-offline --nodedir=${nodejs}/include/node ${concatStringsSep " " npmInstallFlags} ${concatStringsSep " " npmRebuildFlags}
if ! ${boolToString dontNpmBuild}; then
${nodejs}/bin/npm run ${npmBuildScript} -- ${concatStringsSep " " npmBuildFlags} ${concatStringsSep " " npmFlags}
Expand All @@ -141,5 +132,4 @@ nodejs.stdenv.mkDerivation (args // {
strictDeps = true;
dontStrip = args.dontStrip or true;
meta = (args.meta or { }) // { platforms = args.meta.platforms or nodejs.meta.platforms; };
}
)
})
21 changes: 21 additions & 0 deletions flake.lock

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

34 changes: 21 additions & 13 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@
description = "Build NPM packages in Nix";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nix-github-actions.url = "github:nix-community/nix-github-actions";
inputs.nix-github-actions.inputs.nixpkgs.follows = "nixpkgs";

outputs = { self, nixpkgs, flake-utils, ... }:
outputs = { self, nixpkgs, flake-utils, nix-github-actions }:

with nixpkgs.lib;
with flake-utils.lib;
with nixpkgs.legacyPackages.x86_64-linux;
with nix-github-actions.lib;

let
systems = nodejs.meta.platforms;
in

eachSystem systems
eachSystem nixpkgs.legacyPackages.x86_64-linux.nodejs.meta.platforms
(system:

let
pkgs = import nixpkgs { inherit system; };
noxide = import ./default.nix { inherit pkgs; };
tests = import ./tests.nix { inherit pkgs; };
pkgs = import nixpkgs {
inherit system;
overlays = [ self.overlays.default ];
};
in

{
formatter = pkgs.nixpkgs-fmt;
checks = tests;
legacyPackages.noxide = noxide;
lib.noxide = pkgs.lib.noxide;
checks = {
empty-package = pkgs.callPackage ./test/empty-package { };
hello-world = pkgs.callPackage ./test/hello-world { };
hello-world-deps = pkgs.callPackage ./test/hello-world-deps { };
# hello-world-deps-override = pkgs.callPackage ./test/hello-world-deps-override { };
hello-world-external-deps = pkgs.callPackage ./test/hello-world-external-deps { };
hello-world-workspaces = pkgs.callPackage ./test/hello-world-workspaces { };
};
}
)

//

{
overlays.default = final: prev: { inherit noxide; };
overlays.default = final: prev: { lib = prev.lib // { noxide = final.callPackage ./default.nix { }; }; };
githubActions = mkGithubMatrix { checks = getAttrs (attrNames githubPlatforms) self.checks; };
};
}
16 changes: 16 additions & 0 deletions test/empty-package/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ pkgs, lib }:

lib.noxide {
name = "empty-package";
src = lib.sources.cleanSource ./.;
dontNpmBuild = true;
npmDepsHash = "";

doInstallCheck = true;
installCheckPhase = ''
if [ ! -f package.json ]; then
echo "package.json not found in $out"
exit 1
fi
'';
}
24 changes: 24 additions & 0 deletions test/hello-world-deps-override/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ pkgs, lib }:

lib.noxide {
name = "hello-world-deps-override";
src = lib.sources.cleanSource ./.;
dontNpmBuild = true;
# customPatchPackages = { asd = pkgs.callPackage ./../empty-package {}; };
installPhase = ''
mkdir -p $out
cp -r * $out
mkdir -p $out/bin
echo "#!${pkgs.nodejs}/bin/node" > $out/bin/hello-world-deps
echo "require('../main.js')" >> $out/bin/hello-world-deps
chmod +x $out/bin/hello-world-deps
'';

doInstallCheck = true;
installCheckPhase = ''
if [ ! -f package.json ]; then
echo "package.json not found in $out"
exit 1
fi
'';
}
3 changes: 3 additions & 0 deletions test/hello-world-deps-override/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const _ = require('colors');

console.log('Hello World'.green);
23 changes: 23 additions & 0 deletions test/hello-world-deps-override/package-lock.json

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

7 changes: 7 additions & 0 deletions test/hello-world-deps-override/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "hello-world-deps-override",
"version": "1.0.0",
"dependencies": {
"colors": "1.4.0"
}
}
23 changes: 23 additions & 0 deletions test/hello-world-deps/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ pkgs, lib }:

lib.noxide {
name = "hello-world-deps";
src = lib.sources.cleanSource ./.;
dontNpmBuild = true;
installPhase = ''
mkdir -p $out
cp -r * $out
mkdir -p $out/bin
echo "#!${pkgs.nodejs}/bin/node" > $out/bin/hello-world-deps
echo "require('../main.js')" >> $out/bin/hello-world-deps
chmod +x $out/bin/hello-world-deps
'';

doInstallCheck = true;
installCheckPhase = ''
if [ ! -f package.json ]; then
echo "package.json not found in $out"
exit 1
fi
'';
}
24 changes: 24 additions & 0 deletions test/hello-world-external-deps/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ pkgs, lib }:

lib.noxide {
name = "hello-world-external-deps";
src = lib.sources.cleanSource ./.;
dontNpmBuild = true;

installPhase = ''
mkdir -p $out
cp -r * $out
mkdir -p $out/bin
echo "#!${pkgs.nodejs}/bin/node" > $out/bin/hello-world-external-deps
echo "require('../main.js')" >> $out/bin/hello-world-external-deps
chmod +x $out/bin/hello-world-external-deps
'';

doInstallCheck = true;
installCheckPhase = ''
if [ ! -f package.json ]; then
echo "package.json not found in $out"
exit 1
fi
'';
}
Loading

0 comments on commit bdffd4c

Please sign in to comment.