Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Add support for `buildNpmPackage`

## v0.3.6 - 2026-05-15

### Fixes
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Options:
- `buildRustPackage`
- `buildPythonApplication` and `buildPythonPackage`
- `buildGoModule`
- `buildNpmPackage`

### Supported fetchers

Expand Down
5 changes: 5 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Builder {
application: bool,
rust: Option<CargoVendor>,
},
BuildNpmPackage,
BuildRustPackage {
vendor: CargoVendor,
},
Expand Down Expand Up @@ -40,6 +41,10 @@ impl Display for Builder {
}
}

Builder::BuildNpmPackage => {
write!(f, "buildNpmPackage")?;
}

Builder::BuildRustPackage { vendor } => {
write!(
f,
Expand Down
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct Opts {
#[clap(rename_all = "camelCase")]
pub enum BuilderFunction {
BuildGoModule,
BuildNpmPackage,
BuildPythonApplication,
BuildPythonPackage,
BuildRustPackage,
Expand Down
1 change: 1 addition & 0 deletions src/lang/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod go;
pub mod node;
pub mod python;
pub mod rust;
32 changes: 32 additions & 0 deletions src/lang/node/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::{fs::File, io::BufReader, path::Path};

use serde::Deserialize;
use tracing::warn;

use crate::utils::ResultExt;

#[derive(Deserialize)]
struct PackageJson {
#[serde(default)]
scripts: Scripts,
}

#[derive(Default, Deserialize)]
struct Scripts {
build: Option<String>,
}

// assumes a build script exists when package.json can't be read or parsed
pub fn npm_lacks_build_script(src_dir: &Path) -> bool {
let Some(file) = File::open(src_dir.join("package.json")).ok_inspect(|e| warn!("{e}")) else {
return false;
};

match serde_json::from_reader::<_, PackageJson>(BufReader::new(file)) {
Ok(package) => package.scripts.build.is_none(),
Err(e) => {
warn!("{e}");
false
}
}
}
51 changes: 51 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use crate::{
inputs::{AllInputs, write_all_lambda_inputs, write_inputs, write_lambda_input},
lang::{
go::{load_go_dependencies, write_ldflags},
node::npm_lacks_build_script,
python::{Pyproject, PythonDependencies, parse_requirements_txt},
rust::{cargo_deps_hash, load_cargo_lock, write_cargo_lock},
},
Expand Down Expand Up @@ -396,13 +397,17 @@ async fn run() -> Result<()> {
let has_cmake = src_dir.join("CMakeLists.txt").is_file();
let has_go = src_dir.join("go.mod").is_file();
let has_meson = src_dir.join("meson.build").is_file();
let has_npm = src_dir.join("package.json").is_file();
let has_npm_lock = src_dir.join("package-lock.json").is_file()
|| src_dir.join("npm-shrinkwrap.json").is_file();
let has_zig = src_dir.join("build.zig").is_file();
let pyproject_toml = src_dir.join("pyproject.toml");
let has_python = pyproject_toml.is_file() || src_dir.join("setup.py").is_file();

let builder = match (opts.builder, opts.cargo_vendor) {
(Some(builder), rust @ Some(vendor)) if has_cargo => match builder {
BuilderFunction::BuildGoModule => Builder::BuildGoModule,
BuilderFunction::BuildNpmPackage => Builder::BuildNpmPackage,
BuilderFunction::BuildPythonApplication => Builder::BuildPythonPackage {
application: true,
rust,
Expand All @@ -419,6 +424,7 @@ async fn run() -> Result<()> {
let rust = has_cargo.then_some(CargoVendor::FetchCargoVendor);
match builder {
BuilderFunction::BuildGoModule => Builder::BuildGoModule,
BuilderFunction::BuildNpmPackage => Builder::BuildNpmPackage,
BuilderFunction::BuildPythonApplication => Builder::BuildPythonPackage {
application: true,
rust,
Expand Down Expand Up @@ -471,6 +477,10 @@ async fn run() -> Result<()> {
}
}

if has_npm {
builders.push(Builder::BuildNpmPackage);
}

builders.push(Builder::MkDerivation { rust: None });
builders.push(Builder::MkDerivationNoCC);

Expand Down Expand Up @@ -512,6 +522,9 @@ async fn run() -> Result<()> {
Builder::BuildGoModule => {
writeln!(out, " buildGoModule,")?;
}
Builder::BuildNpmPackage => {
writeln!(out, " buildNpmPackage,")?;
}
Builder::BuildPythonPackage { application, rust } => {
writeln!(
out,
Expand Down Expand Up @@ -637,6 +650,44 @@ async fn run() -> Result<()> {
res
}

Builder::BuildNpmPackage => {
let hash = if has_npm_lock
&& let Some(hash) = fod_hash(format!(
r#"(import({nixpkgs}){{}}).fetchNpmDeps{{src={src};hash="{FAKE_HASH}";}}"#,
))
.await
{
hash
} else {
FAKE_HASH.into()
};

let res = write_all_lambda_inputs(&mut out, &inputs, &mut BTreeSet::new())?;
if nix_update_script {
writeln!(out, " nix-update-script,")?;
}

writedoc! {out, r#"
}}:

buildNpmPackage (finalAttrs: {{
pname = {pname:?};
version = {version:?};
__structuredAttrs = true;

src = {src_expr};

npmDepsHash = "{hash}";

"#}?;

if npm_lacks_build_script(&src_dir) {
writeln!(out, " dontNpmBuild = true;\n")?;
}

res
}

Builder::BuildPythonPackage { application, rust } => {
enum CargoVendorData {
Hash(String),
Expand Down
34 changes: 34 additions & 0 deletions tests/cmd/npm.out/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
lib,
buildNpmPackage,
fetchFromGitHub,
nix-update-script,
}:

buildNpmPackage (finalAttrs: {
pname = "cowsay";
version = "1.5.0";
__structuredAttrs = true;

src = fetchFromGitHub {
owner = "piuccio";
repo = "cowsay";
tag = "v${finalAttrs.version}";
hash = "sha256-TZ3EQGzVptNqK3cNrkLnyP1FzBd81XaszVucEnmBy4Y=";
};

npmDepsHash = "sha256-MIvLeuElaN9IbdB+SMgOLNTeycaK0k/M/R+xRxSD4U8=";

dontNpmBuild = true;

passthru.updateScript = nix-update-script { };

meta = {
description = "[..]";
homepage = "https://github.com/piuccio/cowsay";
changelog = "https://github.com/piuccio/cowsay/releases/tag/${finalAttrs.src.tag}";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ alice ];
mainProgram = "cowsay";
};
})
1 change: 1 addition & 0 deletions tests/cmd/npm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
args = ["--headless", "--url=https://github.com/piuccio/cowsay", "--rev=v1.5.0"]