2022-07-30 21:49:00 +02:00
|
|
|
;;; lisp/lib/system.el -*- lexical-binding: t; -*-
|
2021-03-01 11:02:07 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-system-distro ()
|
|
|
|
"Return a symbol representing the installed distro."
|
2022-08-12 20:24:17 +02:00
|
|
|
;; REVIEW Use `with-memoization' when 27.x support is dropped
|
|
|
|
(or (get 'doom-system-distro 'cached-value)
|
|
|
|
(put 'doom-system-distro 'cached-value
|
|
|
|
(cond ((featurep :os 'windows) 'windows)
|
|
|
|
((featurep :os 'macos) 'macos)
|
|
|
|
((and (file-exists-p "/etc/os-release")
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents "/etc/os-release")
|
|
|
|
(when (re-search-forward "^ID=\"?\\([^\"\n]+\\)\"?" nil t)
|
|
|
|
(intern (downcase (match-string 1)))))))
|
|
|
|
;; A few redundancies in case os-release fails us
|
|
|
|
((file-exists-p "/etc/debian_version")
|
|
|
|
'debian)
|
|
|
|
((executable-find "nixos-version")
|
|
|
|
'nixos)
|
|
|
|
((and (or (file-exists-p "/etc/config.scm")
|
|
|
|
(file-directory-p "/run/current-system"))
|
|
|
|
(executable-find "guix"))
|
|
|
|
'guix)
|
|
|
|
('linux)))))
|
2021-03-01 11:02:07 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-system-distro-version ()
|
|
|
|
"Return a distro name and version string."
|
|
|
|
(letf! (defun sh (&rest args) (cdr (apply #'doom-call-process args)))
|
2021-03-04 16:10:33 -05:00
|
|
|
(let ((distro (doom-system-distro)))
|
2021-03-01 11:02:07 -05:00
|
|
|
(cond
|
|
|
|
((eq distro 'windows)
|
|
|
|
(format "Windows %s" "Unknown")) ; TODO
|
|
|
|
((eq distro 'macos)
|
|
|
|
(format "MacOS %s" (sh "sw_vers" "-productVersion")))
|
|
|
|
((executable-find "lsb_release")
|
|
|
|
(sh "lsb_release" "-s" "-d"))
|
|
|
|
((executable-find "nixos-version")
|
|
|
|
(format "NixOS %s" (sh "nixos-version")))
|
2021-03-02 11:59:09 -05:00
|
|
|
((and (file-exists-p "/etc/os-release")
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents "/etc/os-release")
|
|
|
|
(when (re-search-forward "^PRETTY_NAME=\"?\\([^\"\n]+\\)\"?" nil t)
|
|
|
|
(match-string 1)))))
|
2021-03-01 11:02:07 -05:00
|
|
|
((when-let (files (doom-glob "/etc/*-release"))
|
|
|
|
(truncate-string-to-width
|
|
|
|
(replace-regexp-in-string "\n" " " (cat (car files) 73) nil t)
|
|
|
|
64 nil nil "...")))
|
|
|
|
((concat "Unknown " (sh "uname" "-v")))))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-system-distro-icon ()
|
|
|
|
"Display icon for the installed distro."
|
2022-08-12 20:24:17 +02:00
|
|
|
;; REVIEW Use `with-memoization' when 27.x support is dropped
|
|
|
|
(or (get 'doom-system-distro-icon 'cached-value)
|
|
|
|
(put 'doom-system-distro-icon 'cached-value
|
|
|
|
(propertize
|
|
|
|
(pcase (doom-system-distro)
|
|
|
|
(`windows (all-the-icons-faicon "windows"))
|
|
|
|
(`macos (all-the-icons-faicon "apple"))
|
|
|
|
(`arch "\uF303")
|
|
|
|
(`debian "\uF306")
|
|
|
|
(`raspbian "\uF315")
|
|
|
|
(`ubuntu "\uF31b")
|
|
|
|
(`elementary "\uF309")
|
|
|
|
(`fedora "\uF30a")
|
|
|
|
(`coreos "\uF305")
|
|
|
|
(`gentoo "\uF30d")
|
|
|
|
(`mageia "\uF310")
|
|
|
|
(`centos "\uF304")
|
|
|
|
((or `opensuse `tumbleweed) "\uF314")
|
|
|
|
(`sabayon "\uF317")
|
|
|
|
(`slackware "\uF319")
|
|
|
|
(`linuxmint "\uF30e")
|
|
|
|
(`alpine "\uF300")
|
|
|
|
(`aosc "\uF301")
|
|
|
|
(`nixos "\uF313")
|
|
|
|
(`devuan "\uF307")
|
|
|
|
(`manjaro "\uF312")
|
|
|
|
((or `void `artix) "\uF17c")
|
|
|
|
(_ (all-the-icons-faicon "linux")))
|
|
|
|
'face '(:height 1)
|
|
|
|
'display '(raise 0)))))
|
2021-03-01 11:02:07 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-system-cpus ()
|
|
|
|
"Return the max number of processing units on this system.
|
|
|
|
Tries to be portable. Returns 1 if cannot be determined."
|
2021-05-12 14:50:59 -04:00
|
|
|
(or (get 'doom-system-cpus 'cached-value)
|
|
|
|
(put 'doom-system-cpus 'cached-value
|
|
|
|
(let ((cpus
|
2021-08-06 03:06:14 -04:00
|
|
|
(cond ((fboundp 'w32-get-nproc)
|
2021-05-12 14:50:59 -04:00
|
|
|
(w32-get-nproc))
|
|
|
|
((getenv "NUMBER_OF_PROCESSORS"))
|
|
|
|
((executable-find "nproc")
|
|
|
|
(doom-call-process "nproc"))
|
|
|
|
((executable-find "sysctl")
|
|
|
|
(doom-call-process "sysctl" "-n" "hw.ncpu")))))
|
|
|
|
(max
|
|
|
|
1 (or (cl-typecase cpus
|
2021-08-06 03:41:34 -04:00
|
|
|
(integer cpus)
|
2021-05-12 14:50:59 -04:00
|
|
|
(string
|
|
|
|
(condition-case _
|
|
|
|
(string-to-number cpus)
|
|
|
|
(wrong-type-argument
|
|
|
|
(user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S"
|
|
|
|
cpus))))
|
2022-06-08 14:25:58 +02:00
|
|
|
(cons
|
2021-05-12 14:50:59 -04:00
|
|
|
(if (zerop (car cpus))
|
|
|
|
(string-to-number (cdr cpus))
|
|
|
|
(user-error "Failed to look up number of processors, because:\n\n%s"
|
|
|
|
(cdr cpus)))))
|
|
|
|
1))))))
|