env-bootstrap.sh 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. #!/usr/bin/env sh
  2. # Copyright 2025 Nick Brassel (@tzarc)
  3. # SPDX-License-Identifier: GPL-2.0-or-later
  4. ################################################################################
  5. # This script will install the QMK CLI, toolchains, and flashing utilities.
  6. ################################################################################
  7. # Environment variables:
  8. # CONFIRM: Skip the pre-install delay. (or: --confirm)
  9. # QMK_DISTRIB_DIR: The directory to install the QMK distribution to. (or: --qmk-distrib-dir=...)
  10. # UV_INSTALL_DIR: The directory to install `uv` to. (or: --uv-install-dir=...)
  11. # UV_TOOL_DIR: The directory to install `uv` tools to. (or: --uv-tool-dir=...)
  12. # SKIP_CLEAN: Skip cleaning the distribution directory. (or: --skip-clean)
  13. # SKIP_PACKAGE_MANAGER: Skip installing the necessary packages for the package manager. (or: --skip-package-manager)
  14. # SKIP_UV: Skip installing `uv`. (or: --skip-uv)
  15. # SKIP_QMK_CLI: Skip installing the QMK CLI. (or: --skip-qmk-cli)
  16. # SKIP_QMK_TOOLCHAINS: Skip installing the QMK toolchains. (or: --skip-qmk-toolchains)
  17. # SKIP_QMK_FLASHUTILS: Skip installing the QMK flashing utilities. (or: --skip-qmk-flashutils)
  18. # SKIP_UDEV_RULES: Skip installing the udev rules for Linux. (or: --skip-udev-rules)
  19. # SKIP_WINDOWS_DRIVERS: Skip installing the Windows drivers for the flashing utilities. (or: --skip-windows-drivers)
  20. #
  21. # Arguments above may be negated by prefixing with `--no-` instead (e.g. `--no-skip-clean`).
  22. ################################################################################
  23. # Usage:
  24. # curl -fsSL https://raw.githubusercontent.com/qmk/qmk_firmware/master/util/env-bootstrap.sh | sh
  25. #
  26. # Help:
  27. # curl -fsSL https://raw.githubusercontent.com/qmk/qmk_firmware/master/util/env-bootstrap.sh | sh -s -- --help
  28. #
  29. # An example which skips installing `uv` using environment variables:
  30. # curl -fsSL https://raw.githubusercontent.com/qmk/qmk_firmware/master/util/env-bootstrap.sh | SKIP_UV=1 sh
  31. #
  32. # ...or by using command line arguments:
  33. # curl -fsSL https://raw.githubusercontent.com/qmk/qmk_firmware/master/util/env-bootstrap.sh | sh -s -- --skip-uv
  34. #
  35. # Any other configurable items listed above may be specified in the same way.
  36. ################################################################################
  37. { # this ensures the entire script is downloaded #
  38. set -eu
  39. # Prevent user grep settings from injecting flags (e.g. --color=always) that
  40. # corrupt captured output and break pattern matching throughout this script.
  41. unset GREP_OPTIONS GREP_COLOR GREP_COLORS
  42. BOOTSTRAP_TMPDIR="$(mktemp -d /tmp/qmk-bootstrap-failure.XXXXXX)"
  43. trap 'rm -rf "$BOOTSTRAP_TMPDIR" >/dev/null 2>&1 || true' EXIT
  44. FAILURE_FILE="${BOOTSTRAP_TMPDIR}/fail"
  45. # Work out which `sed` to use
  46. command -v gsed >/dev/null 2>&1 && SED=gsed || SED=sed
  47. script_args() {
  48. cat <<__EOT__
  49. --help -- Shows this help text
  50. --confirm -- Skips the delay before installation
  51. --uv-install-dir={path} -- The directory to install \`uv\` into
  52. --uv-tool-dir={path} -- The directory to install \`uv\` tools into
  53. --qmk-distrib-dir={path} -- The directory to install the QMK distribution into
  54. --skip-clean -- Skip cleaning the QMK distribution directory
  55. --skip-package-manager -- Skip installing the necessary packages for the package manager
  56. --skip-uv -- Skip installing \`uv\`
  57. --skip-qmk-cli -- Skip installing the QMK CLI
  58. --skip-qmk-toolchains -- Skip installing the QMK toolchains
  59. --skip-qmk-flashutils -- Skip installing the QMK flashing utilities
  60. --skip-udev-rules -- Skip installing the udev rules for Linux
  61. --skip-windows-drivers -- Skip installing the Windows drivers for the flashing utilities
  62. __EOT__
  63. # Hidden:
  64. # --wsl-install -- Installs the WSL variant of qmk_flashutils
  65. }
  66. signal_execution_failure() {
  67. touch "$FAILURE_FILE" >/dev/null 2>&1 || true
  68. }
  69. exit_if_execution_failed() {
  70. if [ -e "$FAILURE_FILE" ]; then
  71. exit 1
  72. fi
  73. }
  74. script_help() {
  75. echo "$(basename ${this_script:-qmk-install.sh}) $(script_args | sort | ${SED} -e 's@^\s*@@g' -e 's@\s\+--.*@@g' -e 's@^@[@' -e 's@$@]@' | tr '\n' ' ')"
  76. echo
  77. echo "Arguments:"
  78. script_args
  79. echo
  80. echo "Switch arguments may be negated by prefixing with '--no-' (e.g. '--no-skip-clean')."
  81. }
  82. script_parse_args() {
  83. local N
  84. local V
  85. while [ ! -z "${1:-}" ]; do
  86. case "$1" in
  87. --help)
  88. script_help
  89. exit 0
  90. ;;
  91. --*=*)
  92. N=${1%%=*}
  93. N=${N##--}
  94. N=$(echo $N | tr '-' '_' | tr 'a-z' 'A-Z')
  95. V=${1##*=}
  96. export $N="$V"
  97. ;;
  98. --no-*)
  99. N=${1##--no-}
  100. N=$(echo $N | tr '-' '_' | tr 'a-z' 'A-Z')
  101. unset $N
  102. ;;
  103. --*)
  104. N=${1##--}
  105. N=$(echo $N | tr '-' '_' | tr 'a-z' 'A-Z')
  106. export $N=true
  107. ;;
  108. *)
  109. echo "Unknown argument: '$1'" >&2
  110. echo
  111. script_help >&2
  112. exit 1
  113. ;;
  114. esac
  115. shift
  116. unset N
  117. unset V
  118. done
  119. }
  120. nsudo() {
  121. if [ "$(fn_os)" = "windows" ]; then
  122. # No need for sudo under QMK MSYS
  123. return
  124. elif [ $(id -u) -ne 0 ]; then
  125. if [ -n "$(command -v sudo 2>/dev/null || true)" ]; then
  126. echo "sudo"
  127. elif [ -n "$(command -v doas 2>/dev/null || true)" ]; then
  128. echo "doas"
  129. else
  130. echo "Please install 'sudo' or 'doas' to continue." >&2
  131. exit 1
  132. fi
  133. fi
  134. true
  135. }
  136. download_url() {
  137. local url=$1
  138. local filename=${2:-$(basename "$url")}
  139. local quiet=''
  140. if [ -n "$(command -v curl 2>/dev/null || true)" ]; then
  141. [ "$filename" = "-" ] && quiet='-s' || echo "Downloading '$url' => '$filename'" >&2
  142. curl -LSf $quiet -o "$filename" "$url"
  143. elif [ -n "$(command -v wget 2>/dev/null || true)" ]; then
  144. [ "$filename" = "-" ] && quiet='-q' || echo "Downloading '$url' => '$filename'" >&2
  145. wget $quiet "-O$filename" "$url"
  146. else
  147. echo "Please install 'curl' to continue." >&2
  148. exit 1
  149. fi
  150. }
  151. github_api_call() {
  152. local url="$1"
  153. local token="${GITHUB_TOKEN:-${GH_TOKEN:-}}"
  154. if [ -n "${token:-}" ]; then
  155. if [ -n "$(command -v curl 2>/dev/null || true)" ]; then
  156. curl -fsSL -H "Authorization: token $token" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/$url"
  157. elif [ -n "$(command -v wget 2>/dev/null || true)" ]; then
  158. wget -q --header="Authorization: token $token" --header="Accept: application/vnd.github.v3+json" "https://api.github.com/$url" -O -
  159. fi
  160. else
  161. download_url "https://api.github.com/$url" -
  162. fi
  163. }
  164. fn_os() {
  165. local os_name=$(echo ${1:-} | tr 'A-Z' 'a-z')
  166. if [ -z "$os_name" ]; then
  167. os_name=$(uname -s | tr 'A-Z' 'a-z')
  168. fi
  169. case "$os_name" in
  170. *darwin* | *macos* | *apple*)
  171. echo macos
  172. ;;
  173. *windows* | *mingw* | *msys*)
  174. echo windows
  175. ;;
  176. *linux*)
  177. echo linux
  178. ;;
  179. *)
  180. echo unknown
  181. ;;
  182. esac
  183. }
  184. fn_arch() {
  185. local arch_name=$(echo ${1:-} | tr 'A-Z' 'a-z')
  186. if [ -z "$arch_name" ]; then
  187. arch_name=$(uname -m | tr 'A-Z' 'a-z')
  188. fi
  189. case "$arch_name" in
  190. *arm64* | *aarch64*)
  191. echo ARM64
  192. ;;
  193. *riscv64*)
  194. echo RV64
  195. ;;
  196. *x86_64* | *x64*)
  197. echo X64
  198. ;;
  199. *)
  200. echo unknown
  201. ;;
  202. esac
  203. }
  204. preinstall_delay() {
  205. [ -z "${CONFIRM:-}" ] || return 0
  206. echo >&2
  207. echo "Waiting 10 seconds before proceeding. Press Ctrl+C to cancel installation." >&2
  208. sleep 10
  209. }
  210. get_package_manager_deps() {
  211. case $(fn_os) in
  212. macos) echo "zstd clang-format make hidapi libusb dos2unix git" ;;
  213. windows) echo "base-devel: zstd:p toolchain:p clang:p hidapi:p dos2unix: git: unzip:" ;;
  214. linux)
  215. if ldd --version 2>&1 | grep -qi musl; then
  216. echo >&2
  217. echo "Sorry, QMK's pre-built toolchains are compiled against glibc and will not run on musl-based Linux distributions." >&2
  218. echo >&2
  219. echo "Try using a glibc-based distribution, or use Docker instead:" >&2
  220. echo " - https://docs.qmk.fm/newbs_getting_started#set-up-your-environment" >&2
  221. echo " - https://docs.qmk.fm/#/getting_started_docker" >&2
  222. echo >&2
  223. echo "If you cannot use a compatible distro, you can try installing the \`qmk\` Python package manually using \`pip\`, most likely requiring a virtual environment:" >&2
  224. echo " % python3 -m pip install qmk" >&2
  225. echo >&2
  226. echo "All other dependencies will need to be installed manually, such as make, git, AVR and ARM toolchains, and associated flashing utilities." >&2
  227. echo >&2
  228. echo "**NOTE**: QMK does not provide official support for musl-based environments. Here be dragons, you are on your own." >&2
  229. signal_execution_failure
  230. return
  231. fi
  232. case $(grep ID /etc/os-release) in
  233. *arch* | *manjaro* | *cachyos*) echo "zstd base-devel clang diffutils wget unzip zip hidapi dos2unix git" ;;
  234. *debian* | *ubuntu*) echo "zstd build-essential clang-format diffutils wget unzip zip libhidapi-hidraw0 dos2unix git" ;;
  235. *fedora*) echo "zstd clang diffutils which gcc git wget unzip zip hidapi dos2unix libusb-devel libusb1-devel libusb-compat-0.1-devel libusb0-devel git epel-release" ;;
  236. *suse*) echo "zstd make gcc binutils clang diffutils wget unzip zip libhidapi-hidraw0 dos2unix git libusb-1_0-devel gzip which" ;;
  237. *gentoo*) echo "zstd sys-apps/diffutils wget unzip zip dev-libs/hidapi dos2unix dev-vcs/git dev-libs/libusb app-arch/gzip which" ;;
  238. *)
  239. echo >&2
  240. echo "Sorry, we don't recognize your distribution." >&2
  241. echo >&2
  242. echo "Proceeding with the installation, however you will need to install at least the following tools manually:" >&2
  243. echo " - make, git, curl, zstd, unzip, [lib]hidapi" >&2
  244. echo "Other tools may be required depending on your distribution." >&2
  245. echo >&2
  246. echo "Alternatively, if you prefer Docker, try using the docker image instead:" >&2
  247. echo " - https://docs.qmk.fm/#/getting_started_docker" >&2
  248. ;;
  249. esac
  250. ;;
  251. *)
  252. # We can only really support macOS, Windows, and Linux at this time due to `uv` requirements.
  253. echo >&2
  254. echo "Sorry, we don't recognize your OS. Try using a compatible OS instead:" >&2
  255. echo " - https://docs.qmk.fm/newbs_getting_started#set-up-your-environment" >&2
  256. echo >&2
  257. echo "If you cannot use a compatible OS, you can try installing the \`qmk\` Python package manually using \`pip\`, most likely requiring a virtual environment:" >&2
  258. echo " % python3 -m pip install qmk" >&2
  259. echo >&2
  260. echo "All other dependencies will need to be installed manually, such as make, git, AVR and ARM toolchains, and associated flashing utilities." >&2
  261. echo >&2
  262. echo "**NOTE**: QMK does not provide official support for your environment. Here be dragons, you are on your own." >&2
  263. signal_execution_failure
  264. ;;
  265. esac
  266. }
  267. print_package_manager_deps_and_delay() {
  268. get_package_manager_deps | tr ' ' '\n' | sort | xargs -I'{}' echo " - {}" >&2
  269. exit_if_execution_failed
  270. preinstall_delay || exit 1
  271. }
  272. install_package_manager_deps() {
  273. # Install the necessary packages for the package manager
  274. case $(fn_os) in
  275. macos)
  276. if [ -n "$(command -v brew 2>/dev/null || true)" ]; then
  277. echo "It will also install the following system packages using 'brew':" >&2
  278. print_package_manager_deps_and_delay
  279. brew update
  280. local existing=""
  281. local new=""
  282. for dep in $(get_package_manager_deps); do
  283. if brew list --formula | grep -q "^${dep}\$"; then
  284. existing="${existing:-} $dep"
  285. else
  286. new="${new:-} $dep"
  287. fi
  288. done
  289. if [ -n "${existing:-}" ]; then
  290. brew upgrade $existing
  291. fi
  292. if [ -n "${new:-}" ]; then
  293. brew install $new
  294. fi
  295. else
  296. echo "Please install 'brew' to continue. See https://brew.sh/ for more information." >&2
  297. exit 1
  298. fi
  299. ;;
  300. windows)
  301. echo "It will also install the following packages using 'pacman'/'pacboy':" >&2
  302. print_package_manager_deps_and_delay
  303. $(nsudo) pacman --needed --noconfirm --disable-download-timeout -S pactoys
  304. $(nsudo) pacboy sync --needed --noconfirm --disable-download-timeout $(get_package_manager_deps)
  305. ;;
  306. linux)
  307. case $(grep ID /etc/os-release) in
  308. *arch* | *manjaro* | *cachyos*)
  309. echo "It will also install the following system packages using 'pacman':" >&2
  310. print_package_manager_deps_and_delay
  311. $(nsudo) pacman --needed --noconfirm -S $(get_package_manager_deps)
  312. ;;
  313. *debian* | *ubuntu*)
  314. echo "It will also install the following system packages using 'apt':" >&2
  315. print_package_manager_deps_and_delay
  316. $(nsudo) apt-get update
  317. DEBIAN_FRONTEND=noninteractive \
  318. $(nsudo) apt-get --quiet --yes install $(get_package_manager_deps)
  319. ;;
  320. *fedora*)
  321. echo "It will also install the following system packages using 'dnf':" >&2
  322. print_package_manager_deps_and_delay
  323. # Some RHEL-likes need EPEL for hidapi and libusb packages
  324. $(nsudo) dnf -y install epel-release 2>/dev/null || true
  325. # RHEL-likes have naming differences in libusb/hidapi packages; try each individually
  326. $(nsudo) dnf -y install $(get_package_manager_deps | tr ' ' '\n' | grep -v 'epel-release' | grep -v libusb | grep -v hidapi | tr '\n' ' ')
  327. for pkg in $(get_package_manager_deps | tr ' ' '\n' | grep -E 'libusb|hidapi'); do
  328. $(nsudo) dnf -y install "$pkg" 2>/dev/null || true
  329. done
  330. ;;
  331. *opensuse* | *suse*)
  332. echo "It will also install the following system packages using 'zypper':" >&2
  333. print_package_manager_deps_and_delay
  334. $(nsudo) zypper --non-interactive refresh
  335. $(nsudo) zypper --non-interactive install $(get_package_manager_deps)
  336. ;;
  337. *gentoo*)
  338. echo "It will also install the following system packages using 'emerge':" >&2
  339. print_package_manager_deps_and_delay
  340. $(nsudo) emaint sync
  341. $(nsudo) emerge --noreplace --ask=n $(get_package_manager_deps | tr ' ' '\n') || signal_execution_failure
  342. exit_if_execution_failed
  343. ;;
  344. *)
  345. print_package_manager_deps_and_delay
  346. echo "Proceeding with the installation, you will need to ensure prerequisites are installed." >&2
  347. ;;
  348. esac
  349. ;;
  350. *)
  351. print_package_manager_deps_and_delay
  352. ;;
  353. esac
  354. }
  355. install_uv() {
  356. # Install `uv` (or update as necessary)
  357. download_url https://astral.sh/uv/install.sh - | TMPDIR="$(posix_ish_path "${TMPDIR:-}")" UV_INSTALL_DIR="$(windows_ish_path "${UV_INSTALL_DIR:-}")" sh
  358. }
  359. setup_paths() {
  360. # Set up the paths for any of the locations `uv` expects
  361. if [ -n "${XDG_BIN_HOME:-}" ]; then
  362. export PATH="$XDG_BIN_HOME:$PATH"
  363. fi
  364. if [ -n "${XDG_DATA_HOME:-}" ]; then
  365. export PATH="$XDG_DATA_HOME/../bin:$PATH"
  366. fi
  367. [ ! -d "$HOME/.local/bin" ] || export PATH="$HOME/.local/bin:$PATH"
  368. if [ -n "${UV_INSTALL_DIR:-}" ]; then
  369. export PATH="$UV_INSTALL_DIR/bin:$UV_INSTALL_DIR:$PATH" # cater for both "flat" and "hierarchical" installs of `uv`
  370. fi
  371. if [ -n "${UV_TOOL_BIN_DIR:-}" ]; then
  372. export PATH="$UV_TOOL_BIN_DIR:$PATH"
  373. fi
  374. }
  375. uv_command() {
  376. if [ "$(fn_os)" = "windows" ]; then
  377. UV_TOOL_DIR="$(windows_ish_path "${UV_TOOL_DIR:-}")" \
  378. UV_TOOL_BIN_DIR="$(windows_ish_path "${UV_TOOL_BIN_DIR:-}")" \
  379. uv "$@"
  380. else
  381. uv "$@"
  382. fi
  383. }
  384. install_qmk_cli() {
  385. # Install the QMK CLI
  386. uv_command tool install --force --with pip --upgrade --python $PYTHON_TARGET_VERSION qmk
  387. # QMK is installed to...
  388. local qmk_tooldir="$(posix_ish_path "$(uv_command tool dir)/qmk")"
  389. # Activate the environment
  390. if [ -e "$qmk_tooldir/bin" ]; then
  391. . "$qmk_tooldir/bin/activate"
  392. elif [ -e "$qmk_tooldir/Scripts" ]; then
  393. . "$qmk_tooldir/Scripts/activate"
  394. else
  395. echo "Could not find the QMK environment to activate." >&2
  396. exit 1
  397. fi
  398. # Install the QMK dependencies
  399. uv_command pip install --upgrade -r https://raw.githubusercontent.com/qmk/qmk_firmware/refs/heads/master/requirements.txt
  400. uv_command pip install --upgrade -r https://raw.githubusercontent.com/qmk/qmk_firmware/refs/heads/master/requirements-dev.txt
  401. # Deactivate the environment
  402. deactivate
  403. }
  404. install_toolchains() {
  405. # Get the latest toolchain release from https://github.com/qmk/qmk_toolchains
  406. local latest_toolchains_release=$(github_api_call repos/qmk/qmk_toolchains/releases/latest - | grep -oE '"tag_name": "[^"]+' | grep -oE '[^"]+$')
  407. # Download the specific release asset with a matching keyword
  408. local toolchain_url=$(github_api_call repos/qmk/qmk_toolchains/releases/tags/$latest_toolchains_release - | grep -oE '"browser_download_url": "[^"]+"' | grep -oE 'https://[^"]+' | grep -E "qmk_toolchains-.*$(fn_os)$(fn_arch)")
  409. if [ -z "$toolchain_url" ]; then
  410. echo "No toolchain found for this OS/Arch combination." >&2
  411. exit 1
  412. fi
  413. # Download the toolchain release to the toolchains location
  414. echo "Downloading compiler toolchains..." >&2
  415. local target_file="$QMK_DISTRIB_DIR/$(basename "$toolchain_url")"
  416. download_url "$toolchain_url" "$target_file"
  417. # Extract the toolchain
  418. echo "Extracting compiler toolchains to '$QMK_DISTRIB_DIR'..." >&2
  419. zstdcat "$target_file" | tar xf - -C "$QMK_DISTRIB_DIR" --strip-components=1
  420. }
  421. install_flashing_tools() {
  422. local osarchvariant="$(fn_os)$(fn_arch)"
  423. # Special case for WSL
  424. if [ -n "${WSL_INSTALL:-}" ] || [ -n "${WSL_DISTRO_NAME:-}" ] || [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]; then
  425. osarchvariant="windowsWSL"
  426. fi
  427. # Get the latest flashing tools release from https://github.com/qmk/qmk_flashutils
  428. local latest_flashutils_release=$(github_api_call repos/qmk/qmk_flashutils/releases/latest - | grep -oE '"tag_name": "[^"]+' | grep -oE '[^"]+$')
  429. # Download the specific release asset with a matching keyword
  430. local flashutils_url=$(github_api_call repos/qmk/qmk_flashutils/releases/tags/$latest_flashutils_release - | grep -oE '"browser_download_url": "[^"]+"' | grep -oE 'https://[^"]+' | grep -E "qmk_flashutils-.*$osarchvariant")
  431. if [ -z "$flashutils_url" ]; then
  432. echo "No flashing tools found for this OS/Arch combination." >&2
  433. exit 1
  434. fi
  435. # Download the flashing tools release to the toolchains location
  436. echo "Downloading flashing tools..." >&2
  437. local target_file="$QMK_DISTRIB_DIR/$(basename "$flashutils_url")"
  438. download_url "$flashutils_url" "$target_file"
  439. # Extract the flashing tools
  440. echo "Extracting flashing tools to '$QMK_DISTRIB_DIR'..." >&2
  441. zstdcat "$target_file" | tar xf - -C "$QMK_DISTRIB_DIR/bin"
  442. # Move the release file to etc
  443. mv "$QMK_DISTRIB_DIR/bin/flashutils_release"* "$QMK_DISTRIB_DIR/etc"
  444. }
  445. install_linux_udev_rules() {
  446. # Get the latest qmk_udev release
  447. local latest_udev_release=$(github_api_call repos/qmk/qmk_udev/releases/latest - | grep -oE '"tag_name": "[^"]+' | grep -oE '[^"]+$')
  448. if [ -z "$latest_udev_release" ]; then
  449. echo "Could not determine latest qmk_udev release." >&2
  450. exit 1
  451. fi
  452. echo "Using qmk_udev release: $latest_udev_release" >&2
  453. # Download the udev rules file
  454. local qmk_rules_file="$QMK_DISTRIB_DIR/50-qmk.rules"
  455. local release_base="https://github.com/qmk/qmk_udev/releases/download/$latest_udev_release"
  456. download_url "$release_base/50-qmk.rules" "$qmk_rules_file"
  457. # Download the architecture-appropriate qmk_id binary
  458. local arch="$(fn_arch)"
  459. local qmk_id_file="$QMK_DISTRIB_DIR/qmk_id"
  460. download_url "$release_base/qmk_id-linux${arch}" "$qmk_id_file"
  461. # Remove existing QMK udev rules and qmk_id helpers from all standard locations
  462. echo "Removing existing QMK udev rules and helpers..." >&2
  463. for dir in /etc/udev/rules.d /run/udev/rules.d /usr/lib/udev/rules.d /usr/local/lib/udev/rules.d /lib/udev/rules.d; do
  464. if [ -d "$dir" ]; then
  465. for f in "$dir"/*-qmk.rules; do
  466. [ -e "$f" ] && echo "Removing $f" >&2 && $(nsudo) rm -f "$f"
  467. done
  468. fi
  469. done
  470. for dir in /usr/lib/udev /usr/local/lib/udev /lib/udev; do
  471. [ -e "$dir/qmk_id" ] && echo "Removing $dir/qmk_id" >&2 && $(nsudo) rm -f "$dir/qmk_id"
  472. done
  473. # Install qmk_id binary
  474. echo "Installing /usr/lib/udev/qmk_id ..." >&2
  475. $(nsudo) install -d -m 0755 /usr/lib/udev
  476. $(nsudo) install -m 0755 "$qmk_id_file" /usr/lib/udev/qmk_id
  477. # Install udev rules
  478. echo "Installing /etc/udev/rules.d/50-qmk.rules ..." >&2
  479. $(nsudo) install -d -m 0755 /etc/udev/rules.d
  480. $(nsudo) install -m 0644 "$qmk_rules_file" /etc/udev/rules.d/50-qmk.rules
  481. # Clean up downloaded files
  482. rm -f "$qmk_rules_file" "$qmk_id_file" || true
  483. # Reload udev rules
  484. if command -v udevadm >/dev/null 2>&1; then
  485. echo "Reloading udev rules..." >&2
  486. $(nsudo) udevadm control --reload-rules || true
  487. $(nsudo) udevadm trigger || true
  488. else
  489. echo "udevadm not found, skipping udev rules reload." >&2
  490. fi
  491. }
  492. install_windows_drivers() {
  493. # Get the latest driver installer release from https://github.com/qmk/qmk_driver_installer
  494. local latest_driver_installer_release=$(github_api_call repos/qmk/qmk_driver_installer/releases/latest - | grep -oE '"tag_name": "[^"]+' | grep -oE '[^"]+$')
  495. # Download the specific release asset
  496. local driver_installer_url=$(github_api_call repos/qmk/qmk_driver_installer/releases/tags/$latest_driver_installer_release - | grep -oE '"browser_download_url": "[^"]+"' | grep -oE 'https://[^"]+' | grep '\.exe')
  497. if [ -z "$driver_installer_url" ]; then
  498. echo "No driver installer found." >&2
  499. exit 1
  500. fi
  501. # Download the driver installer release to the toolchains location
  502. echo "Downloading driver installer..." >&2
  503. local target_file="$QMK_DISTRIB_DIR/$(basename "$driver_installer_url")"
  504. download_url "$driver_installer_url" "$target_file"
  505. # Download the drivers list
  506. download_url "https://raw.githubusercontent.com/qmk/qmk_firmware/refs/heads/master/util/drivers.txt" "$QMK_DISTRIB_DIR/drivers.txt"
  507. # Execute the driver installer
  508. cd "$QMK_DISTRIB_DIR"
  509. cmd.exe //c "qmk_driver_installer.exe --all --force drivers.txt"
  510. cd -
  511. # Remove the temporary files
  512. rm -f "$QMK_DISTRIB_DIR/qmk_driver_installer.exe" "$QMK_DISTRIB_DIR/drivers.txt" || true
  513. }
  514. clean_tarballs() {
  515. # Clean up the tarballs
  516. rm -f "$QMK_DISTRIB_DIR"/*.tar.zst || true
  517. }
  518. windows_ish_path() {
  519. [ -n "$1" ] || return 0
  520. [ "$(uname -o 2>/dev/null || true)" = "Msys" ] && cygpath -w "$1" || echo "$1"
  521. }
  522. posix_ish_path() {
  523. [ -n "$1" ] || return 0
  524. [ "$(uname -o 2>/dev/null || true)" = "Msys" ] && cygpath -u "$1" || echo "$1"
  525. }
  526. # Set the Python version we want to use with the QMK CLI
  527. export PYTHON_TARGET_VERSION=${PYTHON_TARGET_VERSION:-3.14}
  528. # Windows/MSYS doesn't like `/tmp` so we need to set a different temporary directory.
  529. # Also set the default `UV_INSTALL_DIR` and `QMK_DISTRIB_DIR` to locations which don't pollute the user's home directory, keeping the installation internal to MSYS.
  530. if [ "$(uname -o 2>/dev/null || true)" = "Msys" ]; then
  531. export TMPDIR="$(posix_ish_path "$TMP")"
  532. export UV_INSTALL_DIR="$(posix_ish_path "${UV_INSTALL_DIR:-/opt/uv}")"
  533. export QMK_DISTRIB_DIR="$(posix_ish_path "${QMK_DISTRIB_DIR:-/opt/qmk}")"
  534. export UV_TOOL_DIR="$(posix_ish_path "${UV_TOOL_DIR:-"$UV_INSTALL_DIR/tools"}")"
  535. export UV_TOOL_BIN_DIR="$(posix_ish_path "$UV_TOOL_DIR/bin")"
  536. fi
  537. script_parse_args "$@"
  538. echo "This QMK CLI installation script will install \`uv\`, the QMK CLI, as well as QMK-supplied toolchains and flashing utilities." >&2
  539. [ -z "${SKIP_PACKAGE_MANAGER:-}" ] || { preinstall_delay || exit 1; }
  540. [ -n "${SKIP_PACKAGE_MANAGER:-}" ] || install_package_manager_deps
  541. [ -n "${SKIP_UV:-}" ] || install_uv
  542. # Make sure the usual `uv` and other associated directories are on the $PATH
  543. setup_paths
  544. # Work out where we want to install the distribution and tools now that `uv` is installed
  545. export QMK_DISTRIB_DIR="$(posix_ish_path "${QMK_DISTRIB_DIR:-$(printf 'import platformdirs\nprint(platformdirs.user_data_dir("qmk"))' | uv_command run --quiet --python $PYTHON_TARGET_VERSION --with platformdirs -)}")"
  546. # Clear out the distrib directory if necessary
  547. if [ -z "${SKIP_CLEAN:-}" ] || [ -z "${SKIP_QMK_TOOLCHAINS:-}" -a -z "${SKIP_QMK_FLASHUTILS:-}" ]; then
  548. if [ -d "$QMK_DISTRIB_DIR" ]; then
  549. echo "Removing old QMK distribution..." >&2
  550. rm -rf "$QMK_DISTRIB_DIR"
  551. fi
  552. fi
  553. mkdir -p "$QMK_DISTRIB_DIR"
  554. [ -n "${SKIP_QMK_CLI:-}" ] || install_qmk_cli
  555. [ -n "${SKIP_QMK_TOOLCHAINS:-}" ] || install_toolchains
  556. [ -n "${SKIP_QMK_FLASHUTILS:-}" ] || install_flashing_tools
  557. if [ "$(uname -s 2>/dev/null || true)" = "Linux" ]; then
  558. [ -n "${SKIP_UDEV_RULES:-}" ] || install_linux_udev_rules
  559. fi
  560. if [ "$(uname -o 2>/dev/null || true)" = "Msys" ]; then
  561. [ -n "${SKIP_WINDOWS_DRIVERS:-}" ] || install_windows_drivers
  562. fi
  563. clean_tarballs
  564. # Notify the user that they may need to restart their shell to get the `qmk` command
  565. echo >&2
  566. echo "QMK CLI installation complete." >&2
  567. echo "The QMK CLI has been installed to '$(posix_ish_path "$(dirname "$(command -v qmk)")")'." >&2
  568. echo "The QMK CLI venv has been created at '$(posix_ish_path "$(uv_command tool dir)/qmk")'." >&2
  569. echo "Toolchains and flashing utilities have been installed to '$QMK_DISTRIB_DIR'." >&2
  570. echo >&2
  571. echo "You may need to restart your shell to gain access to the 'qmk' command." >&2
  572. echo "Alternatively, add "$(posix_ish_path "$(dirname "$(command -v qmk)")")" to your \$PATH:" >&2
  573. echo " export PATH=\"$(posix_ish_path "$(dirname "$(command -v qmk)")"):\$PATH\"" >&2
  574. } # this ensures the entire script is downloaded #