If we make piles of sand or of sugar, they form cones; the maximum slope of the cones depends on the material. Is there a formula which gives this slope (or the angle of the cone), given some quantities such as density, dimensions of the grains, etc.?
This is the nearest I could find to a formula, Calculating Angle of Repose. It's a 73 page thesis, based on computer models, experiments such as the one below and assumptions covering the particular points in your question, such as density and grain size.
This below is all from Wikipedia, and there is also a link on this site , but no equations unfortunately, at Slopes of Granulated Material, which provides more details.
I will happily delete this answer if anybody comes up with a math based rather than experimentally derived formula.
Methods in determining the angle of repose
Tilting box method
This method is appropriate for fine-grained, non-cohesive materials, with individual particle size less than 10 mm. The material is placed within a box with a transparent side to observe the granular test material. It should initially be level and parallel to the base of the box. The box is slowly tilted at a rate of approximately 0.3 degrees/second. Tilting is stopped when the material begins to slide in bulk, and the angle of the tilt is measured.
Fixed funnel method
The material is poured through a funnel to form a cone. The tip of the funnel should be held close to the growing cone and slowly raised as the pile grows, to minimize the impact of falling particles. Stop pouring the material when the pile reaches a predetermined height or the base a predetermined width. Rather than attempt to measure the angle of the resulting cone directly, divide the height by half the width of the base of the cone. The inverse tangent of this ratio is the angle of repose.
Revolving cylinder method
The material is placed within a cylinder with at least one transparent face. The cylinder is rotated at a fixed speed and the observer watches the material moving within the rotating cylinder. The effect is similar to watching clothes tumble over one another in a slowly rotating clothes dryer. The granular material will assume a certain angle as it flows within the rotating cylinder. This method is recommended for obtaining the dynamic angle of repose, and may vary from the static angle of repose measured by other methods. When describing the angle of repose for a substance, always specify the method used.