--[[
  Example configuration file for macOS.

  This is designed to work out of the box (you will need to supply a working
  bootstrap kit), but you will almost certainly want to customise it for
  optimum performance.

  Full reference for every section, action, and variable:
  https://docs.rs/bob/latest/bob/config/
]]

-- General configuration variables.
options = {
    build_threads = 4,
    scan_threads = 4,
    -- Log level: error, warn, info, debug, trace.  Override with RUST_LOG env.
    log_level = "info",
}

--[[
  Dynamic resource allocation settings.  Uses statistics from the history db,
  knowledge of upcoming builds, and package weight to make informed choices for
  what MAKE_JOBS and WRKOBJDIR should be set to for each package build.

  If you set MAKE_JOBS or WRKOBJDIR in mk.conf then you must use ?= so that
  bob's environment settings take precedence.

  On first builds with no history, conservative values are used.

dynamic = {
    jobs = 16,
    wrkobjdir = {
        tmpfs = "/tmp/work",
        disk = "/Users/builder/work",
        threshold = "1G",

        -- By default, builds that failed previously will be assigned to disk
        -- as we cannot guarantee they will fit tmpfs.  Set failed_threshold
        -- to override this when the failed disk usage is under the threshold.
        -- Recommended to be around 50% of the main threshold.
        failed_threshold = "500M",

        -- Some builds use significantly more disk space during the build than
        -- is left at the end when the WRKDIR usage calculations are recorded.
        -- There is no way to accurately catch this, so always_disk forces the
        -- list of specified pkgpaths to always be assigned to disk.
        always_disk = {
            "sysutils/ansible",
        },
    },
}
]]

-- Variables that configure pkgsrc, where it is, what packages to build, etc.
pkgsrc = {
    basedir = "/Volumes/data/pkgsrc",
    -- bootstrap = "/path/to/bootstrap.tar.gz",
    make = "/opt/pkg/bin/bmake",
    -- or pkgpaths = read_pkgpaths("/path/to/file"),
    pkgpaths = {
        "mail/mutt",
        "sysutils/coreutils",
    },

    --[[
      It is strongly recommended to set up an unprivileged user to perform
      builds.  If this is enabled, there is an action below to automatically
      create the user home directory.  If build_user_home is not set it is
      retrieved via getpwnam(3).

    build_user = "builder",
    build_user_home = "/Users/builder",
    ]]

    --[[
      On build failure, save files matching these glob patterns from WRKDIR.

    save_wrkdir_patterns = {
        "**/CMakeError.log",
        "**/CMakeOutput.log",
        "**/config.log",
        "**/meson-log.txt",
    },
    ]]
}

--[[
  Configure pkg_summary generation.  These are the default values.  Enable
  include_restricted if you are not going to publish the packages and want
  restricted NO_BIN_ON_* packages to be included in the pkg_summary file.

summary = {
    include_restricted = false,
    file_cksum = false,
    compression = { "gz", "zst" },
}
]]

-- The sandboxes section defines where, and how, sandboxes will be created.
sandboxes = {
    basedir = "/Volumes/data/chroot",
    -- If bindfs is not in PATH you can set it here.
    -- bindfs = "/usr/local/bin/bindfs",

    --[[
      Environment variable configuration for sandboxed processes.  It is
      recommended to be as strict as possible, especially in the "build"
      section used by production build sandboxes, as pollution from the user
      environment can negatively impact builds.  The "dev" section used by
      "bob sandbox exec" is primarily for interactive pkgsrc work, and you
      may want to set useful variables to suit the development environment.
    ]]
    environment = {
        build = {
            clear = true,
            inherit = { "TERM", "HOME" },
            vars = {
                PATH = "/sbin:/bin:/usr/sbin:/usr/bin",
            },
        },
        dev = {
            clear = true,
            inherit = { "TERM", "HOME" },
            vars = {
                BINPKG_SITES = "${bob_packages}",
                DEPENDS_TARGET = "bin-install",
                PATH = "${bob_prefix}/sbin:${bob_prefix}/bin:/sbin:/bin:/usr/sbin:/usr/bin",
                PS1 = [["sandbox:${bob_sandbox_id} "'${PWD}# ']],
            },
        },
    },

    --[[
      List of actions to apply for each sandbox.  During creation these actions
      are performed in order, and during destruction they are performed in
      reverse order.
    ]]
    setup = {
        { action = "mount", fs = "dev", dir = "/dev" },
        { action = "mount", fs = "tmp", dir = "/tmp", opts = "-e -s 384m" },
        { action = "mount", fs = "tmp", dir = "/var", opts = "-e -s 512m" },
        { action = "cmd", create = "mkdir -m 1777 var/tmp; chmod 1777 tmp" },

        -- Read-only system mounts.
        { action = "mount", fs = "bind", dir = "/Library", opts = "-r" },
        { action = "mount", fs = "bind", dir = "/System", opts = "-r" },
        { action = "mount", fs = "bind", dir = "/bin", opts = "-r" },
        { action = "mount", fs = "bind", src = "/private/etc", dest = "/etc", opts = "-r" },
        { action = "mount", fs = "bind", dir = "/sbin", opts = "-r" },
        { action = "mount", fs = "bind", dir = "/usr", opts = "-r" },

        -- Postfix spool needs to be read-write
        { action = "mount", fs = "bind", dir = "/private/var/spool/postfix" },

        -- Enable DNS resolution via mDNSResponder socket per-sandbox
        { action = "macos-mdns-listener" },

        -- At this point everything should be set up so that chrooted commands
        -- will execute successfully.  Perform additional chroot setup.
        { action = "cmd", chroot = true, create = [[
                mkdir -p /var/{folders,select}
                mkdir -p $(getconf DARWIN_USER_TEMP_DIR)
         ]] },

        -- Configure build user directories if enabled.  Bob automatically
        -- sets bob_build_user* variables when the build user is configured,
        -- and the scripts are executed with 'set -eu', so these should be safe.
        { action = "cmd", only = { set = "pkgsrc.build_user" },
          create = [[
                tempdir=$(sudo -u ${bob_build_user} getconf DARWIN_USER_TEMP_DIR)
                for dir in ${bob_build_user_home} ${tempdir}; do \
                        mkdir -p ${bob_sandbox_path}${dir}
                        chown ${bob_build_user} ${bob_sandbox_path}${dir}
                done
          ]],
          destroy = "rm -rf ${bob_sandbox_path}${bob_build_user_home}" },

        -- It is recommended to mount pkgsrc read-only, but you will first need
        -- to configure DISTDIR, PACKAGES, and WRKOBJDIR to other directories.
        { action = "mount", fs = "bind", dir = pkgsrc.basedir },
    },

    --[[
      Per-package hook actions.  Any create action will run after the internal
      pre-build script (unpacks bootstrap kit if needed), and any destroy
      action runs before the internal post-build script (wipes PREFIX and
      PKG_DBDIR).

    hooks = {
        -- As an example, ensure clean passwd file and /tmp for each build.
        { action = "cmd",
          create = "cp /etc/passwd ${bob_sandbox_path}/etc/passwd",
          destroy = "rm -rf ${bob_sandbox_path}/tmp/*",
        },
    },
    ]]
}
