local({
  .pick_cran <- function() {
    # Return a CRAN repo URL, preferring RSPM binaries if available for this OS
    rspm_template <- "https://packagemanager.rstudio.com/cran/__linux__/%s/latest"
    # See https://github.com/rstudio/r-docker#releases-and-tags,
    # but note that RSPM still uses "centos8"
    supported_os <- c("bionic", "focal", "jammy", "centos7", "centos8", "opensuse153")

    if (nzchar(Sys.which("lsb_release"))) {
      os <- tolower(system("lsb_release -cs", intern = TRUE))
      if (os %in% supported_os) {
        return(sprintf(rspm_template, os))
      }
    }
    if (file.exists("/etc/os-release")) {
      os_release <- readLines("/etc/os-release")
      vals <- sub("^.*=(.*)$", "\\1", os_release)
      os <- intersect(vals, supported_os)
      if (length(os)) {
        # e.g. "bionic"
        return(sprintf(rspm_template, os))
      } else {
        names(vals) <- sub("^(.*)=.*$", "\\1", os_release)
        if (grepl("opensuse", vals["ID"])) {
          version <- sub('^"?([0-9]+)\\.?([0-9]+).*"?.*$', "\\1\\2", vals["VERSION_ID"])
          os <- paste0("opensuse", version)
          if (os %in% supported_os) {
            return(sprintf(rspm_template, os))
          }
        }
      }
    }
    if (file.exists("/etc/system-release")) {
      # Something like "CentOS Linux release 7.7.1908 (Core)"
      system_release <- tolower(utils::head(readLines("/etc/system-release"), 1))
      # Extract from that the distro and the major version number
      os <- sub("^([a-z]+) .* ([0-9]+).*$", "\\1\\2", system_release)
      if (os %in% supported_os) {
        return(sprintf(rspm_template, os))
      }
    }

    return(NULL)
  }

  options(
    Ncpus = parallel::detectCores(),
    repos = c(tryCatch(.pick_cran(), error = function(e) NULL), "https://cloud.r-project.org"),
    HTTPUserAgent = sprintf(
      "R/%s R (%s)",
      getRversion(),
      paste(getRversion(), R.version$platform, R.version$arch, R.version$os)
    )
  )

  # there's a bug in 3.5 that will warn/error on these, so only set it around that
  if (getRversion() >= "3.6.0" || getRversion() < "3.5.0") {
    options(
      warnPartialMatchAttr = TRUE,
      warnPartialMatchDollar = TRUE,
      warnPartialMatchArgs = TRUE
    )
  }
})
