A deal to avert a constitutional crisis within the UK as the country exits the European Union (EU) is "certainly achievable", a former senior Whitehall civil servant has told MSPs.

The UK Government is in talks with the devolved administrations over whether the European Union (Withdrawal) Bill needs to be changed after the first ministers of both Scotland and Wales branded it a power grab.

Professor Jim Gallagher, who was Whitehall's most senior civil servant concerned with devolution until he retired in June 2010, said the talks would "probably" be successful - but warned this would only happen if politicians on all sides are "not obsessed by the notion of sovereignty and the gaining of power".

The Scottish and Welsh governments have already made clear they cannot currently recommend legislative consent is given to the Bill - which aims to transpose EU law into British law so the same rules apply on the day of Brexit as the day before.

Clause 11 of the proposed legislation will see EU responsibilities in devolved areas initially transferred to Westminster - with UK ministers arguing this will allow common frameworks to be established across Britain before further devolution.

Westminster could override the objections of Edinburgh and Cardiff and pass the legislation without the consent of the devolved administrations - a move which would almost certainly spark a constitutional crisis.

However, Prof Gallagher, who served as Director-General for Devolution in the Cabinet Office, the No 10 Policy Unit and Ministry of Justice, said it was "significant" that UK ministers had said they wanted to get their agreement.

He was questioned on the talks as he appeared before MSPs on the Scottish Parliament's Finance and Constitution Committee.

Prof Gallagher, a research fellow at Oxford University's Nuffield College, said: "I think the process of negotiation that is under way needs to work and I think it probably will work."

He suggested the "right thing to do" would be for the UK Government to make clause 11 subject to a "sunset clause" - so it only applies for a certain period of time after which the powers would be passed from Westminster to the devolved administrations.

He said: "My impression is that negotiations between the governments have actually begun in earnest, in a way in which they hadn't earlier, partly because we need to put some of the politics aside here and find a solution to what is a very difficult operational system for many people.

"These negotiations will succeed if and only if each side is not obsessed by the notion of sovereignty and the gaining of power, but is actually looking for things that work in the real world.

"If we focus on what needs to be done it is in the context of an almost impossible project of leaving the EU, this part is certainly achievable."

Prof Gallagher told the committee clause 11 - which transfers power from Brussels to Westminster before further devolution at a later point - was defensible "provided that the approach of reserving everything till it is actually devolved is something that lasts for a defined period of time rather than permanently".

However, other experts told MSPs clause 11 was not an appropriate way of dealing with the situation.

Professor Aileen McHarg, a professor of public law at the University of Strathclyde, said: "It may be justifiable as a transitional measure but it is not, in fact, a transitional measure.

"Unlike many of the other provisions of the EU Withdrawal Bill, there is no sunset clause. It presents as an indefinite solution and it is not an appropriate indefinite solution."

She made clear she did "not think this would be a situation in which the UK Parliament would be justified in over-riding the refusal of devolved consent".