1. I find it very helpful to work on something I think is actually important. Lots of companies are working on stuff that... isn't. My personal list is here, yours will no doubt differ: https://codewithoutrules.com/2017/08/07/do-something-useful/
2. Align all your goals together. Getting paid, and working on something you care about, and not just going helter skelter, and so on.
Lots of programming work is necessary but boring, or necessarily slapdash; you need to fully understand why you're doing it, and make sure you really believe it's necessary, or yeah, it doesn't work.
More on that here: https://codewithoutrules.com/2017/08/03/stay-focused/
But there are places where you can find a reasonable compromise. I would look at companies and departments where your work is the main product and not just a cost center. Life is much better where management likes technology and not just deadlines.