Is it possible to derive the principle of least time from the principle of least action in lagrangian or hamiltonian mechanics? Or is Fermat's principle more fundamental than the principle of least action?
Yes, but note that the principle of least time is a special case of the principle of least action. Fermat's principle only applies to geometrical optics, i.e., where we are considering the trajectory of a ray of light, which always travels « in a straight line » at a speed determined by the index of refraction at the particular point it is at.
Therefore the following discussion is only about classical mechanics, with point particles and definite trajectories. There, it will be seen that ulitmately both principles are equivalent. But in the larger context of quantum physics, it is the principle of least action which generalises and not, as far as I know, Fermat`s principle.
The principle of least action was, historically, a generalisation of Fermat's principle, motivated by the rival corpuscular theory of light. Then the new principle was later seen to apply in greater generality, to all particle dynamics.
Gauss and Hertz found a re-formulation of the principle of least action: they were able to define a notion of curvature in the abstract configuration space of a system of particles, see Whittaker, Analytical Dynamics, p. 254, and found that the laws of dynamics followed from a « principle of least curvature.» the system will follow the trajectory which has at each point the least curvature. Felix Klein generalised this further. He put a non-Euclidean geometry on this abstract space describing the trajectory of the system and formulated the law that the actual path is a geodesic in this geometry. This is not the same as Einstein's theory of general relativity since it is an abstract space with a high number of dimensions, as always in Hamiltonian Mechanics.
Using Klein's pint of view, the principle of least action can be deduced back from the principle of least time: abstractly, the paths in this non-Euclidean space can be regarded as the quickest paths given an artificially defined « index of refraction » in this space, defined at each point. So perhaps neither is more fundamental than the other. But from a physically intuitive point of view, perhaps one could justify saying that the principle of least action is more fundamental since Klein's construction could be thought of as rather artificial. see also http://math.ucr.edu/home/baez/classical/ and also Klein's book on the history of mathematics in the 19th century, a wonderful book which should be every mathematical physicist's bedside reading... but I cannot find my copy right this instant...