3

What is the most Emacs-y way to detect the operating system Emacs is running in?

My intention is to conditionally set up package repositories depending on whether they are needed. (If the operating system is one that I know has Emacs packages available, I want to preferentially use those instead.)

I can use hacks like "detect whether /etc/debian_version exists", or even parsing its content. But if Emacs already knows how to tell me what operating system is running, I should use that. Does this exist?

Drew
  • 75,699
  • 9
  • 109
  • 225
bignose
  • 627
  • 3
  • 15

1 Answers1

4

EDIT

I just now found the variable operating-system-release, which (i.c.w. system-type), I guess, comes closest to what you are looking for. However, it might be handy for you to know its value on different operating systems. Maybe readers can help you out in the comments. It seems to be equal to the output of the uname -r command, so you could also search the web for its output on different distro's.

I am using Fedora 36 where its value is:

"6.0.7-200.fc36.x86_64"

END EDIT

As you are referring to `/etc/debian_version, I guess you mean that, in addition to knowing which OS is running, you'd also like to know which (GNU/)linux distribution is being used. The available 'system' variables and functions are described in this section of the elisp manual.

The system-type and system-configuration variables, will tell you which type of OS is being used. There is no 'system' variable that provides info about the used distribution.

However, on (GNU/)linux, you could probably use the following to obtain a useful alist from /etc/os-release (or you could process the file in some other way also of course):

(pp
 (with-temp-buffer
   (insert-file-contents-literally "/etc/os-release")
   (replace-string "\"" "")
   (let ((lines (split-string (buffer-string) "\n" t)))
     (mapcar (lambda (l) (split-string l "=")) lines)))
 (pop-to-buffer "*os-release*"))

where you can remove the pp, which only serves to present a nice view of the result.

dalanicolai
  • 6,108
  • 7
  • 23
  • 3
    My reading was that `system-type` was the primary need; but between the two of them that should do the trick. `C-h i g (elisp)System Environment` to read this inside Emacs. – phils Nov 10 '22 at 09:11
  • `system-name` is the machine name. I'm guessing you meant `system-type`, like @phils mentioned. – cyberbisson Nov 11 '22 at 22:04
  • Ah, I see. I only read the values of those variables from [Ivy](https://github.com/abo-abo/swiper). Then I did not realize that 'system-name' (which here is 'Fedora') actually showed my hostname (I did not read the variable its doc(string)). So then I guess the second part of the answer is the most relevant/useful part... – dalanicolai Nov 11 '22 at 22:49
  • 1
    I have updated the answer – dalanicolai Nov 11 '22 at 23:03