A compound of formula or a pharmaceutically acceptable salt thereof: wherein: X is CH or N; R is H, halo, -OH, C 1-4 alkyl, C 1-4 alkoxy, C 1-4 haloalkyl or C 1-4 haloalkoxy; R 1 is H, C 1-4 alkyl, C 1-4 haloalkyl, or 3-6 membered cycloalkyl; R 2 and R 3 are each, independently, H or C 1-4 alkyl; R 4 is halo, -C 6 H 4 -R 6, -(4-10 membered heterocyclyl)-R 6, -(5-10 membered heteroaryl)-R 6, -(4-6 membered carbocyclyl)-R 6, a 6-10 membered fused heterocyclyl-aryl optionally substituted with 1-3 C 1-4 alkyl groups, or a 6-10 membered fused heterocyclyl-heteroaryl optionally substituted with 1-3 C 1-4 alkyl groups; R 5 is H, or R 4 and R 5 together with the carbon atoms to which they are attached form a 5-12 membered carbocyclic, heterocyclic, aryl, heteroaryl, fused heterocyclyl-aryl, fused heterocyclyl-heteroaryl, fused carbocyclyl-aryl, or fused carbocyclyl-heteroaryl ring, wherein said carbocyclic, heterocyclic, aryl, heteroaryl, fused heterocyclyl-aryl, fused heterocyclyl-heteroaryl, fused carbocyclyl-aryl, or fused carbocyclyl-heteroaryl ring is optionally substituted with 1-3 C 1-4 alkyl groups; R 6 is -(CH 2) m - n -(CH 2) p -R 6a, -(CH 2) m - n -(CH 2) p -R 6b, or -(CH 2) m - n -(CH 2) p -R 6c; R 6a is H, -OH, -NR 7 R 8, -CN, oxo, C 1-4 alkyl, C 1-4 haloalkyl, -CH(NR 7 R 8)-C 1-3 haloalkyl, - CH(NR 7 R 8)-C 1-3 alkyl, -SO 2 C 1-4 alkyl, or -SO 2 NR 7 R 8; R 6b is a 4-10 membered heterocyclyl optionally substituted with 1-3 substituents independently selected from a group consisting of -NR 7 R 8, -C 1-4 alkyl, -C 1-4 hydroxyalkyl, and -C 1-4 alkyl-NR 7 R 8; R 6c is a 5-10 membered heteroaryl optionally substituted with 1-3 substituents independently selected from a group consisting of -NR 7 R 8, -C 1-4 alkyl, -C 1-4 hydroxyalkyl, and -C 1-4 alkyl-NR 7 R 8; R 6d is a 4-10 membered carbocyclyl optionally substituted with 1-3 substituents independently selected from a group consisting of halo, -OH, -NR 7 R 8, -C 1-4 alkyl, -C 1-4 haloalkyl, - C 1-4 hydroxyalkyl, -C 1-4 alkyl-NR 7 R 8, -SO 2 C 1-4 alkyl, and 4-6 membered heterocyclyl; R 7 and R 8 are each, independently, selected from a group consisting of H, C 1-4 alkyl, C 1-4 hydroxyalkyl, a 4-6 membered heterocyclyl, and a 4-6 membered cycloalkyl; m is 0 or 1; n is 0 or 1; and p is 0 or 1. X is CH or N; R is H, halo, -OH, C 1-4 alkyl, C 1-4 alkoxy, C 1-4 haloalkyl or C 1-4 haloalkoxy; R 1 is H, C 1-4 alkyl, C 1-4 haloalkyl, or 3-6 membered cycloalkyl; R 2 and R 3 are each, independently, H or C 1-4 alkyl; R 4 is halo, -C 6 H 4 -R 6, -(4-10 membered heterocyclyl)-R 6, -(5-10 membered heteroaryl)-R 6, -(4-6 membered carbocyclyl)-R 6, a 6-10 membered fused heterocyclyl-aryl optionally substituted with 1-3 C 1-4 alkyl groups, or a 6-10 membered fused heterocyclyl-heteroaryl optionally substituted with 1-3 C 1-4 alkyl groups; R 5 is H, or R 4 and R 5 together with the carbon atoms to which they are attached form a 5-12 membered carbocyclic, heterocyclic, aryl, heteroaryl, fused heterocyclyl-aryl, fused heterocyclyl-heteroaryl, fused carbocyclyl-aryl, or fused carbocyclyl-heteroaryl ring, wherein said carbocyclic, heterocyclic, aryl, heteroaryl, fused heterocyclyl-aryl, fused heterocyclyl-heteroaryl, fused carbocyclyl-aryl, or fused carbocyclyl-heteroaryl ring is optionally substituted with 1-3 C 1-4 alkyl groups; R 6 is -(CH 2) m - n -(CH 2) p -R 6a, -(CH 2) m - n -(CH 2) p -R 6b, or -(CH 2) m - n -(CH 2) p -R 6c; R 6a is H, -OH, -NR 7 R 8, -CN, oxo, C 1-4 alkyl, C 1-4 haloalkyl, -CH(NR 7 R 8)-C 1-3 haloalkyl, - CH(NR 7 R 8)-C 1-3 alkyl, -SO 2 C 1-4 alkyl, or -SO 2 NR 7 R 8; R 6b is a 4-10 membered heterocyclyl optionally substituted with 1-3 substituents independently selected from a group consisting of -NR 7 R 8, -C 1-4 alkyl, -C 1-4 hydroxyalkyl, and -C 1-4 alkyl-NR 7 R 8; R 6c is a 5-10 membered heteroaryl optionally substituted with 1-3 substituents independently selected from a group consisting of -NR 7 R 8, -C 1-4 alkyl, -C 1-4 hydroxyalkyl, and -C 1-4 alkyl-NR 7 R 8; R 6d is a 4-10 membered carbocyclyl optionally substituted with 1-3 substituents independently selected from a group consisting of halo, -OH, -NR 7 R 8, -C 1-4 alkyl, -C 1-4 haloalkyl, - C 1-4 hydroxyalkyl, -C 1-4 alkyl-NR 7 R 8, -SO 2 C 1-4 alkyl, and 4-6 membered heterocyclyl; R 7 and R 8 are each, independently, selected from a group consisting of H, C 1-4 alkyl, C 1-4 hydroxyalkyl, a 4-6 membered heterocyclyl, and a 4-6 membered cycloalkyl; m is 0 or 1; n is 0 or 1; and p is 0 or 1.