From e12ae8b7bb23ed9af4e3366c19a6d7cb6ea33874 Mon Sep 17 00:00:00 2001
From: "Manu [tennox]" <2084639+tennox@users.noreply.github.com>
Date: Tue, 18 Oct 2022 16:26:44 +0100
Subject: [PATCH] feat: add language.nodejs extra

---
 extra/language/nodejs.nix | 52 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 extra/language/nodejs.nix

diff --git a/extra/language/nodejs.nix b/extra/language/nodejs.nix
new file mode 100644
index 00000000..31011c05
--- /dev/null
+++ b/extra/language/nodejs.nix
@@ -0,0 +1,52 @@
+{ lib, config, pkgs, ... }:
+let
+  cfg = config.language.nodejs;
+  strOrPackage = import ../../nix/strOrPackage.nix { inherit lib pkgs; };
+in
+with lib;
+{
+  options.language.nodejs = {
+    package = mkOption {
+      type = strOrPackage;
+      default = pkgs.nodejs; # latest
+      example = literalExpression "pkgs.nodejs-18_x";
+      description = "Which nodejs package to use";
+    };
+  };
+
+  config = {
+    devshell.packages = [
+      cfg.package # nodejs itself
+
+      # TODO: npm
+
+      # Yarn
+      (pkgs.yarn.override {
+        nodejs = cfg.package;
+      })
+
+      # Pnpm
+      (pkgs.nodePackages.pnpm.override {
+        nodejs = cfg.package; # sadly doesn't suffice, but I found this:
+
+        # From discourse: https://discourse.nixos.org/t/how-to-use-pnpm-with-recent-nodejs/21867/2?u=tennox
+        nativeBuildInputs = [ pkgs.makeWrapper ];
+        preRebuild = ''
+          sed 's/"link:/"file:/g' --in-place package.json
+        '';
+        postInstall =
+          let
+            pnpmLibPath = pkgs.lib.makeBinPath [
+              cfg.package.passthru.python
+              cfg.package
+            ];
+          in
+          ''
+            for prog in $out/bin/*; do
+              wrapProgram "$prog" --prefix PATH : ${pnpmLibPath}
+            done
+          '';
+      })
+    ];
+  };
+}