How to get Operating System name

Hello,

I would like to get the underlying operating system in my program.

I found numerous solutions to this, but they all spawn an external process, e.g.

I tried to solve this using dune:

(executable
 (name main))

(rule
 (target config.ml)
 (action
  (with-stdout-to
   %{target}
   (run sh -c "printf 'let os_type = \"%s\"' \"$(uname)\""))))

let () =
print_endline Config.os_type

Code: <div style="background-color:black;color:white;text-align:center.

This is annoying tho, one solution also could be to call c code, is there something that solved this or plans there to introduce it in ocaml?

1 Like

%{os_type} should give you this (see Variables - Dune documentation) - note that all the values given by ocamlc -config are available as variables in Dune files

1 Like

If you’re really after the output of uname, the opam library in recent versions binds the syscall - see OpamStd.Sys.os

3 Likes

Thanks this is what I was searching for. Probably going to call C code for now, because I do not want opam-core as dependency only for this variable.

you can also use ExtUnix.All.uname, which has less dependencies and is meant for this use-case

2 Likes

In addition to ExtUnix there is dkml-c-probe which does not raise exceptions on Windows.

2 Likes

How accurate do you need the OS name to be? If you just need a rough idea then this might work: OCaml library : Sys