From 0ae2e31243ce1ea78522d719d91c969624093e5d Mon Sep 17 00:00:00 2001 From: phanirithvij Date: Tue, 15 Apr 2025 16:38:46 +0530 Subject: [PATCH] dprint-plugins: getPluginList helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: phanirithvij Co-authored-by: Jörg Thalheim --- pkgs/by-name/dp/dprint/plugins/default.nix | 48 ++++++++++++------- .../dp/dprint/plugins/update-plugins.py | 10 ++-- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/pkgs/by-name/dp/dprint/plugins/default.nix b/pkgs/by-name/dp/dprint/plugins/default.nix index 0c0d2ab0b492..391a23e3e4cb 100644 --- a/pkgs/by-name/dp/dprint/plugins/default.nix +++ b/pkgs/by-name/dp/dprint/plugins/default.nix @@ -1,4 +1,8 @@ -{ lib, fetchurl }: +{ + lib, + fetchurl, + stdenv, +}: let mkDprintPlugin = { @@ -12,28 +16,32 @@ let license ? lib.licenses.mit, maintainers ? [ lib.maintainers.phanirithvij ], }: - fetchurl { - inherit - hash - url - pname - version - ; - name = "${pname}-${version}.wasm"; + stdenv.mkDerivation (finalAttrs: { + inherit pname version; + src = fetchurl { inherit url hash; }; + dontUnpack = true; meta = { - inherit - description - license - maintainers - ; + inherit description license maintainers; }; + /* + in the dprint configuration + dprint expects a plugin path to end with .wasm extension + + for auto update with nixpkgs-update to work + we cannot have .wasm extension at the end in the nix store path + */ + buildPhase = '' + mkdir -p $out + cp $src $out/plugin.wasm + ''; passthru = { updateScript = ./update-plugins.py; inherit initConfig updateUrl; }; - }; + }); inherit (lib) filterAttrs + isDerivation mapAttrs' nameValuePair removeSuffix @@ -45,5 +53,13 @@ let name: _: nameValuePair (removeSuffix ".nix" name) (import (./. + "/${name}") { inherit mkDprintPlugin; }) ) files; + # Expects a function that receives the dprint plugin set as an input + # and returns a list of plugins + # Example: + # pkgs.dprint-plugins.getPluginList (plugins: [ + # plugins.dprint-plugin-toml + # (pkgs.callPackage ./dprint/plugins/sample.nix {}) + # ] + getPluginList = cb: map (p: "${p}/plugin.wasm") (cb plugins); in -plugins // { inherit mkDprintPlugin; } +plugins // { inherit mkDprintPlugin getPluginList; } diff --git a/pkgs/by-name/dp/dprint/plugins/update-plugins.py b/pkgs/by-name/dp/dprint/plugins/update-plugins.py index 9a3bd7e38cae..f065d673152b 100755 --- a/pkgs/by-name/dp/dprint/plugins/update-plugins.py +++ b/pkgs/by-name/dp/dprint/plugins/update-plugins.py @@ -29,11 +29,9 @@ else: # get sri hash for a url, no unpack -def nix_prefetch_url(url, name, algo="sha256"): +def nix_prefetch_url(url, algo="sha256"): hash = ( - subprocess.check_output( - ["nix-prefetch-url", "--type", algo, "--name", name, url] - ) + subprocess.check_output(["nix-prefetch-url", "--type", algo, url]) .decode("utf-8") .rstrip() ) @@ -119,7 +117,7 @@ def update_plugin_by_name(name): data = requests.get(p["updateUrl"]).json() p["url"] = data["url"] p["version"] = data["version"] - p["hash"] = nix_prefetch_url(data["url"], f"{name}-{data["version"]}.wasm") + p["hash"] = nix_prefetch_url(data["url"]) write_plugin_derivation(p) @@ -136,7 +134,7 @@ def update_plugins(): pname = pname.replace("/", "-") drv_attrs = { "url": e["url"], - "hash": nix_prefetch_url(e["url"], f"{pname}-{e["version"]}.wasm"), + "hash": nix_prefetch_url(e["url"]), "updateUrl": update_url, "pname": pname, "version": e["version"],