In situations like the one you explain there are three countries and jurisdictions involved:
- Home country (H): The country which nationality you have.
- Residence / work country (R): The country you live and work in de facto
- Client's country (C): The country where the company which employs you or contracts you has its seat
In your case: H = Portugal, R = Luxembourg, C = you're not so clear on this
If H = R = C, it's simple.
If R = C, it's quite a common case.
If H != R != C then it can get complicated.
You will have to have a look at the following areas:
- residence and / or work permit in country R
- work permit in country C
- social security
On taxation, the rule of thumb is that you need to pay your taxes in country R. Country R is usually defined as the country in which you spend 183 days a year or more. (NB: I wonder what would apply if I split my time between 3 countries with ~ 120 days each.)
This does not mean that you are not obliged to pay any taxes in country C. Just maybe you can deduct them from what you need to pay in R. This also depends a lot on if you are an employee in C or you provide services to a customer in C. This is indeed what you will usually find in a double-taxation agreement.
It may be a bit of a simplified view, but social security is something that comes with employment. If you are self-employed then you're usually off that game. So I would argue that if you are an employee of a company in C, then C's social security system is in charge of you. Which can cause funny side effects, like you might have an Irish health insurance but the doctor you want to see in Luxembourg might no be able to treat you on this basis. This is where the EU's common market isn't nearly what it should be, let alone if a non-EU (read: UK) context kicks in.
On the residence and work permit questions: You only name EU member countries, so there is no big issues around this usually. In other contexts, i.e. with non-EU countries like Switzerland, Ukraine, India, USA, ... this may be a lot different.
One should say that many of the rules around "am I allowed to work in this country" have been made in times where digital remote work wasn't a broad reality. In practice, today, it frequently comes down more to "who will ask me" or "who will care" a lot more than: What is the legal setup.