From 8215ac6941378c779fac854dabde5e80275e764b Mon Sep 17 00:00:00 2001 From: adisbladis Date: Tue, 14 Jul 2020 14:34:03 +0200 Subject: [PATCH] Add experimental support for plugin closures --- default.nix | 5 ++++ plugins.nix | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 plugins.nix diff --git a/default.nix b/default.nix index a07e4a5..9ec1657 100644 --- a/default.nix +++ b/default.nix @@ -22,6 +22,9 @@ let # Get license by id falling back to input string getLicenseBySpdxId = spdxId: spdxLicenses.${spdxId} or spdxId; + # Experimental withPlugins functionality + toPluginAble = (import ./plugins.nix { inherit pkgs lib; }).toPluginAble; + /* Returns an attrset { python, poetryPackages, pyProject, poetryLock } for the given pyproject/lockfile. */ @@ -106,6 +109,8 @@ let # The canonical name is setuptools-scm setuptools-scm = super.setuptools_scm; + __toPluginAble = toPluginAble self; + inherit (hooks) pipBuildHook removePathDependenciesHook poetry2nixFixupHook; } ) diff --git a/plugins.nix b/plugins.nix new file mode 100644 index 0000000..b5e807c --- /dev/null +++ b/plugins.nix @@ -0,0 +1,70 @@ +{ pkgs, lib }: +let + inherit (pkgs) stdenv; + + mkPluginDrv = + { self + , plugins + , drv + , postInstall ? "" + , nativeBuildInputs ? [ ] + , buildInputs ? [ ] + }: + let + env = self.python.withPackages (ps: plugins); + in + stdenv.mkDerivation { + pname = drv.pname + "-with-plugins"; + + inherit (drv) src version meta; + + buildInputs = drv.buildInputs ++ drv.propagatedBuildInputs ++ buildInputs; + nativeBuildInputs = drv.nativeBuildInputs ++ nativeBuildInputs; + + dontConfigure = true; + dontBuild = true; + dontUsePythonRecompileBytecode = true; + + passthru = { + inherit (drv.passthru) withPlugins; + inherit plugins; + }; + + # Link bin/ from environment, but only if it's in a plugin + installPhase = '' + runHook preInstall + + mkdir -p $out/bin + + for bindir in ${lib.concatStringsSep " " (map (d: "${lib.getBin d}/bin") plugins)}; do + for bin in $bindir/*; do + ln -s ${env}/bin/$(basename $bin) $out/bin/ + done + done + + runHook postInstall + ''; + + inherit postInstall; + }; + +in +{ + + # Provide the `withPlugins` function + toPluginAble = self: { drv + , finalDrv + , postInstall ? "" + , nativeBuildInputs ? [ ] + , buildInputs ? [ ] + }: drv.overridePythonAttrs (old: { + passthru = old.passthru // { + withPlugins = pluginFn: mkPluginDrv { + plugins = [ finalDrv ] ++ pluginFn self; + inherit self postInstall nativeBuildInputs buildInputs; + drv = finalDrv; + }; + }; + }); + +}