Can you extend the duration of armor of Agathys by gaining more temporary hit points?

The spell is meant to work only as long as you have the temporary hit points that the spell grants. When those temporary hit points are gone, the spell is done.

Keep in mind that temporary hit points aren’t cumulative (see PH, 198). If you have temporary hit points and receive more of them, you don’t add them together, unless a game feature says you can. You decide which temporary hit points to keep. As an example, let’s say you’re a warlock with the Dark One’s Blessing feature, which gives you temporary hit points when you reduce a creature to 0 hit points. You currently have 2 temporary hit points remaining from armor of Agathys, you just slew a monster, and your Dark One’s Blessing can now give you 4 temporary hit points. If you take those temporary hit points, they replace the ones from armor of Agathys and end that spell, so you might not want to take them and keep the spell going instead, so as to continue benefiting from the cold damage it delivers.