Repository: nodejs/node
Pull request: tools: add some options and comments to `shell.nix`

Triggered by https://github.com/nodejs/node/pull/60902 and https://github.com/nodejs/node/pull/60906

- adding `withAmaro`, `withSQLite`, and `withSSL` parameters (to be consistent with the `withTemporal` one that was recently added, and analogeous to `--without-amaro`, `--without-sqlite`, and `--without-ssl` `configure.py` flags). There are many other `configure.py` flags that do not have equivalent Nix parameter, however those 3 are special as they require a different build environment (that's not yet true for Amaro, but the long term goal is to include it as a native library instead of the current WASM approach)
- reorganizing the list of parameters (the current order doesn't have much logic to it, the file becomes much more readable when the parameters are grouped by their purpose)
- fixing a bug when passing `--arg ccache null` would not work as expected
- making use of `lib.optional`, `lib.optionals`, and `lib.optionalString` more consistent
- introducing a local `needsRustCompiler` as I expect we'll add more Rust compiled dependencies (currently it's only `temporal_capi`)

<!--
Before submitting a pull request, please read:

- the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md
- the commit message formatting guidelines at
  https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

If you believe this PR should be highlighted in the Node.js CHANGELOG
please add the `notable-change` label.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->


Changed files:
filename: shell.nix
patch: @@ -1,54 +1,64 @@
 {
   pkgs ? import ./tools/nix/pkgs.nix { },
-  loadJSBuiltinsDynamically ? true, # Load `lib/**.js` from disk instead of embedding
-  withTemporal ? false,
-  ncu-path ? null, # Provide this if you want to use a local version of NCU
-  icu ? pkgs.icu,
-  sharedLibDeps ? import ./tools/nix/sharedLibDeps.nix { inherit pkgs withTemporal; },
+
+  # Optional build tools / config
   ccache ? pkgs.ccache,
+  loadJSBuiltinsDynamically ? true, # Load `lib/**.js` from disk instead of embedding
   ninja ? pkgs.ninja,
-  devTools ? import ./tools/nix/devTools.nix { inherit pkgs ncu-path; },
-  benchmarkTools ? import ./tools/nix/benchmarkTools.nix { inherit pkgs; },
   extraConfigFlags ? [
     "--without-npm"
     "--debug-node"
-  ]
-  ++ pkgs.lib.optionals withTemporal [
-    "--v8-enable-temporal-support"
   ],
+
+  # Build options
+  icu ? pkgs.icu,
+  withAmaro ? true,
+  withSQLite ? true,
+  withSSL ? true,
+  withTemporal ? false,
+  sharedLibDeps ? import ./tools/nix/sharedLibDeps.nix {
+    inherit
+      pkgs
+      withSQLite
+      withSSL
+      withTemporal
+      ;
+  },
+
+  # dev tools (not needed to build Node.js, useful to maintain it)
+  ncu-path ? null, # Provide this if you want to use a local version of NCU
+  devTools ? import ./tools/nix/devTools.nix { inherit pkgs ncu-path; },
+  benchmarkTools ? import ./tools/nix/benchmarkTools.nix { inherit pkgs; },
 }:
 
 let
   useSharedICU = if builtins.isString icu then icu == "system" else icu != null;
   useSharedAda = builtins.hasAttr "ada" sharedLibDeps;
   useSharedOpenSSL = builtins.hasAttr "openssl" sharedLibDeps;
+
+  needsRustCompiler = withTemporal && !builtins.hasAttr "temporal_capi" sharedLibDeps;
 in
 pkgs.mkShell {
   inherit (pkgs.nodejs_latest) nativeBuildInputs;
 
   buildInputs = builtins.attrValues sharedLibDeps ++ pkgs.lib.optional useSharedICU icu;
 
-  packages = [
-    ccache
-  ]
-  ++ devTools
-  ++ benchmarkTools
-  ++ pkgs.lib.optionals (withTemporal && !builtins.hasAttr "temporal_capi" sharedLibDeps) [
-    pkgs.cargo
-    pkgs.rustc
-  ];
+  packages =
+    pkgs.lib.optional (ccache != null) ccache
+    ++ devTools
+    ++ benchmarkTools
+    ++ pkgs.lib.optionals needsRustCompiler [
+      pkgs.cargo
+      pkgs.rustc
+    ];
 
-  shellHook =
-    if (ccache != null) then
-      ''
-        export CC="${pkgs.lib.getExe ccache} $CC"
-        export CXX="${pkgs.lib.getExe ccache} $CXX"
-      ''
-    else
-      "";
+  shellHook = pkgs.lib.optionalString (ccache != null) ''
+    export CC="${pkgs.lib.getExe ccache} $CC"
+    export CXX="${pkgs.lib.getExe ccache} $CXX"
+  '';
 
   BUILD_WITH = if (ninja != null) then "ninja" else "make";
-  NINJA = if (ninja != null) then "${pkgs.lib.getExe ninja}" else "";
+  NINJA = pkgs.lib.optionalString (ninja != null) "${pkgs.lib.getExe ninja}";
   CI_SKIP_TESTS = pkgs.lib.concatStringsSep "," (
     [ ]
     ++ pkgs.lib.optionals useSharedAda [
@@ -70,12 +80,12 @@ pkgs.mkShell {
       )
     ]
     ++ extraConfigFlags
-    ++ pkgs.lib.optionals (ninja != null) [
-      "--ninja"
-    ]
-    ++ pkgs.lib.optionals loadJSBuiltinsDynamically [
-      "--node-builtin-modules-path=${builtins.toString ./.}"
-    ]
+    ++ pkgs.lib.optional (!withAmaro) "--without-amaro"
+    ++ pkgs.lib.optional (!withSQLite) "--without-sqlite"
+    ++ pkgs.lib.optional (!withSSL) "--without-ssl"
+    ++ pkgs.lib.optional withTemporal "--v8-enable-temporal-support"
+    ++ pkgs.lib.optional (ninja != null) "--ninja"
+    ++ pkgs.lib.optional loadJSBuiltinsDynamically "--node-builtin-modules-path=${builtins.toString ./.}"
     ++ pkgs.lib.concatMap (name: [
       "--shared-${builtins.replaceStrings [ "c-ares" ] [ "cares" ] name}"
       "--shared-${builtins.replaceStrings [ "c-ares" ] [ "cares" ] name}-libpath=${
@@ -86,4 +96,5 @@ pkgs.mkShell {
       }/include"
     ]) (builtins.attrNames sharedLibDeps)
   );
+  NOSQLITE = pkgs.lib.optionalString (!withSQLite) "1";
 }

filename: tools/nix/sharedLibDeps.nix
patch: @@ -1,5 +1,7 @@
 {
   pkgs ? import ./pkgs.nix { },
+  withSQLite ? true,
+  withSSL ? true,
   withTemporal ? false,
 }:
 {
@@ -12,7 +14,6 @@
     ngtcp2
     simdjson
     simdutf
-    sqlite
     uvwasi
     zlib
     zstd
@@ -28,6 +29,11 @@
       })
     ];
   };
+}
+// (pkgs.lib.optionalAttrs withSQLite {
+  inherit (pkgs) sqlite;
+})
+// (pkgs.lib.optionalAttrs withSSL {
   openssl = pkgs.openssl.overrideAttrs (old: {
     version = "3.5.4";
     src = pkgs.fetchurl {
@@ -45,7 +51,7 @@
       "dev"
     ];
   });
-}
+})
 // (pkgs.lib.optionalAttrs withTemporal {
   inherit (pkgs) temporal_capi;
 })