Using a "python-with-packages" as buildInput for the ansible package/derivation?

Dear all,

the following code (for home-manager) gives me a python that has the kubernetes and hvac python modules available.

    home.packages = with pkgs; [
      ansible
      (pkgs.python3.withPackages (python-pkgs: [
        python-pkgs.hvac
        python-pkgs.kubernetes
      ]))
    ];

(In my case today this is pointing to /nix/store/ipwx571dlhxmdf1n1yd8vgqq5ndjnxba-python3-3.12.8-env/bin/python3)

The following snippet gives me a Ansible that find additional modules (like kubernetes and hvac):

     packageOverrides = pkgs: {
       ansible = pkgs.ansible.overrideAttrs (oldAttrs: {
         propagatedBuildInputs = oldAttrs.propagatedBuildInputs ++ [
           pkgs.python3Packages.hvac
           pkgs.python3Packages.kubernetes
         ];
       });
     };

Is there a way to have ansible use that python (that has the modules available) as its python interpreter?

What I mean is: which ansible points to a link in ~/.nix-profiles/bin/ which points to a bash script that calls a .ansible-wrapped file, that has a shebang pointing to a python3.12 binary in the nix store:

$ head -1 /nix/store/0cbxbilzv11xs6fb2fnsn1zrizdmyjkq-python3.12-ansible-core-2.17.6/bin/.ansible-wrapped
/nix/store/lhpwdis5hkyljz1d200bj1s6g51ljq9k-python3-3.12.8/bin/python3.12
$

Is it possible to build the ansible package/derivation using the “python with modules” as a build input? So that the .ansible-wrapped script (and thus Ansible) are using this python?

Using the first code snippet in the pkgs.ansible.overrideAttrs only gives me Package duplicates found in closure errors…

(My issue is that calling that python3.12 binary without the additional PATH-mangling done in the .ansible-wrapped does not have the additional modules available. Which is needed as Ansible falls back to using “its own” python as interpreter in some cases. And then cannot find the modules it might need)

Kind Regards,
Johannes

P.S.: Yes, there has been a lot of discussion on this on a mastodon thread and I already got a lot of help there. But I am missing the last part of the puzzle.

1 Like

A hack like this might help:

Btw. I didn’t see it in our Mastodon discussion.
Did you try it this way already?

(pkgs.python3.withPackages (python-pkgs: [
        python-pkgs.ansible
        python-pkgs.ansible-core
        python-pkgs.hvac
        python-pkgs.kubernetes
      ]))

Thanks for the link, @Nebucatnetzer
Regarding the hack, I would like to solve this on NixOS, otherwise I would need to implement this into each and every demo setup I create (and make sure it is only used on NixOS, as it might cause issues for other people trying those setups).

As for the second suggestion, I am trying this currently. It seems “the wrong way around” to define ansible as a package for the python installation, but I understand why this might work…

1 Like

Ha, seems like this really did the trick!

I’ll do some further testing but I could successfully use a vagrant setup that has kubernetes-related modules run on localhost!

Thanks for the hint (and for the reminder) @Nebucatnetzer

Glad I could help :slight_smile:

1 Like