{
  config,
  lib,
  pkgs,
  ...
}:

{
  imports = [ ];

  options.me = {
    ansible.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = "Whether we want to install ansible.";
    };
  };

  config = lib.mkIf config.me.ansible.enable (
    lib.mkMerge [
      {
        environment.systemPackages = with pkgs; [
          ansible
        ];

        nixpkgs.overlays = [
          (final: prev: {
            ansible = pkgs.symlinkJoin {
              name = "ansible";
              paths = [
                (prev.ansible.overridePythonAttrs {
                  propagatedBuildInputs = prev.ansible.propagatedBuildInputs ++ [ prev.python3Packages.jmespath ];
                })
                pkgs.ansible-sshjail
              ];
              buildInputs = [ pkgs.makeWrapper ];

              postBuild = ''
                ${lib.concatMapStringsSep "\n"
                  (
                    prog:
                    (
                      "wrapProgram $out/bin/${prog} ${
                        lib.concatMapStringsSep " "
                          (
                            plugin_type:
                            "--set ANSIBLE_${lib.toUpper plugin_type}_PLUGINS $out/share/ansible/plugins/${lib.toLower plugin_type}_plugins"
                          )
                          [
                            "action"
                            "cache"
                            "callback"
                            "connection"
                            "filter"
                            "inventory"
                            "lookup"
                            "shell"
                            "strategy"
                            "test"
                            "vars"
                          ]
                      } --prefix PATH : ${lib.makeBinPath [ ]}"
                    )
                  )
                  [
                    "ansible"
                    "ansible-config"
                    "ansible-console"
                    "ansible-doc"
                    "ansible-galaxy"
                    "ansible-inventory"
                    "ansible-playbook"
                    "ansible-pull"
                    "ansible-test"
                    "ansible-vault"
                  ]
                }
              '';
            };
          })
        ];
      }
    ]
  );
}