control/controlclient, version/distro: detect NixOS explicitly

The fallthrough happened to work in controlclient already due to the
/etc/os-release PRETTY_NAME default, but make it explicit so it
doesn't look like an accident.

Also add it to version/distro, even though nothing needs it yet.
pull/1045/head
Brad Fitzpatrick 4 years ago
parent 2b2a16d9a2
commit ef15096a7d

@ -73,7 +73,7 @@ func osVersionLinux() string {
return fmt.Sprintf("%s%s", bytes.TrimSpace(cr), attr) return fmt.Sprintf("%s%s", bytes.TrimSpace(cr), attr)
} }
fallthrough fallthrough
case "fedora", "rhel", "alpine": case "fedora", "rhel", "alpine", "nixos":
// Their PRETTY_NAME is fine as-is for all versions I tested. // Their PRETTY_NAME is fine as-is for all versions I tested.
fallthrough fallthrough
default: default:

@ -6,6 +6,7 @@ package controlclient
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"testing" "testing"
"github.com/tailscale/wireguard-go/wgcfg" "github.com/tailscale/wireguard-go/wgcfg"
@ -282,3 +283,15 @@ func TestConciseDiffFrom(t *testing.T) {
}) })
} }
} }
func TestNewHostinfo(t *testing.T) {
hi := NewHostinfo()
if hi == nil {
t.Fatal("no Hostinfo")
}
j, err := json.MarshalIndent(hi, " ", "")
if err != nil {
t.Fatal(err)
}
t.Logf("Got: %s", j)
}

@ -17,6 +17,7 @@ const (
Arch = Distro("arch") Arch = Distro("arch")
Synology = Distro("synology") Synology = Distro("synology")
OpenWrt = Distro("openwrt") OpenWrt = Distro("openwrt")
NixOS = Distro("nixos")
) )
// Get returns the current distro, or the empty string if unknown. // Get returns the current distro, or the empty string if unknown.
@ -27,18 +28,28 @@ func Get() Distro {
return "" return ""
} }
func have(file string) bool {
_, err := os.Stat(file)
return err == nil
}
func haveDir(file string) bool {
fi, err := os.Stat(file)
return err == nil && fi.IsDir()
}
func linuxDistro() Distro { func linuxDistro() Distro {
if fi, err := os.Stat("/usr/syno"); err == nil && fi.IsDir() { switch {
case haveDir("usr/syno"):
return Synology return Synology
} case have("/etc/debian_version"):
if _, err := os.Stat("/etc/debian_version"); err == nil {
return Debian return Debian
} case have("/etc/arch-release"):
if _, err := os.Stat("/etc/arch-release"); err == nil {
return Arch return Arch
} case have("/etc/openwrt_version"):
if _, err := os.Stat("/etc/openwrt_version"); err == nil {
return OpenWrt return OpenWrt
case have("/run/current-system/sw/bin/nixos-version"):
return NixOS
} }
return "" return ""
} }

Loading…
Cancel
Save