Unverified Commit 16917d0c authored by zowoq's avatar zowoq Committed by GitHub
Browse files

opentofu: fix `withPlugins` to properly propagate package (#447849)

parents 5f3726c1 16451ff3
Loading
Loading
Loading
Loading
+91 −87
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ let
    patches = [ ./provider-path-0_15.patch ];

    passthru = {
      inherit full plugins withPlugins;
      inherit plugins;
      tests = {
        inherit opentofu_plugins_test;
      };
@@ -73,8 +73,6 @@ let
    };
  };

  full = withPlugins (p: lib.filter lib.isDerivation (lib.attrValues p.actualProviders));

  opentofu_plugins_test =
    let
      mainTf = writeText "main.tf" ''
@@ -88,7 +86,7 @@ let

        resource "random_id" "test" {}
      '';
      opentofu = package.withPlugins (p: [ p.random ]);
      opentofu = (pluggable package).withPlugins (p: [ p.random ]);
      test = runCommand "opentofu-plugin-test" { buildInputs = [ opentofu ]; } ''
        # make it fail outside of sandbox
        export HTTP_PROXY=http://127.0.0.1:0 HTTPS_PROXY=https://127.0.0.1:0
@@ -105,10 +103,13 @@ let
    "recurseForDerivations"
  ];

  pluggable =
    opentofu:
    let
      withPlugins =
        plugins:
        let
      actualPlugins = plugins package.plugins;
          actualPlugins = plugins opentofu.plugins;

          # Wrap PATH of plugins propagatedBuildInputs, plugins may have runtime dependencies on external binaries
          wrapperInputs = lib.unique (
@@ -117,6 +118,7 @@ let

          passthru = {
            withPlugins = newplugins: withPlugins (x: newplugins x ++ actualPlugins);
            full = withPlugins (p: lib.filter lib.isDerivation (lib.attrValues p.actualProviders));

            # Expose wrappers around the override* functions of the terraform
            # derivation.
@@ -138,28 +140,28 @@ let
            # 3. Specifying overrides on the wrapper is unsupported.
            #
            # See nixpkgs#158620 for details.
        overrideDerivation = f: (package.overrideDerivation f).withPlugins plugins;
        overrideAttrs = f: (package.overrideAttrs f).withPlugins plugins;
        override = x: (package.override x).withPlugins plugins;
            overrideDerivation = f: (pluggable (opentofu.overrideDerivation f)).withPlugins plugins;
            overrideAttrs = f: (pluggable (opentofu.overrideAttrs f)).withPlugins plugins;
            override = x: (pluggable (opentofu.override x)).withPlugins plugins;
          };
        in
        # Don't bother wrapping unless we actually have plugins, since the wrapper will stop automatic downloading
        # of plugins, which might be counterintuitive if someone just wants a vanilla Terraform.
        if actualPlugins == [ ] then
      package.overrideAttrs (orig: {
          opentofu.overrideAttrs (orig: {
            passthru = orig.passthru // passthru;
          })
        else
          lib.appendToName "with-plugins" (
            stdenv.mkDerivation {
          inherit (package) meta pname version;
              inherit (opentofu) meta pname version;
              nativeBuildInputs = [ makeWrapper ];

              # Expose the passthru set with the override functions
              # defined above, as well as any passthru values already
              # set on `terraform` at this point (relevant in case a
              # user overrides attributes).
          passthru = package.passthru // passthru;
              passthru = opentofu.passthru // passthru;

              buildCommand = ''
                # Create wrappers for terraform plugins because OpenTofu only
@@ -183,11 +185,13 @@ let

                # Create a wrapper for opentofu to point it to the plugins dir.
                mkdir -p $out/bin/
            makeWrapper "${package}/bin/tofu" "$out/bin/tofu" \
                makeWrapper "${opentofu}/bin/tofu" "$out/bin/tofu" \
                  --set NIX_TERRAFORM_PLUGIN_DIR $out/libexec/terraform-providers \
                  --prefix PATH : "${lib.makeBinPath wrapperInputs}"
              '';
            }
          );
    in
package
    withPlugins (_: [ ]);
in
pluggable package